VBA VLookup Failing

cmf30

New Member
Joined
Apr 27, 2020
Messages
8
Office Version
  1. 365
  2. 2016
Platform
  1. MacOS
Hi,

I am very much a beginner at VBA and I am trying to use a VLookup and it keeps erroring and (I am not sure why). I have 2 sheets one called COMBINED and the other called HRIS.
1) I want to take a Staff ID in COMBINED tab and find the corresponding ID in the HRIS file
2) Check the nationality for that person (which is in a different column of HRIS tab)
3) Once done, I want to update the Nationality column in the COMBINED File if the existing value is blank

The values I am looking for do exist in the HRIS tab.

I am using a VLookup to do this, but at the moment it keeps failing to get Run Error 1004 or Error Method Vlookup of Object Worksheet Function failed.

The relevant lines of the code is as follows:

VBA Code:
strNationality = Application.WorksheetFunction.VLookup(strStaffID, Sheets("HRIS").Range("G2:Z500"), 10, False)
 
If Sheets("COMBINED").Range("S" & i).Value = "" Then

Sheets("COMBINED").Range("S" & i).Value = strNationality

Else

End If
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi Steve,

Thanks for getting back

Sorry, is the StaffID that is being searched from It is in COMBINED tab.

The Staff ID is present in both tabs and so that is why I am using it to compare. But I am wondering if there is an issue with the formatting because I just did the below check and the two appear to be a different format. One seems to be a string and the other a number and so I am guessing the Vlookup doesn't see them as equal (below example, both CombinedL2 and HRISG51 show the same staff ID). Per below, I did try to convert the column formats beforehand but the two formats are still coming out as different.

Worksheets("HRIS").Columns("G").NumberFormat = "0"
Worksheets("COMBINED").Columns("L").NumberFormat = "0"

MsgBox VarType(Sheets("COMBINED").Range("L2"))
MsgBox VarType(Sheets("HRIS").Range("G51"))

Kind regards,
Claire
 
Upvote 0
Thats almost certainly why it isnt working. Is it numeric? I still cant see where you are assigning it.
 
Upvote 0
It probably is im guessing. Use
VBA Code:
Dim strStaffID as Long
and see if that helps.
 
Upvote 0
Thanks a million, Steve, that worked! Really appreciate it.
 
Upvote 0

Forum statistics

Threads
1,215,018
Messages
6,122,703
Members
449,093
Latest member
Mnur

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