How to update data in worksheet on workbook open.
A user inputted data like this in January.
EXP DATE NAME
Jan. 27 ABC Trucking
Feb. 22 123 Logistics
May. 1 Red dog moving
June.9 Barts trucking
He opens it up on 4-24-18 it would look like this having updated the data in the following manner:
EXP DATE NAME
May. 1 Red dog moving
June.9 Barts trucking
Jan. 27 ABC Trucking
Feb. 22 123 Logistics
Watch the video below:
Watch this video on Youtube.
Here’s the complete VBA code to update the worksheet data automatically when the workbook is opened:
Dim count As Long
Sub moveOlderData()
Dim i As Long, lastrow As Long
lastrow = Sheets(“Sheet1”).Range(“A” & Rows.count).End(xlUp).Row
count = 0
For i = 2 To lastrow
If Cells(i, 1).Value < Date Then
count = count + 1
lastrow = lastrow + 1
Range(Cells(i, 1), Cells(i, 2)).Copy Range(Cells(lastrow, 1), Cells(lastrow, 2))
End If
Next i
End Sub
Sub deldata()
lastrow = Sheets(“Sheet1”).Range(“A” & Rows.count).End(xlUp).Row
lastrow = lastrow – count
For p = 2 To lastrow
If Cells(p, 1) < Date Then
Range(Cells(p, 1), Cells(p, 2)).ClearContents
End If
Next p
End Sub
Sub delBlanks()
Dim q As Long, lastrow As Long
lastrow = Sheets(“Sheet1”).Range(“A” & Rows.count).End(xlUp).Row
‘MsgBox lastrow
‘lastrow = Sheet1.Cells.Find(What:=”*”, After:=Range(“A1”), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For q = lastrow To 2 Step -1
If Cells(q, 1) = “” Then
Cells(q, 1).EntireRow.Delete
End If
Next q
End Sub
Private Sub Workbook_Open()
moveOlderData
deldata
delBlanks
End Sub