[英]Using VBA to perform Vlookup on an external workbook from Word
我無法使用外部工作簿來使用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("filepath\EmpID.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
與Application Object一起使用,因為您已經將XlApp
聲明為Workbook,所以將行更改為
Result = xlApp.Application.WorksheetFunction.VLookup(emp_number, Worksheets("Sheet1").Range("A:B"), 2, False)
它正在工作。 另外,如果在工作簿emp_number
輸入為Number,則進行以下更改
Dim emp_number As Variant
'
'
'
'
emp_number = Val(VerID.Text)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.