Concatenate Excel Object Name in VBA

Excel18

New Member
Joined
Jul 12, 2016
Messages
5
Hello,
I am attempting to create a Microsoft Excel Object (Sheet Name) in VBA using concatenation. I would like to create a macro which in part loops through different sheets by Sheet Number.

For i = 1 To 10
SheetNumber = Sheet & i
...
SheetNumber.Activate

I would like to make "SheetNumber" become the sheet name in the Object panel. It should become Sheet1, Sheet2, Sheet3...

Currently, it concatenates to just i, because Sheet is blank. If I instead change it to:

For i = 1 To 10
SheetNumber = "Sheet" & i
...
SheetNumber.Activate

it becomes "Sheet1", "Sheet2", "Sheet3"... This also gives me an error.

Does anyone have any suggestions? I have to use the concatenation as opposed to Sheets(i).

Thanks!
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You are creating strings, to use a string to refer to a sheet, you have to use the Sheets collection

Code:
Dim sheetName as String

For I = 1 to 10
    sheetName = "Sheet" & I

    ThisWorkbook.Sheets(sheetName).Activate

    MsgBox "I'm on sheet " & ActiveSheet.Name
Next i
 
Upvote 0
Hello and thank you for rour response! This gives me a Run-time error for "Subscript out of range".
 
Upvote 0
That error suggests that there isn't a sheet named "Sheet1" (or whatever depending on the value of I at the time) in the workbook.
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

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