Refer to named range using variables

gmcgough600

New Member
Joined
Nov 21, 2017
Messages
33
Office Version
  1. 365
Hi,

I'm trying to copy a named range without having to have the name of the range hard coded but having difficulty getting this to work, attempt below but it's not working as I expected it to:

This is at the top of the Module as I'm using a UserForm to define the variable site_name:

Code:
Public site_name As String

site_name has been set to "PEHE"

Code:
Application.Range(site_name & "_PI_tags").Copy

The named range is PEHE_PI_tags so I had assumed that I can construct this name from a combo of var and the text string "_PI_tags".

Thanks
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Is all the code in the userform module?
 
Upvote 0
You can construct the name from the variable and the string so id guess you either misspelled the name or the variable doesnt hold the value you think it does.
 
Upvote 0
No none of the code I've given is, the only code used there is:

Code:
Private Sub UserForm_Initialize()'Link OptionButton names to site name cells
Me.OptionButton1.Caption = ThisWorkbook.Sheets("Input").Range("C5").Value
Me.OptionButton2.Caption = ThisWorkbook.Sheets("Input").Range("D5").Value
End Sub

Range C5 contains "PEHE"

Code:
Private Sub OKCommandButton_Click()
   Select Case True
      Case Me.OptionButton1
         site_name = ThisWorkbook.Sheets("Input").Range("C5")
      Case Me.OptionButton2
         site_name = ThisWorkbook.Sheets("Input").Range("D5")
   End Select
Unload Me
End Sub
 
Upvote 0
Thanks, the named range is definitely "PEHE_PI_tags" and watch window shows:

Expression Value Type
site_name : "PEHE" : String :

You can construct the name from the variable and the string so id guess you either misspelled the name or the variable doesnt hold the value you think it does.
 
Last edited:
Upvote 0
In that case try using a message box like
Code:
MsgBox site_name
Range(site_name & "_PI_tags").Copy
to check the value
 
Upvote 0
Glad it's sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,837
Messages
6,181,255
Members
453,028
Latest member
letswriteafairytale

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