Shortcut to show/hide certain columns?

slam

Well-known Member
Joined
Sep 16, 2002
Messages
873
Office Version
  1. 365
  2. 2019
I thought my answer to this was going to be Grouping, a feature I've never used before, but I get the message "This can't be done on a multiple range selection", so I need an alternative solution.

I want columns A, B, C, D to be shown at all times. Then I need functionality to show all columns, or only certain columns (in additional to A, B, C, D). Specifically, it would be each 7th column starting with E. So, for instance, a button to show columns E, L, S, Z, AG, AN, another button to show F, M, T, AA, AH, AO. And so on...

At the moment, my data extends to column AT, but this data will extend by 7 columns per month, so it would be good to set it up to dynamically capture the additional columns from the start.

Is there any built in functionality for this, or is a macro the solution?

Thanks
 
Add to above: to adapt for the other buttons you change the number 5 (for E). To begin with F, you'd use 6. Rather than repeat this code 7 times, I'd pass a value from each button. For the button that starts with E, pass 5. For F, pass 6; for G pass 7; etc. I'll post an example of that in a bit.
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
VBA Code:
Sub Button7_Click() ' sub for 1 button; starts at E
HideCols 5
End Sub

Sub Button8_Click() 'sub for button that starts at F
HideCols 6
End Sub

'etc.

Sub HideCols(intCol As Integer)
Dim i As Integer

For i = 0 To 42 Step 7
    Sheets("Sheet4").Columns(intCol + i).Hidden = True
Next

End Sub
I suppose it is possible to not have 7 subs for 7 buttons but I'd rather not get into that. ;)
 
Upvote 0
Are they sometimes hidden? That's what's not clear to me. You forgot to answer this?

I think I answered it in my first post, and again in my last post :)

I want columns A, B, C, D to be shown at all times

I never want Columns A to D to be hidden.

What I still needed was the code to hide all the other columns besides A to D. I maybe did this a strange way, but I just added a couple of lines to hide all the other columns before only showing the Phase columns.

However, I believe I have got this working, and thank you for pointing me in the right direction. This is what I now have, and it works. I've tested it for the first two buttons, with the only change being the name, and incrementing the n = x

VBA Code:
Sub Phase()
Dim i As Integer, n As Integer
ActiveSheet.Range("E:DF").Select
Selection.EntireColumn.Hidden = True
n = 5
For i = 0 To 110 Step 7
Sheets("Unique").Columns(n + i).Hidden = False
Next
Range("A1").Select
End Sub
 
Upvote 0
For me, neither of those two statements specifies that they may or will never be, hidden. Those statements imply that it is possible. At least that's my way of looking at it. Glad you were able to solve it.
FWIW, I have seen replies from those with more experience than me say that most of the time, selections are not required in order to make things happen, and doing so can trigger other sheet events at worse and slow things down at least.
 
Upvote 0
FWIW, I have seen replies from those with more experience than me say that most of the time, selections are not required in order to make things happen, and doing so can trigger other sheet events at worse and slow things down at least.

Meaning this line? What is the alternative?

VBA Code:
ActiveSheet.Range("E:DF").Select
 
Upvote 0
something like
Sheets("Sheet4").Range("E:DF").EntireColumn.Hidden = True
or to use your code

ActiveSheet.Range("E:DF").EntireColumn.Hidden = True
 
Upvote 1

Forum statistics

Threads
1,215,201
Messages
6,123,621
Members
449,109
Latest member
Sebas8956

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