自动保存&关闭工作簿,VBAWorkbooks除外

时间:2014-06-18 02:09:48

标签: excel vba excel-vba

目前正试图弄清楚如何让我的代码自动保存并关闭打开的工作簿以避免vba项目工作簿,而无需命名vba项目工作簿。有没有办法让你的代码识别vba工作簿与我试图保存和关闭的其他开放工作簿?

Option Explicit
Public ThisFile As String
Public Path As String

Sub CloseAndSaveOpenWorkbooks()
    Dim Wkb As Workbook
    Path = [D1]

    With Application
        .ScreenUpdating = False

        'Loop through the workbooks collection
        For Each Wkb In Workbooks

            With Wkb
            'If NOT on Macro workbook then

                If .Name <> ThisWorkbook.Name Then
                'If the book is read-only
                'don't save but close
                    If Not Wkb.ReadOnly Then
                    'Save current workbook with current workbooks cell A1 as file name
                        .SaveAs Filename:=(Path & "\" & Wkb.Sheets(1).Range("A1").Value & ".xls"), FileFormat:=xlExcel8

                    End If

                    'Closing here leaves the app running, but no books

                    .Close

                End If

            End With

        Next Wkb

        .ScreenUpdating = True

    End With
End Sub

线索的后续问题:VBA: Auto save&close out of all current workbooks except for Macro workbook

1 个答案:

答案 0 :(得分:0)

得到了我的答案!

阅读Article Provided by J_V绝对有帮助。 我换了

If .Name <> ThisWorkbook.Name Then

If Wkb.HasVBProject = False Then

希望这最终会帮助别人。再次感谢J_V!