[英]Index Match Excel VBA, Error
我正在为我的营销课程设计一个项目。 我不断收到以下错误,但我不知道如何解决。 “未设置对象变量或With块变量。” 有人可以看看这个吗? 这将是非常感谢!
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
尝试使用以下代码:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.