Forum Discussion

Chan_Tze_Leong's avatar
Chan_Tze_Leong
Brass Contributor
Mar 28, 2022

View selected columns in Excel using VBA

I am considering of viewing the Q1 (Jan to Mar), Q2 (Jan to June), Q3 (Jan to Sep) and Q4 (Jan tro Dec) ufor Target and Actual months using VBA. The below does not work. Please advise


Cells.Select
Selection.EntireColumn.Hidden = False
Selection.EntireColumn.Hidden = False
Columns("I:Q").Select
Selection.EntireColumn.Hidden = True
Columns("V:AD").Select
Selection.EntireColumn.Hidden = True
End Sub
Sub view2()
'
' view2 Macro
'

'
Cells.Select
Selection.EntireColumn.Hidden = False
Columns("L:Q").Select
Selection.EntireColumn.Hidden = True
Columns("Y:AD").Select
Selection.EntireColumn.Hidden = True
End Sub

      • NikolinoDE's avatar
        NikolinoDE
        Gold Contributor

        Chan_Tze_Leong 

        Here is the VBA code, you just have to create a new button for each month and show/hide the columns accordingly.

         

        Option Explicit
        Private Sub CommandButton1_Click() ' To Show ONLY your Janaury Columns
        'Me.Unprotect ("yourpass")
        Application.ScreenUpdating = False
        With ActiveSheet
            .Range("E:AS").EntireColumn.Hidden = True  ' Displays the month of January ONLY
           
            .Range("F:F, S:S").EntireColumn.Hidden = False   'Always Unhides All your Columns from your previous action
        End With
        Application.ScreenUpdating = True
        'Me.Protect ("yourpass")
        End Sub
        
        

         

         

         Hope I was able to help you with this information.

         

        NikolinoDE

        I know I don't know anything (Socrates)

         

Resources