Using Vlookup On Closed Workbook With VBA resulting in all #NA

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am trying to populate cells of my worksheet using a vlookup formula on a closed workbook.

VBA Code:
              For L1 = 2 To nrec
                .Cells(L1, 7) = "=vlookup(RC[-1],('d:\wsop2023\SupportData\[Facilities.xlsx]Facilities'!R2C1:R500C18),8,false)" 'label
                .Cells(L1, 8) = "=vlookup(RC[-2],('d:\wsop2023\SupportData\[Facilities.xlsx]Facilities'!R2C1:R500C18),5,false)" 'type
                .Cells(L1, 9) = "=vlookup(RC[-3],('d:\wsop2023\SupportData\[Facilities.xlsx]Facilities'!R2C1:R500C18),6,false)" 'unit
                .Cells(L1, 10) = "=vlookup(RC[-4],('d:\wsop2023\SupportData\[Facilities.xlsx]Facilities'!R2C1:R500C18),3,false)" 'class
                .Cells(L1, 11) = "=vlookup(RC[-5],('d:\wsop2023\SupportData\[Facilities.xlsx]Facilities'!R2C1:R500C18),4,false)" 'resp
                .Cells(L1, 12) = "=vlookup(RC[-6],('d:\wsop2023\SupportData\[Facilities.xlsx]Facilities'!R2C1:R500C18),7,false)" 'lit
                .Cells(L1, 13) = "=vlookup(RC[-7],('d:\wsop2023\SupportData\[Facilities.xlsx]Facilities'!R2C1:R500C18),9,false)" 'washrooms
                .Cells(L1, 14) = "=vlookup(RC[-8],('d:\wsop2023\SupportData\[Facilities.xlsx]Facilities'!R2C1:R500C18),10,false)" 'washroom code
                .Cells(L1, 15) = "=vlookup(RC[-9],('d:\wsop2023\SupportData\[Facilities.xlsx]Facilities'!R2C1:R500C18),11,false)" 'family
                .Cells(L1, 16) = "=vlookup(RC[-10],('d:\wsop2023\SupportData\[Facilities.xlsx]Facilities'!R2C1:R500C18),13,false)" 'groom
                .Cells(L1, 17) = "=vlookup(RC[-11],('d:\wsop2023\SupportData\[Facilities.xlsx]Facilities'!R2C1:R500C18),14,false)" 'active/passive
                .Cells(L1, 18) = "=vlookup(RC[-12],('d:\wsop2023\SupportData\[Facilities.xlsx]Facilities'!R2C1:R500C18),18,false)" 'address
              Next L1

This code is placing the applicable vlookup formula in the appropriate cell. The "Facilities.xlsx" workbook is closed and resides in the path indicated in the formula. The worksheet and range are both correct.
When I run this code, I get all #N/A.
The formula applied to the cell on the worksheet appears correct. For example, at cell G5, the formula is "=VLOOKUP(F2,('d:\wsop2023\SupportData\[Facilities.xlsx]Facilities'!$A$2:$R$500),8,FALSE)" This correct. Look for the value in F2 in workbook Facilities.xlsx, worksheet Facilities, range A2:R500. The value for F2 will be found in column A, and it will return a value from column H.

Any thoughts on where I may have erred?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
It looks like you have an extra bracket, for example try...

VBA Code:
.Cells(L1, 7) = "=vlookup(RC[-1],('d:\wsop2023\SupportData\[Facilities.xlsx]Facilities'!R2C1:R500C18,8,false)" 'label

Hope this helps!
 
Upvote 0
By the way, I would suggest that you specify the FormulaR1C1 property of the Range object for clarity...

VBA Code:
.Cells(L1, 7).FormulaR1C1 = "=vlookup(RC[-1],('d:\wsop2023\SupportData\[Facilities.xlsx]Facilities'!R2C1:R500C18,8,false)" 'label

Hope this helps!
 
Upvote 0
Thank you Domenic,
I'm running into an "Application defined or object defined error" with the bracket removed. Without it, I have two opening, and one closing bracket, so not sure if that would be the problem. Putting back in the bracket rids of the error, but results again in the #NA.
VBA Code:
.Cells(L1, 7).FormulaR1C1 = "=vlookup(RC[-1],('d:\wsop2023\SupportData\[Facilities.xlsx]Facilities'!R2C1:R500C18,8,false)" 'label
 
Upvote 0
The way I solve these problems when writing formula using VBA; is to get rid of the equals sign so it just writes a string to the cell. Then go to the cell update the cell ( Hit F2) and put the equals sign back in, and excel will hopefully tell you what is wrong
 
