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!
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
22,784
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
 

Excel18

New Member
Joined
Jul 12, 2016
Messages
5
Hello and thank you for rour response! This gives me a Run-time error for "Subscript out of range".
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
22,784
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.
 

Forum statistics

Threads
1,081,747
Messages
5,361,038
Members
400,610
Latest member
ebey

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top