vba 启动没有任何工作表的空 Excel 工作簿

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3342436/
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-11 11:56:43  来源:igfitidea点击:

Start empty Excel workbook without any worksheets

excel-vbaexcelvba

提问by hawbsl

Creating a new Excel workbook as in:

创建一个新的 Excel 工作簿,如下所示:

Dim xl As Excel.Application
Dim wb As Excel.Workbook
Set xl = New Excel.Application
xl.Visible = False
Set wb = xl.Workbooks.Add

Is there an easy way to stop Excel automatically creating Sheet1, Sheet2, Sheet3?

有没有一种简单的方法可以阻止 Excel 自动创建 Sheet1、Sheet2、Sheet3?

I can always delete these unwanted sheets afterwards but that feels like a clunky solution.

之后我可以随时删除这些不需要的工作表,但这感觉就像一个笨拙的解决方案。

回答by marg

xl.SheetsInNewWorkbook = 1

More Information on MSDN(Scroll down to Add method as it applies to the Workbooks object.)

有关MSDN 的更多信息(向下滚动到Add method as it applies to the Workbooks object.

Full Code:

完整代码:

Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim restoreSheetsInNewWorkbook As Long

Set xl =  New Excel.Application
restoreSheetsInNewWorkbook = xl.SheetsInNewWorkbook
xl.SheetsInNewWorkbook = 1

Set wb = xl.Workbooks.Add
xl.SheetsInNewWorkbook = restoreSheetsInNewWorkbook 'or just set it to 3'

回答by Macness

Or you can:

或者你可以:

Excel 2003 Tools>Options>General Tab and change the "Sheets in new workbook" to 1

Excel 2003 工具>选项>常规选项卡并将“新工作簿中的工作表”更改为1

Excel 2007 Office Button>Excel Options>Popular Section>When creating new workbooks...>Include this many sheets>1

Excel 2007 Office 按钮>Excel 选项>热门部分>创建新工作簿时...>包括这么多工作表>1

回答by RubberDuck

Can't create one without any sheets (to the best of my knowledge), but you can create a workbook with a single sheet without messing around with the user's settings.

不能在没有任何工作表的情况下创建一个(据我所知),但是您可以使用单个工作表创建一个工作簿,而不会弄乱用户的设置。

dim wb as Workbook
Set wb = xl.Workbooks.Add(xlWBATWorksheet)

回答by Gendrian Borromeo

Sub DeleteSheets()
Dim DeleteSheet As Variant
Dim ws              As Worksheet
DeleteSheet = Array("Sheet1", "Sheet2", "Sheet3")

Application.DisplayAlerts = False

For Each ws In Worksheets

   If IsInArray(ws.Name, DeleteSheet) Then ws.Delete

Next

End Sub


Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
Dim i As Integer
Dim Ret As Boolean
Ret = False
For i = LBound(arr) To UBound(arr)
    If VBA.UCase(stringToBeFound) = VBA.UCase(arr(i)) Then
       Ret = True
       Exit For
    End If
Next i
IsInArray = Ret
End Function