2017-08-16 49 views
0

爲什麼我收到錯誤「無法獲取WorksheetFunction類的VLookup屬性」?在這裏發生錯誤:Seg = Application.WorksheetFunction.VLookup(Code, rng, 2, False)爲什麼我的VLookup無法正常工作?

Sub RateCode() 
Dim I As Long 
Dim endRow As Long 
Dim Rate_Code() As Variant 
Dim ws As Worksheet 
Dim ws2 As Worksheet 
Dim rng As Range 
Dim Seg As String 
Set ws2 = Worksheets("Segment") 
Set ws = Worksheets("Sheet1") 
Set rng = ws2.Range("A1:B1300") 
     With ws 
     endRow = Sheet1.Range("A999999").End(xlUp).Row 
     endRow = endRow 
     Rate_Code = Sheet1.Range("F3:F" & endRow) 
     For I = LBound(Rate_Code) To UBound(Rate_Code) 
      Code = Rate_Code(I, 1) 

      If Len(Code) = 5 Then 
       Code = "'" & Mid(Code, 3, 2) 
       Cells(I + 2, 7).Value = Code 
      Else 
       Code = "'" & Left(Code, 2) 
       Cells(I + 2, 7).Value = Code 
      End If 
      Seg = Application.WorksheetFunction.VLookup(Code, rng, 2, False) 
      Seg = Cells(I + 2, 8).Value 
     Next I 
    End With 
End Sub 
+0

且您已經查看[這些](https://stackoverflow.com/search?q=Unable+to+獲得+ + +的工作表+功能+級)的+ VLOOKUP +物業+? – pnuts

+0

@pnuts我已經看過這些答案,但我仍在掙扎。 – Anthony

+0

@Anthony請檢查'代碼'(查找值)是否返回或不在vlookup中並將值存儲在'seg'中 - 嘗試將查找值存儲在單元格中 – Ashok

回答

0

嘗試這個概念可能會有所幫助到u

Sub RateCode() 
Dim I, y As Long 

    Dim endRow As Long 
    Dim Rate_Code() As Variant 
    Dim ws As Worksheet 
    Dim ws2 As Worksheet 
    Dim rng As Range 
    Dim Seg As String 
    Set ws2 = Worksheets("Segment") 
    Set ws = Worksheets("Sheet1") 
    Set rng = ws2.Range("A1:B1300") 
    With ws 

      lr = ws.Range("F" & Rows.Count).End(xlUp).Row 

     For I = 3 To lr 
      Code = Rate_Code(I, 1) 
      If Len(Code) = 5 Then 
      Code = "'" & Mid(Code, 3, 2) 
      Cells(I + 2, 7).Value = Code 
      Else 
      Code = "'" & Left(Code, 2) 
      Cells(I + 2, 7).Value = Code 
      End If 
      y = 8 
      Cells(I, y) = Application.WorksheetFunction.VLookup(Code, rng, 6, 0) 
     Next I 
     End With 
    End Sub