Defining a range in VBA using [] and a variable

schabo

New Member
Joined
Jan 24, 2021
Messages
9
Office Version
  1. 2019
Platform
  1. Windows
Hello,
In my spreadsheet, I defined some name ranges as Page1, Page2, Page3 etc. I want to generate a simple string in VBA that refers to these Name Ranges:
Dim i As Integer
Dim j As String
For i = 1 To 5
j = "Settings For " & [Page & i] & ".xlsm"
Do something
Next i

The Range [Page & i] is obviously not correct. If I write it as [Page1], my string is ok, but I need the name of the page to be define by where we are in the loop. This is just me not being able to use the [] properly. If someone can show me the correct nomenclature, that would be appreciated.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,423
Office Version
  1. 365
Platform
  1. Windows
You can refer to it like
VBA Code:
Range("Page" & i).Value
 
Solution

schabo

New Member
Joined
Jan 24, 2021
Messages
9
Office Version
  1. 2019
Platform
  1. Windows
Of course! I took the habit of using [] as it is more concise and more readable to me. Tks for the help, works perfect!
 

Watch MrExcel Video

Forum statistics

Threads
1,133,270
Messages
5,657,750
Members
418,411
Latest member
Excellency

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