2014-01-23 6 views
0

나는 내 마케팅 수업을위한 프로젝트를 진행하고 있습니다. 다음과 같은 오류가 계속 발생하며 해결 방법을 파악할 수 없습니다. "개체 변수 또는 With 블록 변수가 설정되지 않았습니다." 누군가가 이것 좀 봐 주실 래요?! 그것은 매우 감사하게 될 것입니다!색인 일치 Excel VBA, 오류

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 

답변

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 = "..." 문을 잘못 사용할 필요가있다. Integer의 최대 값은 32767

때문에, Long 타입을 사용하는 것이 좋습니다 EndRow 내 코드

3)에서 올바른 참조