Hiding / Unhiding multiple named ranges at once

Fishboy

Well-known Member
Joined
Feb 13, 2015
Messages
4,267
Hi all,

I currently have an Excel 2010 workbook containing a number of macros to hide columns. Each column is for a specific location (arranged alphabetically) and has it's own hide button which basically hides / unhides all OTHER columns than the one with the button. For example:

Code:
Sub Location2()
Application.ScreenUpdating = False
Columns("C:C").Hidden = Not Columns("C:C").Hidden
Columns("E:AD").Hidden = Not Columns("E:AD").Hidden
End Sub

As you can see from the above the button in question is for column D, and it basically hides / unhides column C, then columns E through to AD. The next column has a button with this code:

Code:
Sub Location3()
Application.ScreenUpdating = False
Columns("C:D").Hidden = Not Columns("C:D").Hidden
Columns("F:AD").Hidden = Not Columns("F:AD").Hidden
End Sub

...which hides / unhides columns C and D, then columns F through to AD, and so on across the sheet.

This is all working fine, however I am trying to future proof the document to allow for additional columns being added. The new columns wont necessarily just be added to the end as the order is alphabetised. To account for this I would like to create named ranges for each location (column) so that when new columns are added, the macros still work without the column references needing to be updated.

Now, this is where I am having difficulty...

I would like to avoid having to make a load of mammoth macros like this:

Code:
Sub Location2()
Application.ScreenUpdating = False
Range("Location1").EntireColumn.Hidden = Not (Range("Location1").EntireColumn.Hidden)
Range("Location3").EntireColumn.Hidden = Not (Range("Location3").EntireColumn.Hidden)
Range("Location4").EntireColumn.Hidden = Not (Range("Location4").EntireColumn.Hidden)
Range("Location5").EntireColumn.Hidden = Not (Range("Location5").EntireColumn.Hidden)
Range("Location6").EntireColumn.Hidden = Not (Range("Location6").EntireColumn.Hidden)
Range("Location7").EntireColumn.Hidden = Not (Range("Location7").EntireColumn.Hidden)
Range("Location8").EntireColumn.Hidden = Not (Range("Location8").EntireColumn.Hidden)
Range("Location9").EntireColumn.Hidden = Not (Range("Location9").EntireColumn.Hidden)
Range("Location10").EntireColumn.Hidden = Not (Range("Location10").EntireColumn.Hidden)
Range("Location11").EntireColumn.Hidden = Not (Range("Location11").EntireColumn.Hidden)
Range("Location12").EntireColumn.Hidden = Not (Range("Location12").EntireColumn.Hidden)
Range("Location13").EntireColumn.Hidden = Not (Range("Location13").EntireColumn.Hidden)
Range("Location14").EntireColumn.Hidden = Not (Range("Location14").EntireColumn.Hidden)
Range("Location15").EntireColumn.Hidden = Not (Range("Location15").EntireColumn.Hidden)
Range("Location16").EntireColumn.Hidden = Not (Range("Location16").EntireColumn.Hidden)
Range("Location17").EntireColumn.Hidden = Not (Range("Location17").EntireColumn.Hidden)
Range("Location18").EntireColumn.Hidden = Not (Range("Location18").EntireColumn.Hidden)
Range("Location19").EntireColumn.Hidden = Not (Range("Location19").EntireColumn.Hidden)
Range("Location20").EntireColumn.Hidden = Not (Range("Location20").EntireColumn.Hidden)
Range("Location21").EntireColumn.Hidden = Not (Range("Location21").EntireColumn.Hidden)
Range("Location22").EntireColumn.Hidden = Not (Range("Location22").EntireColumn.Hidden)
Range("Location23").EntireColumn.Hidden = Not (Range("Location23").EntireColumn.Hidden)
Range("Location24").EntireColumn.Hidden = Not (Range("Location24").EntireColumn.Hidden)
Range("Location25").EntireColumn.Hidden = Not (Range("Location25").EntireColumn.Hidden)
Range("Location26").EntireColumn.Hidden = Not (Range("Location26").EntireColumn.Hidden)
Range("Location27").EntireColumn.Hidden = Not (Range("Location27").EntireColumn.Hidden)
Range("Location28").EntireColumn.Hidden = Not (Range("Location28").EntireColumn.Hidden)
End Sub



