2017-07-15 57 views
0

我从用户输入和与该输入我用VLOOKUP从另一个workbook.However我不能得到正确的值,总是#VALUE错误VLOOKUP是#VALUE

Option Explicit 

Sub SıraNo() 
     Dim ResimNo As Long 
     Dim Calc As Integer 
     Dim p As Integer 
     Dim k As Integer 
     Dim n As Integer 
     Dim strVariable As Integer 
     Dim wa As Workbook 
     Dim twb As Workbook 
     Set twb = ThisWorkbook 
     Dim j As Integer 

Range("A2", Range("A2").End(xlDown)).Select 
      Do 
      If ActiveCell.Value = "" Then Exit Do 
       ActiveCell.Offset(1, 0).Select 
       n = n + 1 
      Loop 

p = 2 
For k = 1 To n 
      ResimNo = InputBox("Please Enter Resim No") 

         Cells(p, 2).Value = ResimNo 
          strVariable = Left(ResimNo, 4) 
         Cells(p, 3).Value = strVariable 

       If strVariable = 5701 Then 
       Set wa = Workbooks.Open("C:\Users\userpc\Desktop\Gökhan\makro\Teknik Resim Arsiv Listesi_5701.xls") 
        ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$H$1500"), , xlYes).Name = _ 
     "Table1" 

       j = k + 1 

    With twb.Sheets("Sheet1") 

       .Cells(j, 4) = Application.VlookUp(.Cells(j, 2).Value2, "Table1", 6, False) 

    End With 
        End If 
     p = p + 1 
Next k 

End Sub 
+0

当我尝试它,错误框出现:CompileError:Variablle没有定义 –

+0

我的错'Application.VlookUp(.Cells(J,2).Value2,wa.Sheets( 1).Range(“Table1”),6,False)'假设您的Table1列表对象位于“wa”工作簿的第一张工作表 –

+0

这次给出了#N/A,我用不同的工作簿尝试了,再次出现同样的错误,它可以关于我定义的表吗? –

回答

1

你可以试试这样的事情...

Dim tbl As ListObject 
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$H$1500"), , xlYes).Name = _ 
     "Table1" 

Set tbl = ActiveSheet.ListObjects("Table1") 
With twb.Sheets("Sheet1") 
    .Cells(j, 4) = Application.VLookup(.Cells(j, 2).Value2, tbl.Range, 6, False) 
End With