Adding a new sheet in code

AccessShell

Board Regular
Joined
Sep 5, 2015
Messages
50
Office Version
  1. 2003 or older
Platform
  1. Windows
I ran "Record New Macro" to get code to add, move and rename a new sheet. The macro made a new sheet and automatically named it "Sheet1" I already had a sheet named "522 Details".

Then I deleted the sheet added by the code, and ran the code. Instead of naming the new sheet "Sheet1" it named it "Sheet2" This does not work well for me as I will need to run this macro several times. Can I adjust the macro code so the new sheet always has a default name before I can change the name for my needs?

VBA Code:
Sub AddASheet()
'
' AddASheet Macro
'
Dim strNewSheetName As String
'
    strNewSheetName = Format(Now, "mmyy") & " Details"
   
    Sheets("522 Details").Select
    Sheets.Add
    Sheets("Sheet1").Select
    Sheets("Sheet1").Move After:=Sheets(2)
    Sheets("Sheet1").Select
    Sheets("Sheet1").Name = strNewSheetName
   
End Sub

Thanks
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
The name of the new sheet is irrelevant, you can use
VBA Code:
Sub AddASheet()
'
' AddASheet Macro
'
Dim strNewSheetName As String
'
    strNewSheetName = Format(Now, "mmyy") & " Details"
   
    Sheets.Add(, Sheets(2)).Name = strNewSheetName
   
End Sub
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,157
Messages
6,123,341
Members
449,097
Latest member
thnirmitha

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