用VB向Excel前插入工作表没错但在后面插入工作表就出错(代码在贴内)
Option ExplicitDim VBExcel As excel.Application
Dim VBExcel_Book As excel.Workbook
Dim VBExcel_sheet As excel.Worksheet
Dim MyFile As Object
Dim FileName As String
Private Sub Command1_Click()
Dim sht2285 As Boolean, i As Integer
sht2285 = False
For i = 1 To VBExcel.Sheets.Count
If VBExcel.Sheets(i).Name = "插入" Then sht2285 = True
Next
If Not sht2285 Then
【代码1能通过编译,但运行时出错,请高手指点指点】VBExcel.Sheets.Add after:=Sheets(Sheets.Count) '在工作表后加入一个工作表
【代码2能够顺利运行】VBExcel.Sheets.Add '在工作表前加入一个工作表
VBExcel.ActiveSheet.Name = "插入"
End If
End Sub
Private Sub Command2_Click() '修改指定工作表名按扭
VBExcel_Book.Sheets("Sheet2").Name = "修改工作表名"
End Sub
Private Sub Form_Load()
Set MyFile = CreateObject("Scripting.FileSystemObject")
Set VBExcel = CreateObject("Excel.Application")
FileName = App.Path & "\新创建.XLS"
If MyFile.FileExists(FileName) = False Then
With VBExcel
.Workbooks.Add
With ActiveWorkbook
.SaveAs FileName
.Close
End With
.Quit
End With
End If
'设置Excel为不可见
VBExcel.Visible = False
Set VBExcel_Book = VBExcel.Workbooks.Open(FileName)
End Sub
Private Sub Form_Unload(Cancel As Integer)
'关闭Excel文件
Set VBExcel_sheet = Nothing
VBExcel.ActiveWorkbook.Close savechanges:=True '保存对EXCELL进行更改。
Set VBExcel_Book = Nothing
VBExcel.Quit
Set VBExcel = Nothing
End Sub