Upvote 0
Thanks for the suggestion. If I followed you correctly, I changed the line of code to
VBA Code:
.Cells(L1, 7).FormulaR1C1 = "vlookup(RC[-1],('d:\wsop2023\SupportData\[Facilities.xlsx]Facilities'!R2C1:R500C18,8,false)" 'label
, removing the"=" preceeding "vlookup"
When run, this put the formula in the call as a string value. F2 didn't do anything.
Just for giggles, I put an = back in front of vlookup to make it a formula, hit F2, nothing happened, then return and nothing happened but RC[-1] was highlighted as if wanting to accept a value? I may not have did this correctly.

If I manually enter the formula into the cell, I get the correct result.
Code:
=VLOOKUP(F2,Facilities.xlsx!$A$2:R500,8,FALSE)

Perhaps RC[-1] is referring to the wrong cell? I'm not real familiar with using R1C1 method. I need to place the looked up value in column 8, based on a reference in the data to column F
 
Last edited:
Upvote 0
Sorry, my mistake. I didn't notice the matching bracket toward the beginning of the formula. The syntax seems correct. But you say that it returns #N/A.

When the error occurs, what is the lookup value? And what type of values does Facilities.xlsx!A2:A500 contain?
 
Upvote 0
Silly question ... how can I determine what the lookup value is being seen as vs. what I think it should be? Like I said, when the code runs, it places the right formula into the cell with the correct syntax, which would be pointing to the correct cell for the lookup value. The cell value being looked up is text, and does reside in the reference database. A2:A500 contains text values that should match values being looked up.
 
Upvote 0
It looks like Column F contains your lookup values. Can you post a few examples of the lookup values?

And the same thing for your lookup range in Facilities.xlsx!A2:A500. Can you post a few examples?

Better still, it would be even more helpful if you could post some sample data for both your lookup values and lookup table using the xl2BB add-in.
 
Upvote 0
Here is a sample of the lookup values. Because it's sensitive personal data, I have had to censor it.
WSOP 23_v23.04.24 .xlsm
F
1Facility/Equipment/Instructor
2Bxxxxel - Bxxxxel xxxxd #1
3Bxxxxel - Bxxxxel xxxxd #3
4Bxxxxel - Bxxxxel xxxxd #4
5Bxxxxel - xxxmoxx
6Bxx - Loxxx xxxxd
7Bxx - Upxxx xxxxd
8Cexxxxrae PS - xxxxxxd #1
9Cexxxxrae PS - xxxxxxd #2
10Cxxxxnnial PS - xxxxxxd #1
11Cxxxxnnial PS - xxxxxxd #2
12Cxxxxrvaxxxx xxxxows xxxxd
13Hxxxxxxe - Hxxxxtaxxxx xxxx
14Hxxxxxxe - Loxxx xxxxxxd
15Hxxxxxxe - Loxxx xxxxxxd
16Hxxxxxxe - Upxxx xxxxxxd
17Hxxxxxxe - Upxxx xxxxxxd
CoreData_08-Jul


And the lookup range ...
Facilities.xlsx
A
1A FIELD (do not alter)
2xxxurn xxxxxxd
3Bxxxxel - xxxxxxxd
4Bxxxxel - Bxxxxel xxxxd #1
5Bxxxxel - Bxxxxel xxxxd #2
6Bxxxxel - Bxxxxel xxxxd #3
7Bxxxxel - Bxxxxel xxxxd #4
8Bxxxxel - Kxxxxis/Bxxxxel Sidxxxxk Txxxl
9Bxxxxel - Txxxxs
10Bxxxx xxxxh xxxxd
11Bxx - Loxxx xxxxd
12Bxx - Upxxx xxxxd
13Cexxxxrae PS - xxxxxxd #1
14Cexxxxrae PS - xxxxxxd #2
15Cxxxxnnial PS - xxxxxxd #1
16Cxxxxnnial PS - xxxxxxd #2
17Cxxxxrvaxxxx xxxxows xxxxd
18Crexxxxde - xxxxd #1 (North)
19Crexxxxide - xxxxd #2 (South)
20Crexxxxde - xxxxd #3 (East)
21xxxxbrixxx - xxxxd
22xxxxbrix - xxxxxxd
23Emxxxx PS xxxxxxd
24Hxxxxxxe - Hoxxxtalxxx Axxa
25Hxxxxxxe - Loxxx xxxxxxd
26Hxxxxxxe - Upxxx xxxxxxd
Facilities


Not sure if it will help or not, but it's the best I can do. I did check the length of the data string in the lookup values to rule out the possibility of a trailing space or something. The length came back matching what was really there.
 
Upvote 0

Forum statistics

Threads
1,216,303
Messages
6,129,983
Members
449,548
Latest member
lharr28

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