如題,代碼如下,執行後3個sheet原封不動的在那兒並未成功刪除掉第一個sheet,
求助,幫忙尋找解決方法
'?獲得Sheet的值.查看獲得Sheet值得方法
'
'=========================================
'myString = "CreateExcel"
Dim oExcel,oBook,oSheet
Dim filePath
Dim getValue,getValue2
Dim fso,isExist,tempFolder
filePath = "C:\Users\L.H\Desktop\VBS\excel.xlsx"
'判斷即將保存的文件是否已經存在於該目錄,若存在,則刪除該文件
Set fso = CreateObject("Scripting.FileSystemObject")
isExist = fso.FileExists(filePath)
If isExist Then
fso.DeleteFile(filePath)
End If
'建立新的Excel引用程序
Set oExcel = CreateObject("Excel.Application")
oExcel.caption="Test"
'建立新的工作空間
Set oBook = oExcel.Workbooks.add
'默認就已經建立了一個Sheet
'添加新的表
oBook.WorkSheets.add
oBook.WorkSheets.add
'選中第一個表
Set oSheet = oBook.WorkSheets.Item(1)
oSheet.Name = "MySheetofTest"
oSheet.Cells(1,1).value = "DataInThere"
Set oSheet = oBook.WorkSheets.Item(2)
oSheet.Name = "MySheetofTest2"
oSheet.Cells(1,2).value = "DataInThere2"
Set oSheet = oBook.WorkSheets.Item(3)
oSheet.Name = "MySheetofTest3"
oSheet.Cells(1,3).value = "DataInThere3"
'TODO:刪除第一張表'
oBook.WorkSheets(1).Delete
oBook.SaveAs(filePath)
'獲得已選中的表的第一行一列單元格的值
Set oSheet = oBook.WorkSheets.Item(1)
getValue = oSheet.Cells(1,1).Value
Set oSheet = oBook.WorkSheets.Item(2)
getValue2 = oSheet.Cells(1,2).Value
oBook.CLOSE
oExcel.quit
'釋放內存
Set oSheet = Nothing
Set oBook = Nothing
Set oExcel = Nothing
MsgBox(getValue&"+"&getValue2)
執行了一下,沒問題。
你在cmd下執行一下看看,是不是報錯了。