我的代码成功地解决了我的最后一行代码然后在达到设置数据范围时给出运行时错误1004但我希望代码运行到我的范围中的最后一行。有谁知道如何修复我的代码?
Sub PopulatingArrayVariable()
Dim myArray() As Variant
Dim DataRange As Range
Dim cell As Range
Dim x As Long
Dim TotalTargets As Double
Dim lastrow As Long
lastrow = Cells(Rows.Count, "D").End(xlUp).Row
TotalTargets = WorksheetFunction.Max(Columns("D"))
Set DataRange = Sheets("Result").Range("D:I" & lastrow)
For Each cell In DataRange.Cells
ReDim Preserve myArray(x)
myArray(x) = cell.Value
x = x + 1
Next cell
End Sub
答案 0 :(得分:1)
这里的错误类型:
Set DataRange = Sheets("Result").Range("D:I" & lastrow)
应该是
Set DataRange = Sheets("Result").Range("D1:I" & lastrow)
或Dx,其中x是您的第一行。