2016-07-20 57 views
0

我想運行一個宏來格式化每個包含我當前文件夾中不同工作表的每個excel文件。這個宏會將每個文件中每個工作表的頁面設置設置爲1頁寬,1頁高的頁面設置,將頁面方向設置爲橫向,然後保存。使用宏在一個文件夾中格式化多個excel文件

我已經記錄了宏1號的第一個工作表excel文件,它看起來像這樣:

Application.PrintCommunication = False 
    With ActiveSheet.PageSetup 
     .PrintTitleRows = "" 
     .PrintTitleColumns = "" 
    End With 
    Application.PrintCommunication = True 
    ActiveSheet.PageSetup.PrintArea = "" 
    Application.PrintCommunication = False 
    With ActiveSheet.PageSetup 
     .LeftHeader = "" 
     .CenterHeader = "" 
     .RightHeader = "" 
     .LeftMargin = Application.InchesToPoints(0.08) 
     .RightMargin = Application.InchesToPoints(0.08) 
     .TopMargin = Application.InchesToPoints(1) 
     .BottomMargin = Application.InchesToPoints(1) 
     .HeaderMargin = Application.InchesToPoints(0.5) 
     .FooterMargin = Application.InchesToPoints(0.5) 
     .PrintHeadings = False 
     .PrintGridlines = False 
     .PrintComments = xlPrintNoComments 
     .CenterHorizontally = False 
     .CenterVertically = False 
     .Orientation = xlLandscape 
     .Draft = False 
     .PaperSize = xlPaperLetter 
     .FirstPageNumber = xlAutomatic 
     .Order = xlDownThenOver 
     .BlackAndWhite = False 
     .Zoom = False 
     .FitToPagesWide = 1 
     .FitToPagesTall = 1 
     .PrintErrors = xlPrintErrorsDisplayed 
     .OddAndEvenPagesHeaderFooter = False 
     .DifferentFirstPageHeaderFooter = False 
     .ScaleWithDocHeaderFooter = True 
     .AlignMarginsHeaderFooter = True 
     .FirstPage.RightHeader.Text = "" 
     .FirstPage.LeftFooter.Text = "" 
     .FirstPage.CenterFooter.Text = "" 
     .FirstPage.RightFooter.Text = "" 
    End With 
    Application.PrintCommunication = True 
    ActiveWorkbook.Save 
End Sub 

請幫我修改這個代碼,以便它可以在每個接口的所有工作表的Excel工作簿中我的文件夾。 謝謝

+2

你應該尋找「通過關閉工作簿如何循環和更改格式」或類似的東西(我只是在那個搜索查詢猜測)。然後嘗試一下,如果你有問題,請求我們幫助解決問題。這不是一門編碼服務,而是一門教學和故障排除工作。 :-) – Rodger

+1

@ Rodger's就可以了 - 簡單地說,看看如何遍歷工作簿中的所有工作表(這幾乎是三行),並且還[避免使用'.Select' /'.Activate](https:// stackoverflow .com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros)(具體來說,查找顯示如何設置Worksheet變量的答案)。謝天謝地,你不必過多地調整你的代碼,AFAICT。 – BruceWayne

+0

請不要對Excel/VBA問題使用[宏]標記。它的標籤信息說:*不適用於MS-OFFICE/VBA /宏語言。改爲使用相應的標籤。*' –

回答

0
Sub ProcessFiles() 
    Dim FolderPath As String 
    FolderPath = "Your Folder Path Goes Here" 
    ToggleEvents False 

    Dim FileName As String, xlFilename As String 
    Dim xlWB As Excel.Workbook, xlWS As Excel.Worksheet 

    If Not Right(FolderPath, 1) Then FolderPath = FolderPath & "\" 

    FileName = Dir(FolderPath, vbDirectory) 

    Do While FileName <> "" 
     xlFilename = FolderPath & FileName 
     Set xlWB = Workbooks.Open(xlFilename) 
     For Each xlWS In xlWB.Worksheets 
      ModifyPageSetup xlWS 
     Next 

     xlWB.Close True 

    Loop 

    Application.PrintCommunication = True 
    ToggleEvents True 
End Sub 

Sub ToggleEvents(EnableEvents As Boolean) 

    With Application 
     .EnableEvents = EnableEvents 
     .Calculation = IIf(EnableEvents, xlCalculationAutomatic, xlCalculationManual) 
     .ScreenUpdating = EnableEvents 
    End With 

End Sub 

Sub ModifyPageSetup(xlWS As Excel.Worksheet) 

    With xlWS.PageSetup 
     .PrintTitleRows = "" 
     .PrintTitleColumns = "" 
     Application.PrintCommunication = True 
     .PageSetup.PrintArea = "" 
     Application.PrintCommunication = False 
    End With 


    With xlWS.PageSetup 
     .LeftHeader = "" 
     .CenterHeader = "" 
     .RightHeader = "" 
     .LeftMargin = Application.InchesToPoints(0.08) 
     .RightMargin = Application.InchesToPoints(0.08) 
     .TopMargin = Application.InchesToPoints(1) 
     .BottomMargin = Application.InchesToPoints(1) 
     .HeaderMargin = Application.InchesToPoints(0.5) 
     .FooterMargin = Application.InchesToPoints(0.5) 
     .PrintHeadings = False 
     .PrintGridlines = False 
     .PrintComments = xlPrintNoComments 
     .CenterHorizontally = False 
     .CenterVertically = False 
     .Orientation = xlLandscape 
     .Draft = False 
     .PaperSize = xlPaperLetter 
     .FirstPageNumber = xlAutomatic 
     .Order = xlDownThenOver 
     .BlackAndWhite = False 
     .Zoom = False 
     .FitToPagesWide = 1 
     .FitToPagesTall = 1 
     .PrintErrors = xlPrintErrorsDisplayed 
     .OddAndEvenPagesHeaderFooter = False 
     .DifferentFirstPageHeaderFooter = False 
     .ScaleWithDocHeaderFooter = True 
     .AlignMarginsHeaderFooter = True 
     .FirstPage.RightHeader.Text = "" 
     .FirstPage.LeftFooter.Text = "" 
     .FirstPage.CenterFooter.Text = "" 
     .FirstPage.RightFooter.Text = "" 
    End With 

End Sub 
+0

我應該在哪裏輸入上述代碼中的文件夾路徑? – Karan

+0

我更新了我的答案,以清楚說明放置文件夾路徑的位置。 –

相關問題