使用宏格式化文件夹中的多个Excel文件

时间:2016-07-20 19:27:19

标签: excel vba excel-vba macros

我想运行一个宏来格式化当前文件夹中包含不同工作表的每个excel文件。此宏将每个文件中每个工作表的页面设置设置为1页宽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工作簿的所有工作表。 谢谢

1 个答案:

答案 0 :(得分: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