Macro to Autofit Cols Page Preview

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,603
Office Version
  1. 2021
Platform
  1. Windows
I have the following Code below to Set the Page Layout from Sheet BR1 South to Last Sheet

The code works well, except that I need t autofit the columns as they display ##### in Page Preview Mode

It would be appreciated if someone could amend my code

Code:
 Sub SetPageSetupFromBR1SouthToLastSheet()

Dim wb As Workbook

Dim ws As Worksheet

Dim StartProcessing As Boolean

Dim printArea As String

Dim col As Range



' Optimization settings

Application.ScreenUpdating = False

Application.EnableEvents = False

Application.DisplayAlerts = False

Application.Calculation = xlCalculationManual

Application.PrintCommunication = False ' Speeds up print-related tasks



' Set the workbook

Set wb = ThisWorkbook



' Initialize variables

StartProcessing = False

printArea = "A1:L40"



' Loop through sheets

For Each ws In wb.Sheets

' Start processing only from "B11 South."

If StartProcessing Or ws.Name = "BR1 South." Then

StartProcessing = True



' Only apply settings if A1 is not blank

If Not IsEmpty(ws.Range("A1").Value) Then

' Page setup adjustments

With ws.PageSetup

.Orientation = xlLandscape ' Landscape orientation

.printArea = printArea ' Set print area

.PrintGridlines = False ' Remove gridlines

.Zoom = False ' Use scaling

.FitToPagesWide = 1 ' Fit to one page wide

.FitToPagesTall = 1 ' Fit to one page tall

End With



' Autofit columns

ws.Columns.AutoFit



' Add buffer to column widths

For Each col In ws.UsedRange.Columns

col.ColumnWidth = col.ColumnWidth + 1 ' Add a small buffer

Next col

End If

End If

Next ws



' Restore application settings

Application.Calculation = xlCalculationAutomatic

Application.PrintCommunication = True

Application.ScreenUpdating = True

Application.EnableEvents = True

Application.DisplayAlerts = True



MsgBox "Page setup and column autofit (with buffer) applied from 'BR1 South.' to the last sheet.", vbInformation

End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try :

VBA Code:
ws.Cells.EntireColumn.AutoFit
 
Upvote 0
Thanks for the Reply and your input. The Col Width in normal view is perfect but on page Preview mode some cols display #####
 
Upvote 0
Not certain what is happening for you. Obviously the autofit should work regardless.
 
Upvote 0
Thanks for the Reply and your input. The Col Width in normal view is perfect but on page Preview mode some cols display #####
If you manually expand those columns, does it show the correct value?
What exactly is in these cells, hard-coded values or formulas?
If formulas, what do the formulas look like?
Lastly, what font type and font size are you using?
 
Upvote 0
Hi guys

I managed to sort out the code so that the columns are autofitted in Normal and Page Break Preview Mode

Macro a bit slow but otherwise works perfectly

Code:
 Sub SetPageSetupFromBR1SouthToLastSheet()

Dim wb As Workbook

Dim ws As Worksheet

Dim StartProcessing As Boolean

Dim printArea As String



' Optimization settings

Application.ScreenUpdating = False

Application.EnableEvents = False

Application.DisplayAlerts = False

Application.Calculation = xlCalculationManual

Application.PrintCommunication = False ' Speeds up print-related tasks



' Set the workbook

Set wb = ThisWorkbook



' Initialize variables

StartProcessing = False

printArea = "A1:L40"



' Loop through sheets

For Each ws In wb.Sheets

' Start processing only from "BR1South"

If StartProcessing Or ws.Name = "BR1South" Then

StartProcessing = True



' Only apply settings if A1 is not blank

If Not IsEmpty(ws.Range("A1").Value) Then

' Autofit columns in range A1:L40

With ws.Range("A1:L40").Columns

.AutoFit

.ColumnWidth = .ColumnWidth + 1 ' Add a buffer to all columns

End With



' Page setup adjustments (minimized)

With ws.PageSetup

If .Orientation <> xlLandscape Or .FitToPagesWide <> 1 Or .FitToPagesTall <> 1 Then

.Orientation = xlLandscape ' Landscape orientation

.printArea = printArea ' Set print area

.Zoom = False ' Use scaling

.FitToPagesWide = 1 ' Fit to one page wide

.FitToPagesTall = 1 ' Fit to one page tall

End If

End With

End If

End If

Next ws



' Restore application settings

Application.Calculation = xlCalculationAutomatic

Application.PrintCommunication = True

Application.ScreenUpdating = True

Application.EnableEvents = True

Application.DisplayAlerts = True



' Select Macro sheet and notify user

Sheets("Macro").Select

MsgBox "Macro Completed", vbInformation

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,804
Messages
6,181,061
Members
453,017
Latest member
rlundbulls23

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