VBA Code to Bulk Edit Multiple Files in a Folder Structure Page Layouts to 1 page

westlabanker

New Member
Joined
Aug 31, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello,

I would like some assistance with a VBA code that can edit all the excel files in a folder structure to change the page layouts for all the sheets to 1 pages wide and tall. I have been trying a couple different options and cant seem to find one that works effectively.

Thank you in advance and I will be quick with my questions/comments!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try this macro - I would test it on a subset of a copy of your folder structure. As written, the macro changes the page layouts of all sheets in all workbook files matching "*.xlsx" in the folder tree starting at the specified folder.

I found that disabling communication with the printer, Application.PrintCommunication = False, is necessary for every sheet, otherwise the page layout change doesn't persist in the saved workbook.

VBA Code:
Option Explicit

Public Sub Change_Page_Layouts_All_Workbooks()
        
    Application.ScreenUpdating = False
    Change_Page_Layouts_Workbooks_In_Folders "C:\path\to\folder", "*.xlsx"
    Application.ScreenUpdating = True
    MsgBox "Done"
    
End Sub


Private Sub Change_Page_Layouts_Workbooks_In_Folders(folderPath As String, matchFiles As String)
   
    Static FSO As Object
    Dim Folder As Object, Subfolder As Object, File As Object
    Dim wb As Workbook, ws As Worksheet
    
    If FSO Is Nothing Then Set FSO = CreateObject("Scripting.FileSystemObject")

    'Process matching files in this folder
    
    Set Folder = FSO.GetFolder(folderPath)
    For Each File In Folder.Files
        If LCase(File.Name) Like LCase(matchFiles) Then
            Set wb = Workbooks.Open(File.Path)
            For Each ws In wb.Worksheets
                'Must disable communication with the printer for each sheet, otherwise Page Layout change doesn't persist in saved workbook
                Application.PrintCommunication = False
                ws.PageSetup.FitToPagesWide = 1
                ws.PageSetup.FitToPagesTall = 1
                Application.PrintCommunication = True
            Next
            wb.Close SaveChanges:=True
        End If
    Next
    
    'Process matching files in subfolders
    
    For Each Subfolder In Folder.SubFolders
        Change_Page_Layouts_Workbooks_In_Folders Subfolder.Path, matchFiles
    Next
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,055
Messages
6,122,902
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top