I have a sheet called "Summary"
I have tried to write code to hide Cols C:E if not hidden at the begiining of my code before settting page breaks and to view the page
It would be appreciated if someone could kindly amend my code
I have tried to write code to hide Cols C:E if not hidden at the begiining of my code before settting page breaks and to view the page
Code:
Sub Print_Preview()
Dim lr As Long
Dim rngCell As Range
Dim hideColumns As Boolean
With Sheets("Summary")
lr = .Cells(.Rows.Count, "A").End(xlUp).Row
hideColumns = False ' Assume columns are not hidden
For Each rngCell In .Range("C1:E1")
If rngCell.EntireColumn.Hidden = True Then
hideColumns = True ' Columns are not hidden
Exit For
End If
Next rngCell
If hideColumns Then
.Range("C:E").EntireColumn.Hidden = True ' Hide columns C to E
End If
With .PageSetup
.PrintGridlines = True
.PrintArea = "A2:AC" & lr + 5
.PrintTitleRows = "$1:$1"
.PrintTitleColumns = "A:B"
.LeftHeader = "&D&T"
.CenterHeader = "SVC and Parts Turnover"
.Orientation = xlLandscape
.FitToPagesWide = 1
End With
.UsedRange.SpecialCells(xlCellTypeVisible).EntireColumn.AutoFit
.Activate ' Activate the sheet before the page preview
ActiveWindow.View = xlPageBreakPreview
.UsedRange.EntireColumn.AutoFit
End With
End Sub
It would be appreciated if someone could kindly amend my code