I wrote a VB macro in excel to set a "Autotest" worksheet active and activate the cell with exact match "Date". Then, I change every date underneath this cell to advance the year value ahead 4 years. I have a lot of data sets, so I wanted this to run no regardless of format of the work books. I know there will always be an "Autotest" sheet and I know there will always be a "Date" cell above the list of dates. This code works for most of the data sets but I keep getting this error, the debug points to the lastrow
statement but I can't determine the issue. Thanks in advance.
Sub FourYrsAhead()
' FourYrsAhead Macro
' This macro will forward all of the dates under the "Dates" column in the Autotest sheet forward 4 years.
'Sets variable lastRow as an integer
Dim lastRow As Integer
'Sets theSheetImWorkingOn as a worksheet variable
Dim theSheetImWorkingOn As Worksheet
'Sets theColumnNumberForTheDates as an integer
Dim theColumnNumberForTheDates As Integer
'Activates the Autotest worksheet as the active sheet
Worksheets("Autotest").Activate
'Finds the exact string 'Date' and makes it the active cell
Cells.Find(What:="Date", LookAt:=xlWhole).Activate
'Sets the column number to the current active call value
theColumnNumberForTheDates = ActiveCell.Column
'Sets the sheet that will be worked on
Set theSheetImWorkingOn = Sheets("Autotest")
'Sets the last row variable
lastRow = theSheetImWorkingOn.Cells(1000000, theColumnNumberForTheDates).End(xlUp).Row
'For loop that starts as the active cell row plus 1 and loops down each row
For x = ActiveCell.Row + 1 To lastRow
'Changes the yyyy value in each row ahead 4 years
theSheetImWorkingOn.Cells(x, theColumnNumberForTheDates) = DateAdd("yyyy", 4, theSheetImWorkingOn.Cells(x, theColumnNumberForTheDates))
'Moves to the next row
Next x
End Sub