Refer to named range using variables

gmcgough600

New Member
Joined
Nov 21, 2017
Messages
32
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

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
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,215,191
Messages
6,123,553
Members
449,108
Latest member
rache47

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