Macro to hide cols C:E is not hiddden

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,603
Office Version
  1. 2021
Platform
  1. Windows
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


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
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi
Try
VBA Code:
Sheets("Summary").Range("C1:E1").Columns.Hidden = True
 
Upvote 0
Glad we could help
And thank for the feedback
Be happy and safe
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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