How to name a new worksheet that is added through VB

amiersen

New Member
Joined
Jan 11, 2005
Messages
21
I am using this code to create a new worksheet within an "IF" statement:

Application.Worksheets.Add

How do I rename this new worksheet without needing to know what (automatic) name it was assigned (ex. Sheet1)

Thanks!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
You can use a variable to do it. You don't mention what your If statement is, so I just threw one together:

<font face=Tahoma><SPAN style="color:#00007F">Sub</SPAN> AddSheet()
<SPAN style="color:#00007F">Dim</SPAN> NewSheet <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>

<SPAN style="color:#00007F">If</SPAN> 1 + 1 = 2 <SPAN style="color:#00007F">Then</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> NewSheet = Worksheets.Add
    NewSheet.Name = "This is the New Sheet"
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

If you need to, this can also be set up so that the name of the sheet is determined by user input instead of 'static' in the code as it currently is.
 
Upvote 0
Something along these lines (note no testing for invalid characters):

<font face=Tahoma><SPAN style="color:#00007F">Sub</SPAN> NewSheet()
    <SPAN style="color:#00007F">Dim</SPAN> strShtName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
        strShtName = InputBox("What do you want to name me?", "New Sheet Name")
        
        Worksheets.Add
        ActiveSheet.Name = strShtName
      
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

Hope that helps,

Smitty
 
Upvote 0
Hi,

Here are a couple of choices (BTW - no need for the big, bold writing - it just annoys people!)
Code:
Sub Macro1()
    Worksheets.Add
    ActiveSheet.Name = "Bob"
End Sub

Sub Macro2()
    Dim ws As Worksheet
    Set ws = Worksheets.Add
    ws.Name = "Bob2"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,834
Members
449,051
Latest member
excelquestion515

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