vba vlookup & named range

Joe Z

New Member
Joined
Mar 9, 2006
Messages
44
I am having difficulty using vlookup in vba using FormulaR1C1. I am tring to lookup a value from a table using the table O_Rng located in a different sheet. Here is what I have:
HTML:
Dim O_Rng as range

set O_Rng = Range("org_tbl!$A$2:$H$59")

sheets("Sheet3").cells(2,9).select
activecell.offset(4,0).FormulaR1C1= "=vlookup(RC[-7],org_tbl!O_Rng,2,false)

I get #Name error.
I have tried changing the lookup table value to
HTML:
activecell.offset(4,0).formulaR1C1= 
"=vlookup(RC[-7],Worksheets(""org_tbl"").range(""$A$2:$H$59""),2,false)
with no luck, yet I can use
HTML:
Activecell.offset(4,0) = application.worksheetfunction _
.vlookup(Activecell.offset(4,-7),worksheets("org_tbl").range("$A$2:$H$59"),2,false)
and it gives me the correct result.

Any suggestions, thanks in advance!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
Joe

It's not particularly clear what you want to do but perhaps this might work.
Code:
Dim O_Rng As Range
Set O_Rng = Range("org_tbl!$A$2:$H$59")
Sheets("Sheet3").Cells(2, 9).Offset(4, 0).FormulaR1C1 = "=vlookup(RC[-7],org_tbl!" & O_Rng.Address(ReferenceStyle:=xlR1C1) & ",2,false)"
PS Why are you mixing up A1 and R1C1 notation? Wouldn't it just become confusing.:eek:
 
Upvote 0

Joe Z

New Member
Joined
Mar 9, 2006
Messages
44
Joe

It's not particularly clear what you want to do but perhaps this might work.
Code:
Dim O_Rng As Range
Set O_Rng = Range("org_tbl!$A$2:$H$59")
Sheets("Sheet3").Cells(2, 9).Offset(4, 0).FormulaR1C1 = "=vlookup(RC[-7],org_tbl!" & O_Rng.Address(ReferenceStyle:=xlR1C1) & ",2,false)"
PS Why are you mixing up A1 and R1C1 notation? Wouldn't it just become confusing.:eek:
Thank you! You were right on. My problem was I did not realize that I needed to include the .address to the named range. I am very new to vba, hence the mixing od A1 R1C1 notation. It was the result of trying numerous scenarios and getting them all meshed together. Thanks.
 
Upvote 0

Forum statistics

Threads
1,191,273
Messages
5,985,691
Members
439,974
Latest member
sjoerdbosch

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
Top