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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
You can refer to it like
VBA Code:
Range("Page" & i).Value
 
Upvote 0
Solution
Of course! I took the habit of using [] as it is more concise and more readable to me. Tks for the help, works perfect!
 
Upvote 0

Forum statistics

Threads
1,215,422
Messages
6,124,808
Members
449,191
Latest member
rscraig11

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