简体   繁体   中英

Excel macro VBA new workbook with one named sheet

I need a macro to able to create a new workbook with only one sheet named "Options" and the following worksheets must start with "Sheet1" not with "Sheet2". Is it possible?

The code below will add a new Workbook with just 1 sheet named "Options".

However, regarding the second part, in order to start from "Sheet1" instead of "Sheet2" it involves adding code to the new created Workbook, since by default the new workbook already has 1 worksheet named "Options", so in fact you are adding a 2nd worksheet, so Excel automatically names it "Sheet2"

Option Explicit

Sub CreateNewWB()

With Application
    .SheetsInNewWorkbook = 1
    .Workbooks.Add
    .Sheets(1).Name = "Options"
End With

End Sub

Think this is what you're looking for

This first creates a new workbook, then adds a new sheet at the beginning before renaming it to "Options". The rest of the sheets should then follow the default naming pattern

Option Explicit
Public Sub NewWorkbook()
    With Workbooks.Add
        With .Sheets.Add(Before:=.Sheets(1))
            .Name = "Options"
        End With
    End With
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM