2014-06-11 107 views
0

我試圖關閉當前打開的所有工作簿,除了我的宏工作簿和.SaveAs我的路徑,但我希望我的路徑是我的指定單元格宏觀工作手冊[D1]要精確。我也希望將文件名保存爲工作簿中的單元格A1,我正在保存並關閉。現在我卡住了。我列出了目前我正在使用的代碼,我遇到的這段代碼的問題在於,它將當前選定的工作簿與代碼當前正在循環的工作簿中的名稱保存爲單元格A1中的名稱。我希望這是有道理的。VBA:自動保存並關閉除宏工作簿以外的所有當前工作簿

  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 

回答

0
ActiveWorkbook.Sheets(1).Range("A1").Value 

應該

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

哈!謝謝蒂姆。非常感謝快速回復! – new2hacking

相關問題