Excel宏关闭所有打开的工作簿

时间:2017-05-17 07:28:22

标签: excel excel-vba vba

我有像

这样的宏代码
 Do Until MyFileName = ""
    Workbooks.Open Filename:=MyPath & MyFileName
    ActiveWorkbook.SaveAs Filename:=Left(MyFileName, InStr(1, MyFileName, ".xls") - 1), FileFormat:=xlCSV, CreateBackup:=False
    ActiveWorkbooks.CloseSaveChanges:=TRUE
    MyFileName = Dir
Loop

它工作正常但在转换为csv时文件正在打开。我想关闭文件并保存更改是真的。请帮我。

1 个答案:

答案 0 :(得分:0)

将新名称保存在变量中并使用Workbook("name").close(true)

Dim wbName As String

Do Until MyFileName = ""
    Workbooks.Open Filename:=MyPath & MyFileName
    wbName = Left(MyFileName, InStr(1, MyFileName, ".xls") - 1)

    ActiveWorkbook.SaveAs Filename:=wbName, FileFormat:=xlCSV, CreateBackup:=False
    Workbooks(wbName).Close (True)
    ActiveWorkbooks.Close SaveChanges:=True
    MyFileName = Dir
Loop
End Sub