I can't get an external workbook to use Vlookup to return the value of a different column.
I am trying to take a word document, enter an ID number into a text box, have vlookup find the ID number in an external document then return a name from the next column. Then the name will populate a different textbox and the date the number was entered will go in a third box. Right now EMPID.txt has only two columns A: EmpNum and 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 get assigned and shows as the value in a message box but nothing gets returned or assigned to result. The date is updated in the third textbox anytime the first "lost focus".
WorksheetFunction
works with Application Object, Since You already declared XlApp
as Workbook change the line to
Result = xlApp.Application.WorksheetFunction.VLookup(emp_number, Worksheets("Sheet1").Range("A:B"), 2, False)
and it is working. Also if emp_number
is entered as Number in the workbook, then make the following changes
Dim emp_number As Variant
'
'
'
'
emp_number = Val(VerID.Text)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.