VBA-Using a vLookup formula with a variable for a named range in another workbook

wilt823

New Member
Joined
Feb 29, 2020
Messages
18
Office Version
  1. 2013
Platform
  1. Windows
I am trying to create a vLookup formula using a variable for the named range within another workbook. I have already researched numerous websites including this one and nothing I have tried works. I am still a VBA novice, so I apologize in advance if this turns out to be something very simple.

I have a workbook (dim wb) which contains the macro and has all of the data used to populate a template workbook. The template file is the active workbook in this part of the macro and also has the cell in which I am putting the vLookup formula. The lookup range is another cell in the template file and the table array is a named range ("rngBilling") within the macro workbook on tab "Billing" (dim ws). When I run the macro I get error msg 1004 "application-defined or object-defined error" on the last line of the code. When I step into the code and hover the cursor over the variables in the table array part of the formula, all three show the correct value (filename: Billing Detail.xlsm, sheet name: Billing, and cell range: Billing A6:M53).

Dim wb As Workbook
Set wb = ThisWorkbook
Dim ws As Worksheet
Set ws = wb.Worksheets("Billing")
Dim rngBilling As range
Set rngBilling = ws.range("rngBilling")


range("E48").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(R[-45]C[-2]," & wb.Name & ws.Name & rngBilling.Name & ",7)"

Not sure if the problem is the ws.name or the rngBilling.name or both; I have used the wb and ws in other parts of the macro without trouble, but this is the only place so far where I've tried to use the rngBilling.


When I use this code in place of the last line it works perfectly:
'ActiveCell.FormulaR1C1 = _ '"=VLOOKUP(R[-45]C[-2],'[" & wb.Name & "]Billing'!R7C1:R47C13,7)"
But the table array is not dynamic and will not capture changes in the named range "rngBilling" which happen often.

Any help would be greatly appreciated.
Thank you.
Win10, Off 2013
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
How about
VBA Code:
Range("E48").FormulaR1C1 = _
   "=VLOOKUP(R[-45]C[-2],'[" & wb.Name & "]" & ws.Name & "'!" & rngBilling.Address(, , xlR1C1) & ",7)"
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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