我在excel中编写了一个VB宏来设置"自动测试"工作表处于活动状态并激活具有完全匹配的单元格"日期"。然后,我更改此单元格下的每个日期以提前4年的年份值。我有很多数据集,所以无论工作书的格式如何,我都希望这样做。我知道总有一个"自动测试"表格,我知道总会有一个"日期"日期列表上方的单元格。此代码适用于大多数数据集,但我一直收到此错误,调试指向lastrow
语句,但我无法确定问题。提前谢谢。
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
答案 0 :(得分:0)
根据您运行的Excel版本,可能超出了Excel的行限制。在2003年及更早版本中,工作表限制为65536行,因此调用Cells
中的第1,000,000行将导致错误。但是,如果您正在运行2007或更高版本,那么这不应该是问题。
使用YourSheet.Rows.Count
代替1000000
将避免导致此错误的工作簿类型,同时仍确保涵盖所有数据