Hide columns with VBA

crew11k

New Member
Joined
Nov 11, 2011
Messages
8
I have a .xlsx spreadsheet with 10 columns and 2 rows (A1:J10). If A1 is blank I want to hide columns A through J. If B1 is blank I want to hide columns B through J. If cell C1 is blank, I want to hide columns C through J etc. How do I write code such that once the columns are hidden, if the value in B1 changes, it will unhide the column. I think it is a select a case type case but am not sure how to write this. Any thoughts or suggestions would be greatly appreciated.

Thanks

Ron
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Thank you for the kind welcome. A formula linking from another worksheet will be the cause of the change in value which would trigger the unhide or hide column. Does this help?
 
Upvote 0
You could use a calculate event, first unhiding all of the columns, then evaluate each cell for your criteria. If it matches, then hide the columns, otherwise show them.

With the calculate event and a range of cells to evaluate you generally use a For...Each...Next construct:

<font face=Calibri><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br>    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> Range<br>    <br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>        Cells.Columns.EntireColumn.Hidden = <SPAN style="color:#00007F">False</SPAN><br>        <br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> c <SPAN style="color:#00007F">In</SPAN> Range("A1:J1")<br>            <SPAN style="color:#00007F">If</SPAN> c.Value = 0 <SPAN style="color:#00007F">Then</SPAN><br>                Range(Cells(1, c.Column), Cells(1, "J")).EntireColumn.Hidden = <SPAN style="color:#00007F">True</SPAN><br>            Else: Cells(1, c.Column).EntireColumn.Hidden = <SPAN style="color:#00007F">False</SPAN><br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> c<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

HTH,
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,540
Members
449,038
Latest member
Guest1337

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