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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Von Pookie

MrExcel MVP
Joined
Feb 17, 2002
Messages
13,686
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.
 

Cbrine

Well-known Member
Joined
Dec 2, 2003
Messages
3,196
application.worksheets.add
activesheet.name = "NewSheet"
 

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
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
 

Richie(UK)

MrExcel MVP
Joined
May 17, 2002
Messages
3,329
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
 

Watch MrExcel Video

Forum statistics

Threads
1,129,349
Messages
5,635,774
Members
416,880
Latest member
kyleswanson

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
Top