简体   繁体   English

循环浏览文件夹中的文件

[英]Loop through files in a folder

I want to format each Excel file containing different worksheets in my current folder.我想格式化当前文件夹中包含不同工作表的每个 Excel 文件。

This code will set the page setup for each worksheet in each file to a scale of 1 page wide by 1 page tall, set page orientation to landscape and then save it.此代码将每个文件中每个工作表的页面设置设置为 1 页宽 x 1 页高,将页面方向设置为横向,然后保存。

I recorded the code for the first worksheet of the first excel file:我记录了第一个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

How do I edit this code so that it works on all the worksheets of each Excel workbook in my folder?如何编辑此代码以使其适用于我文件夹中每个 Excel 工作簿的所有工作表?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM