处理完成后,Excelmacros不closuresCSV文件
我正在运行下面的脚本,它看起来像它不closures和脚本运行时的文件。 如何编辑脚本以便能够closures脚本。
Sub LoopFiles() Dim MyFileName As String, MyPath As String Application.ScreenUpdating = False Application.DisplayAlerts = False MyPath = "C:\macro\" MyFileName = Dir(MyPath & "*.xlsx") ChDir "C:\macro\csv" Do Until MyFileName = "" Workbooks.Open Filename:=MyPath & MyFileName ActiveWorkbook.SaveAs Filename:=Left(MyFileName, InStr(1, MyFileName, ".xlsx") - 1), FileFormat:=xlCSV, CreateBackup:=False MyFileName = Dir Loop Application.ScreenUpdating = True Application.DisplayAlerts = True End Sub
添加以下行: ActiveWorkbook.Close False
在我已经在下面演示的空间。
Do Until MyFileName = "" Workbooks.Open Filename:=MyPath & MyFileName ActiveWorkbook.SaveAs Filename:=Left(MyFileName, InStr(1, MyFileName, ".xlsx") - 1), FileFormat:=xlCSV, CreateBackup:=False ActiveWorkbook.Close False MyFileName = Dir Loop
或更清洁的版本做到这一点:
Dim wkb as Workbook Do Until MyFileName = "" Set wkb = Workbooks.Open(Filename:=MyPath & MyFileName) With wkb .SaveAs Filename:=Left(MyFileName, InStr(1, MyFileName, ".xlsx") - 1), FileFormat:=xlCSV, CreateBackup:=False .Close False End With Loop