VBA Xlookup Never Returns Results

Rex2024

New Member
Joined
Nov 17, 2022
Messages
32
Office Version
  1. 365
Platform
  1. Windows
I would like my user to enter their employee number in a field which will then lookup their name upon clicking a command button. I will transfer this name to my next form for various uses.

Employee Numbers are in column A & First name in column B

This formula works in the worksheet with the data and want to just enter an employee number in cell F2:

=XLOOKUP(F2, A:A, B:B, "Not Found", 0, 1)

But this doesn't work in the VBA form, it always returns "Not found":

Sub OpenRecordLookups_Click()

Dim DPUsingForm As String

DPUsingForm = Application.WorksheetFunction.XLookup(Me.DPEmpNum.Value, ThisWorkbook.Sheets("AuthorizedUsers").Range("A:A"), ThisWorkbook.Sheets("AuthorizedUsers").Range("B:B"), "Not Found", 0, 1)

MsgBox "Welcome employee " & DPUsingForm

End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi & welcome to MrExcel.
How about
VBA Code:
With ThisWorkbook.Sheets("AuthorizedUsers")
DPUsingForm = Application.XLookup(CLng(Me.DPEmpNum.Value), .Range("A:A"), .Range("B:B"), "Not Found", 0, 1)
End With
 
Upvote 0
That worked!
Hi & welcome to MrExcel.
How about
VBA Code:
With ThisWorkbook.Sheets("AuthorizedUsers")
DPUsingForm = Application.XLookup(CLng(Me.DPEmpNum.Value), .Range("A:A"), .Range("B:B"), "Not Found", 0, 1)
End With
That worked!

Why did that work and what I did not work?
 
Upvote 0
Assuming that DPEmpNum is a textbox, combobox or similar it will always return text & not a number, wrapping it in CLng converts it to a number.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,607
Members
449,037
Latest member
Arbind kumar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top