簡體   English   中英

索引匹配Excel VBA,錯誤

[英]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)由於lookupRange1lookupRange2是對象,因此需要使用Set

2)您的Cells(k, 15).Formula = "..."語句錯誤。 在我的代碼中看到正確的代碼

3)對於EndRow ,最好使用Long類型,因為Integer最大值僅為32767

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM