简体   繁体   English

索引匹配Excel VBA,错误

[英]Index Match Excel VBA, Error

I'm working on a project for my marketing class. 我正在为我的营销课程设计一个项目。 I keep getting the following error, and I can't figure out how to resolve it. 我不断收到以下错误,但我不知道如何解决。 "Object variable or With block variable not set." “未设置对象变量或With块变量。” Can someone please take a look at this?! 有人可以看看这个吗? It would be super appreciated! 这将是非常感谢!

    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

Try to use following code: 尝试使用以下代码:

    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) since lookupRange1 and lookupRange2 are objects, you need to use Set 1)由于lookupRange1lookupRange2是对象,因此需要使用Set

2) your Cells(k, 15).Formula = "..." statement was wrong. 2)您的Cells(k, 15).Formula = "..."语句错误。 See correct one in my code 在我的代码中看到正确的代码

3) for EndRow it's better to use Long type, because max value of Integer is only 32767 3)对于EndRow ,最好使用Long类型,因为Integer最大值仅为32767

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM