我正在为我的营销课程开展一个项目。我一直收到以下错误,我无法弄清楚如何解决它。 “对象变量或未设置块变量。”有人可以看看这个吗?!非常感谢!
Dim k as Integer
Dim EndRow As Integer
Dim lookupRange1 As Range
Dim lookupRange2 As Range
Dim lookupValue1 As Integer
EndRow = Range("M" & Rows.Count).End(xlUp).Row
lookupRange1 = Sheets("Data_Main").Range("C2:C50000")
lookupRange2 = Sheets("Data_Main").Range("A2:A50000")
With Application.WorksheetFunction
For k = 2 To EndRow
lookupValue1 = Cells(k, 13).Value
Cells(k, 15).Formula = ".Index(lookupRange1, .Match(lookupValue1, lookupRange2, 0))"
Next k
End With
答案 0 :(得分:1)
尝试使用以下代码:
Dim k As Long
Dim EndRow As Long
Dim lookupRange1 As Range
Dim lookupRange2 As Range
Dim lookupValue1 As Integer
EndRow = Range("M" & Rows.Count).End(xlUp).Row
Set lookupRange1 = Sheets("Data_Main").Range("C2:C50000")
Set lookupRange2 = Sheets("Data_Main").Range("A2:A50000")
For k = 2 To EndRow
lookupValue1 = Cells(k, 13).Value
Cells(k, 15).Formula = "=Index(" & lookupRange1.Address & ", Match(" & lookupValue1 & ", " & lookupRange2.Address & ", 0))"
Next k
1)由于lookupRange1
和lookupRange2
是对象,因此您需要使用Set
2)你的Cells(k, 15).Formula = "..."
陈述错了。在我的代码中查看正确的
3)对于EndRow
,最好使用Long
类型,因为Integer
的最大值仅为32767