带有一个命名工作表的 Excel 宏 VBA 新工作簿

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42916763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:19:31  来源:igfitidea点击:

Excel macro VBA new workbook with one named sheet

vbaexcel-vbaexcel

提问by Anne

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?

我需要一个宏才能创建一个只有一个名为“Options”的工作表的新工作簿,并且以下工作表必须以“Sheet1”而不是“Sheet2”开头。是否可以?

采纳答案by Tom

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

回答by Shai Rado

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

下面的代码将添加一个新Workbook的只有 1 个名为“选项”的工作表。

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"

但是,关于第二部分,为了从“Sheet1”而不是“Sheet2”开始,它涉及向新创建的工作簿添加代码,因为默认情况下新工作簿已经有 1 个名为“选项”的工作表,所以实际上你是添加第二个工作表,因此 Excel 会自动将其命名为“Sheet2”

Option Explicit

Sub CreateNewWB()

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

End Sub