Private Sub CommandButton1_Click()
Dim app As Excel.Application
Dim xlapp As Variant
Dim xlworkbooks As Workbooks
Dim MyFile, MyPath, MyName
MyPath = "d:\temp"
MyName = Dir(MyPath & "\" & "*.xls")
Do While MyName <> "" @# 开始循环。
Set xlapp = GetObject(MyPath & "\" & MyName)
Set w_sheets = xlapp.Worksheets
xlapp.Application.DisplayAlerts = False @#不显示警告
xlapp.Worksheets("sheet2").Delete
Workbooks(MyName).Save
xlapp.Application.DisplayAlerts = True
xlapp.Application.Visible = True
Workbooks(MyName).Close
MyName = Dir @# 查找下一个目录。
Loop
End Sub
后来,再和高手探讨调试,发现如果不用getobject,而是直接先用workbook.open就可达到正常。
Application.Workbooks.Open MyName @#此是关键,先打开工作表
反正是为什么会出现上面的情形我也说不清楚,不过能解决问题就好。。。呵呵
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/