Is there a clever way in VBA to have code which basically identifies a specific named range and hides ALL OTHER named ranges? This way although I will still have to make a macro for each location (column), it will be a lot simpler than that beast above and easier to make new ones for any additional columns that are added.

Any help people can offer would be greatly appreciated.

Fishboy
 
Here is my suggestion. This keeps you from having to name each column.
On your worksheet put all the names like:
Basildon
Birmingham
BristBasildon
Birmingham
Bristolol
In column “A”
In column “B” put the Column number assigned this name
Like:
Bassildon 1
Birmingham 2
Now put the below script in the sheet.
To install this code:

• Right-click on the sheet tab
• Select View Code from the pop-up context menu
• Paste the code in the VBA edit window

Now when you double click on the number in column “B” it will cause that column number to be hidden. Double click it again and it will unhide that column number.
To hide all or unhide all put the text “All” in column “A” and something like this in column “B” C:J
Double clicking on C:J
Would cause columns C:J to hide or unhide.
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
If Not Intersect(Target, Range("B:B")) Is Nothing Then
Columns(Target.Value).Hidden = Not Columns(Target.Value).Hidden
End If
End Sub
Hi again My Aswer Is This, thanks for the help.

Now, either I am doing something wrong, am confused or this doesn't actually do what I want it to. I am unsure which of these it is...

Just to make sure we are all actually reading from the same page, please see an example snippet from the top of my workbook. This shows how the data is already laid out.

Row 1 has all of the macro buttons (not shown by MrExcel HTML editor, but trust me, there are buttons there)
Row 2 are the column headers, including the 16 current locations, one in each column going horizontally across the sheet.
The reason column B appears blank is further down the sheet (not shown) there are other macro buttons which do other things unrelated to this issue.

Excel 2010
ABCDEFGHIJKLMNOPQR

<tbody>
[TD="align: center"]1[/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]
[TD="align: center"][/TD]

