Using Variables as sheet names in VBA (Sheets.Add)

kotzus

New Member
Joined
Mar 21, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi,

Trying to use a variable string as the name in a sheet.

Public mon As String (mon is "4")
c1 = """"
c2 = c1 + mon + c1
Sheets.Add after:=Worksheets(c2)



c2 has the value of "4"

This is not working. Probably because all of it is the string and the Function " is missing

I would be obliged if you could assist me

Regards
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You haven't really explained what you want to do in terms of starting conditions and desired result.

What does "not working" mean? What do you want to happen and what happens instead?

This code will add a new sheet after the existing sheet named "4".

Other than that I don't know what your question is.
 
Upvote 0
Public mon As String (mon is "4")
That cannot be what you have. Post the exact procedure and if you want to include information, use comments like this
VBA Code:
Public mon As String()
c2 = c2 + 1 'this is a comment about c2
End Sub
and use code tags so that the code maintains its indentation.

Also explain what you are trying to achieve. You should also always declare your variables if for no other reason that others won't have to guess what they represent. I always enforce variable declaration with Option Explicit.
 
Upvote 0
Dear all,
thank you for your suggestions

I need to create a sheet name based on a variable
e.g. sheet name should be 4. String "4" is being taken from a variable with the name mon.
the line should look like Sheets.Add after:=Worksheets("4")

VBA Code:
Public Sub Month_Click()

mon = goMonth.Value

Dim c1, c2 As String
c1 = """"
c2 = c1 + mon + c1

Sheets.Add after:=Worksheets(c2)

Result: Subscript out of range
 
Upvote 0
I think you're code is trying to add a new worksheet after one that doesn't exist! The name in brackets should be the sheet you want the new one to come after but if I'm understanding you correctly, it looks like you want a new sheet named 4 to come after a non-existent sheet also named 4.
 
Upvote 0
If you want to create a new worksheet as the last worksheet and name it "4" you need to do this:
VBA Code:
Dim NewSheet As Worksheet
Set NewSheet = Sheets.Add (after:=Worksheets(Worksheets.Count))
NewSheet.Name = c2
 
Upvote 0
Hi and thank you for the reply.

VBA Code:
Workbooks("IC1.xlsm").Activate
  
Sheets.Add after:=Worksheets(c2)
The last worksheet has the name "Result10" I have already 2 sheets in my book

Still I think there must be a problem with the created string c2 and the interpretation in the Sheets.Add
 

Attachments

  • vba.jpg
    vba.jpg
    14.8 KB · Views: 2
Upvote 0
Read post #6 for the solution.

Your original line of code
Rich (BB code):
Sheets.Add after:=Worksheets(c2)
does NOT add a worksheet named 4. It adds a new worksheet AFTER the worksheet named 4. Except there is no worksheet named 4, so you get an error.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,097
Messages
6,123,076
Members
449,094
Latest member
mystic19

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