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
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