【问题标题】:VBA: Auto save&close out of all current workbooks except for Macro workbookVBA:自动保存和关闭除宏工作簿之外的所有当前工作簿
【发布时间】:2015-01-30 22:37:43
【问题描述】:

我正在尝试关闭所有当前打开的工作簿,但我的宏工作簿和 .SaveAs 我的路径除外,但我希望我的路径是我的宏工作簿 [D1] 中的指定单元格。我还希望将文件名保存为我当前正在保存和关闭的工作簿中的单元格 A1。现在我被困住了。我已经列出了我当前正在使用的代码,而我在使用这段代码时遇到的问题是,它在当前选定的工作簿与代码当前循环的工作簿中保存为单元格 A1 中的名称.我希望这是有道理的。

            Option Explicit
            Public ThisFile As String
            Public Path As String

            Sub CloseAndSaveOpenWorkbooks()
                Dim Wkb As Workbook
                ' ThisFile = ActiveWorkbook.Sheets(1).Range("A1").Value ** Commented out as this piece of code was not working as intended **
                Path = "C:\Users\uuis\Desktop"

                With Application
                    .ScreenUpdating = False

                     '       Loop through the workbooks collection
                    For Each Wkb In Workbooks

                        With Wkb

                            If .Name <> ThisWorkbook.Name Then
                             '               if the book is read-only
                             '               don't save but close
                            If Not Wkb.ReadOnly Then

                                .SaveAs Filename:=(Path & "\" & ActiveWorkbook.Sheets(1).Range("A1").Value & ".xls"), FileFormat:=xlExcel8

                            End If

                             '               We save this workbook, but we don't close it
                             '               because we will quit Excel at the end,
                             '               Closing here leaves the app running, but no books

                                .Close

                            End If

                        End With

                    Next Wkb


                    .ScreenUpdating = True
                    ' .Quit 'Quit Excel
                End With
            End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:
    ActiveWorkbook.Sheets(1).Range("A1").Value
    

    应该是

    Wkb.Sheets(1).Range("A1").Value
    

    【讨论】:

      猜你喜欢
      • 2018-11-07
      • 2014-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多