All you need to do is make a small change to the HideCols function. Now what do we do if we want to see the hidden rows again? Un-hiding Columns Based on Cell Value when Macro is Executed
#Unhide a column in excel code
In this way, the above code hides all the columns containing an ‘X’ in row 8. Line 8 simply demarcates the end of the HideCols sub-routine.If the cell contains the value “X”, then we set the ‘Hidden’ attribute of the entire column (corresponding to that cell) to True, which means we want to hide the entire corresponding column. In lines 3 to 7, we looped through each cell in row “8” of the Active Worksheet.In line 2 we defined a variable called cell, which can refer to a range of cells.
It should essentially analyze each cell from A8 to F8 and adjust the ‘Hidden’ attribute of the column that you want to hide. Say you have columns containing sales figures for Monday through Friday, and you want to run a macro to hide all columns that have the letter X in row 8.įor this, we need a macro that will loop through each cell of row 8 and hide the corresponding column. Let us use the following dataset to demonstrate:
It can be a number, a letter, a word, or even a phrase. The value, based on which you want to hide the columns, can be anything you like.
#Unhide a column in excel how to
In this example, we will show you how to hide all columns that contain a particular value in a given cell. Hiding Columns Based on Cell Value when Macro is Executed Hiding Columns in Real-time Based on a Cell Value.Un-hiding Columns Based on Cell Value when Macro is Executed.Hiding Columns Based on Cell Value when Macro is Executed.