[TD="align: center"]2[/TD]
[TD="bgcolor: #008BC6, align: center"]Candidate Summary[/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"]Basildon[/TD]
[TD="bgcolor: #008BC6, align: center"]Birmingham[/TD]
[TD="bgcolor: #008BC6, align: center"]Bristol[/TD]
[TD="bgcolor: #008BC6, align: center"]Crawley[/TD]
[TD="bgcolor: #008BC6, align: center"]Didcot[/TD]
[TD="bgcolor: #008BC6, align: center"]Glasgow[/TD]
[TD="bgcolor: #008BC6, align: center"]Leeds[/TD]
[TD="bgcolor: #008BC6, align: center"]Leicester[/TD]
[TD="bgcolor: #008BC6, align: center"]Livingston[/TD]
[TD="bgcolor: #008BC6, align: center"]London[/TD]
[TD="bgcolor: #008BC6, align: center"]Manchester[/TD]
[TD="bgcolor: #008BC6, align: center"]Milton Keynes[/TD]
[TD="bgcolor: #008BC6, align: center"]Newcastle[/TD]
[TD="bgcolor: #008BC6, align: center"]Norwich[/TD]
[TD="bgcolor: #008BC6, align: center"]Peterborough[/TD]
[TD="bgcolor: #008BC6, align: center"]Swindon[/TD]

[TD="align: center"]3[/TD]
[TD="align: center"]Total Candidates[/TD]
[TD="align: center"][/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]23[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]28[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]64[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]5[/TD]

[TD="align: center"]4[/TD]
[TD="bgcolor: #008BC6, align: center"]Vetting Level Summary[/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]

[TD="align: center"]5[/TD]
[TD="align: center"]L1 Vetting[/TD]
[TD="align: center"][/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]

[TD="align: center"]6[/TD]
[TD="align: center"]L2 Vetting[/TD]
[TD="align: center"][/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]23[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]25[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]10[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]5[/TD]

[TD="align: center"]7[/TD]
[TD="align: center"]L3 Vetting[/TD]
[TD="align: center"][/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]54[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]

</tbody>
Sheet1



So, as per the example, when I click button at the top of column C (currently assigned to Basildon), columns D:R hide / unhide.
When I click the button at the top of column D (currently assigned to Birmingham), columns B, then E:R hide / unhide.
When I click the button at the top of column E (currently assigned to Bristol), columns B:C, then F:R hide / unhide.
And so on...

From what I can gather from your code, (or at least what I saw from testing on a copy of my data), it only worked on column B. I could not get it to work with any column after B.

Am I doing something wrong, or now that you can visualise the layout of data can you see if your code doesn't apply?
 
Last edited:
Upvote 0
Yes I understand what you are doing. With my plan you need no scripts except for the one I asked you to install in the sheet. You should see my instructions from my previous post.
Now my script works this way.
You should put the Names Bristol , Birmingham in Column "A"
Then in column "B" put the letter "C"
Now when you double click on the Letter "C" in column "B" it will hide Column "C"
Double click it again and it will unhide the Column.
This eliminates making all the scripts and naming all the ranges.
The basics of the script are:
Target value Hide
Your target is the cell you double click on and the value is the letter in the cell you double clicked on.
So if you double click on the value "B" it hides Column "B"
Now this only works on values in Column "B"
I don't know how to explain this any better.
Now this only hides or unhides one column at a time.
As I explained in my previous post
If you put into column B "C:J" without the quotes and then double click "C:J" it will hide or unhide those columns.
Let me know if you understand this. This plan would save you tons of work. I know this works I have tested it.
 
Last edited:
Upvote 0
Right, so it is always going to be the top of colum B that I am clicking on, but I just need to change the value of column B (in your latter example C:J) to make those columns hide?

if that is right, how do I make it hide say B and C, skip D as I want this shown, but then also hide E onwards? As in, how would I define that at the top of column B?
 
Upvote 0
No. My script should work on any row of column B.
Put the letter "J" in cell B6 and then double click cell B6 and it should hide/unhide column "J"
When you understand this I will then explain hiding or unhideing all the others.
 
Upvote 0
No. My script should work on any row of column B.
Put the letter "J" in cell B6 and then double click cell B6 and it should hide/unhide column "J"
When you understand this I will then explain hiding or unhideing all the others.
Ok, I understand this and have it working as described, and think I am ready to hear how to hide / unhide the rest. That said however, in order to achieve this I have had to completely redesign the layout of my test data and now the place names go down column B instead of go across the top, which is what I need. Can your code be amended to work across row 2 instead of down column B?
 
Upvote 0
Nevermind, the change from B:B to 2:2 was actually obvious when I thought about it. If you can explain how your idea will hide multiple columns but skipping out the ones I need kept, I think we are on to a winner here
 
Upvote 0
Glad you changed your mind about changing from B to 2. That was causing me problems.
As far as hiding all the columns except for the one you want visible.
Would it not work for you to double click C:J which hides or unhides them all and then double click the one you want to see.
I would think if your not working on them you would want them hid.

I have one more change I made so you would be unable to hide column B or A which is where I assume you have your City Name in A and letter in column "B".
I'll include that change in my final post if your happy with my suggestion on hiding all but one.
With what we are doing here would eliminate the need for all the buttons and scripts.
 
Last edited:
Upvote 0
Ok, so I changed B:B to 2:2, and eventually from 2:2 to 1:1, so basically this functionality replaces where the buttons used to be.


Excel 2010
ABCDEFGHIJKLMNOPQR
1CD:REFGHIJKLMNOPQRS
2Candidate SummaryBasildonBirminghamBristolCrawleyDidcotGlasgowLeedsLeicesterLivingstonLondonManchesterMilton KeynesNewcastleNorwichPeterboroughSwindon
3Total Candidates4124112219301167215
4Vetting Level Summary
5L1 Vetting0000000002000000
6L2 Vetting4024112200271161815
7L3 Vetting01000001910005400
Sheet1


The problems I am having though are as follows:

In column C if I put D:R, that is all good. It only shows me the data from column C, but...

For column D I need C to hide, AND then E:R. I do not see how this system can account for that.

This problem then escalates as I go further across the sheet. For column E I need to hide C:D, skip E as I want this shown, but then hide F:R.

As cumbersome as the macros were, they at least allowed me to specify exactly what columns to hide and what to show.
 
Upvote 0
Yes I saw several problems with using 2:2 instead of B:B
And in my previous post I recommended hiding them all by double clicking C:J and then double clicking the one you want to unhide but this is not possible when your using 2:2.
This was just a suggestion. I did not know you were definitely set on using a 2:2 version.
You see the ideal and will have to decide what you think is best.
I would have to think a lot about how to make it easy using 2:2
 
Upvote 0
Ok, so I changed B:B to 2:2, and eventually from 2:2 to 1:1, so basically this functionality replaces where the buttons used to be.

Excel 2010
ABCDEFGHIJKLMNOPQR
CD:REFGHIJKLMNOPQRS

<tbody>
[TD="align: center"]1[/TD]
[TD="align: right"][/TD]

[TD="align: center"]2[/TD]
[TD="bgcolor: #008BC6, align: center"]Candidate Summary[/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"]Basildon[/TD]
[TD="bgcolor: #008BC6, align: center"]Birmingham[/TD]
[TD="bgcolor: #008BC6, align: center"]Bristol[/TD]
[TD="bgcolor: #008BC6, align: center"]Crawley[/TD]
[TD="bgcolor: #008BC6, align: center"]Didcot[/TD]
[TD="bgcolor: #008BC6, align: center"]Glasgow[/TD]
[TD="bgcolor: #008BC6, align: center"]Leeds[/TD]
[TD="bgcolor: #008BC6, align: center"]Leicester[/TD]
[TD="bgcolor: #008BC6, align: center"]Livingston[/TD]
[TD="bgcolor: #008BC6, align: center"]London[/TD]
[TD="bgcolor: #008BC6, align: center"]Manchester[/TD]
[TD="bgcolor: #008BC6, align: center"]Milton Keynes[/TD]
[TD="bgcolor: #008BC6, align: center"]Newcastle[/TD]
[TD="bgcolor: #008BC6, align: center"]Norwich[/TD]
[TD="bgcolor: #008BC6, align: center"]Peterborough[/TD]
[TD="bgcolor: #008BC6, align: center"]Swindon[/TD]

[TD="align: center"]3[/TD]
[TD="align: center"]Total Candidates[/TD]
[TD="align: center"][/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]24[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]9[/TD]
[TD="align: center"]30[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]6[/TD]
[TD="align: center"]72[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]5[/TD]

[TD="align: center"]4[/TD]
[TD="bgcolor: #008BC6, align: center"]Vetting Level Summary[/TD]
[TD="bgcolor: #000000, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]
[TD="bgcolor: #008BC6, align: center"][/TD]

[TD="align: center"]5[/TD]
[TD="align: center"]L1 Vetting[/TD]
[TD="align: center"][/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]

[TD="align: center"]6[/TD]
[TD="align: center"]L2 Vetting[/TD]
[TD="align: center"][/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]24[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]27[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]6[/TD]
[TD="align: center"]18[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]5[/TD]

[TD="align: center"]7[/TD]
[TD="align: center"]L3 Vetting[/TD]
[TD="align: center"][/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]9[/TD]
[TD="align: center"]1[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]54[/TD]
[TD="align: center"]0[/TD]
[TD="align: center"]0[/TD]

</tbody>
Sheet1
Now this is just a little snippet of my data, only showing the first 7 rows. My actual data goes on for about 60-70 rows downwards, whereas I will only even have say maybe 20-25 locations max. Changing it so my locations went down a column instead of across a row would actually be a massive ball ache and ultimately involve a time consuming rewrite of the whole thing (those values in the cells are lookups and formulas, not just raw data).

I really do appreciate the help, but I may have to look into other possible workarounds or solutions.
 
Upvote 0

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