VBA Hide/Unhide Columns, different/multiple sheets

Minx_Minxy

New Member
Joined
Jul 13, 2015
Messages
37
Office Version
  1. 365
Platform
  1. Windows
I have two sheets, "Operation" and "EHSQ", I want to be able to hide and unhide certain columns on these sheets, I am thinking of using a a shape and assign the macro where the user can press, like Q1, Q2, Q3 etc. So the user hits Q1, if the columns are unhidden, then the macro hides it and vice versa.

The columns on each sheets are different:

I need the macro to:

1) make it work to hide and unhide when user hit the shape with the macro assigned
2) make it work not only in the Operations sheet, but for EHSQ as well

This is what I got so far

Code for Operations
VBA Code:
Sub Q1_Hide
  Columns("H:J").Select
    Selection.EntireColumn.Hidden = True
End Sub

Sub Q2_Hide
Columns("K:M").Select
    Selection.EntireColumn.Hidden = True
End Sub

Sub Q3_Hide
Columns("N:P").Select
    Selection.EntireColumn.Hidden = True
End Sub

Sub Q4_Hide
Columns("Q:S").Select
    Selection.EntireColumn.Hidden = True
End Sub
 
SubQ1_UnHide
  Columns("H:J").Select
    Selection.EntireColumn.Hidden = False
End Sub

SubQ2_UnHide
Columns("K:M").Select
    Selection.EntireColumn.Hidden = False
End Sub

SubQ3_UnHide
Columns("N:P").Select
    Selection.EntireColumn.Hidden = False
End Sub

SubQ4_Unhide
Columns("Q:S").Select
    Selection.EntireColumn.Hidden = False
End Sub


This one is for EHSQ:

VBA Code:
SubQ1_Hide
  Columns("G:I").Select
    Selection.EntireColumn.Hidden = True
End Sub

SubQ2_Hide
Columns("J:L").Select
    Selection.EntireColumn.Hidden = True
End Sub

SubQ3_Hide
Columns("M:O").Select
    Selection.EntireColumn.Hidden = True
End Sub

SubQ4_Hide
Columns("P:R").Select
    Selection.EntireColumn.Hidden = True
End Sub
 
SubQ1_UnHide
  Columns("G:I").Select
    Selection.EntireColumn.Hidden = False
End Sub

SubQ2_UnHide
Columns("J:L").Select
    Selection.EntireColumn.Hidden = False
End Sub

SubQ3_UnHide
Columns("M:O").Select
    Selection.EntireColumn.Hidden = False
End Sub

SubQ4_Unhide
Columns("P:R").Select
    Selection.EntireColumn.Hidden = False
End Sub
 
Last edited by a moderator:
You appear to have some form of corruption with that workbook.

I copied one of the sheets to a new workbook & ran the code with no issues - if error persist for you then may be worthwhile you doing the same for your project.

I did ask that you place the four shapes in column B (as shown in image) before running the code - once this is complete, you can move the shapes to their required place.

View attachment 118718

I am definitely struggling with this one and I cannot create the file from scratch :cry:

I did a work around instead. I created separate subs for each shape, so 8 in total and assigned the macros to each shape. The columns are hiding and unhiding with no issues so far 🤞

here is the code taht I used

VBA Code:
Sub OperationsQ1()

Dim OperationsQ1 As Range
Set OperationsQ1 = ThisWorkbook.Worksheets("Operations").Range("H:J") 'change range

If OperationsQ1.EntireColumn.Hidden = False Then
     OperationsQ1.EntireColumn.Hidden = True
ElseIf OperationsQ1.EntireColumn.Hidden = True Then
    OperationsQ1.EntireColumn.Hidden = False
End If

End Sub



Thank you so much for helping me on this Dave 👉👈 your code definitely works, but as you said my original file is corrupted.
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,224,812
Messages
6,181,098
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