创建新的Excel工作簿,如下所示:
Dim xl As Excel.Application
Dim wb As Excel.Workbook
Set xl = New Excel.Application
xl.Visible = False
Set wb = xl.Workbooks.Add
是否有一种简单的方法可以停止Excel自动创建Sheet1,Sheet2,Sheet3?
我可以随后删除这些不需要的纸张,但这感觉就像一个笨重的解决方案。
答案 0 :(得分:11)
xl.SheetsInNewWorkbook = 1
有关MSDN的更多信息(向下滚动到Add method as it applies to the Workbooks object.
)
完整代码:
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'
答案 1 :(得分:3)
或者你可以:
Excel 2003 工具>选项>常规标签,并将“新工作簿中的表格”更改为1
Excel 2007 Office按钮> Excel选项>热门部分>创建新工作簿时...>包含这么多工作表> 1
答案 2 :(得分:3)
无法创建一个没有任何工作表(据我所知),但您可以使用单个工作表创建工作簿,而无需弄乱用户的设置。
dim wb as Workbook
Set wb = xl.Workbooks.Add(xlWBATWorksheet)
答案 3 :(得分:0)
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