我有一个excel“Closed.Xls”,目前尚未打开。
请告诉我如何在此已关闭的Excel文件中添加新工作表。我知道如何在当前的Excel中添加新工作表。
Dim WS as Worksheet
Set WS = Sheets.Add
请告诉我如何
注意:我不想重命名工作表。
由于
答案 0 :(得分:9)
这样的东西会做你想要的。如果不打开工作簿是不可能的,但是如果关闭屏幕更新,它会给出不打开的外观。
Option Explicit
Sub Add_Sheet_ClosedBook()
Dim bk As Workbook
Dim sh As Worksheet
Dim shName As String
With Application
.ScreenUpdating = False
.DisplayAlerts = False
Set bk = .Workbooks.Open _
("Path to Book.xls")
End With
With bk
Set sh = .Sheets.Add
shName = sh.Name
.Save
.Close
End With
With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
答案 1 :(得分:6)
要获取工作表名称,请使用worksheet
变量,即
Sub Added()
Dim Wb As Workbook
Dim ws As Worksheet
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
End With
Set Wb = Workbooks.Open("c:\Temp\closed.xls")
Set ws = Wb.Sheets.Add
Debug.Print ws.Name
Wb.Save
Wb.Close
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
End Sub
答案 2 :(得分:3)
可以使用Microsoft.ACE.OLEDB.12.0提供程序在不打开工作簿的情况下添加工作表:
set cn = new adodb.connection
with cn
.provider = "Microsoft.ACE.OLEDB.12.0"
.connectionstring = "Data Source=" & strSomeFilename & ";Extended Properties=""Excel12.0;"""
.open
end with
set cmd = new adodb.command
cmd.activeconnection = cn
cmd.commandtext = "CREATE TABLE MySheet (ID char(255))"
cmd.execute
这将向新工作表的单元格A1添加标题“ID”。如有必要,您可以找到删除/更改它的方法。