我无法获得外部工作簿使用vlookup返回其他列的值。
我正在尝试使用Word文档,将ID号输入文本框,让Vlookup在外部文档中找到ID号,然后从下一列返回名称。然后,该名称将填充不同的文本框,并且输入号码的日期将进入第三个框中。现在,Empid.txt只有两个列A:Empnum和B:Empname。
Private Sub VerID_LostFocus()
Dim emp_number As String
Dim xlApp As Excel.Workbook
Set xlApp = Excel.Workbooks.Open("filepathEmpID.xlsx")
emp_number = VerID.Text
MsgBox emp_number
On Error Resume Next
result = Excel.WorksheetFunction.VLookup(emp_number, xlApp.Worksheets("Sheet1").Range("A:B"), 2, False)
On Error GoTo 0
If result <> "" Then MsgBox result
MsgBox result
VerSig.Text = result
Set xlApp = Nothing
VerDate.Value = Format(Date, "mm/dd/yyyy")
End Sub
emp_number被分配并显示为消息框中的值,但没有返回或分配给结果。第一个"丢失焦点"时,日期在第三个文本框中更新。
WorksheetFunction
与应用程序对象一起使用,因为您已经声明了XlApp
,因为工作簿将行更改为
Result = xlApp.Application.WorksheetFunction.VLookup(emp_number, Worksheets("Sheet1").Range("A:B"), 2, False)
它正在工作。另外,如果在工作簿中输入emp_number
,请进行以下更改
Dim emp_number As Variant
'
'
'
'
emp_number = Val(VerID.Text)