

But if that sheet doesn’t exist, then you’ll get a Run-time error ‘9’. If the sheet that you want to unhide it already visible, this code won’t show any error. To unhide a sheet, you need to change the visible property to TRUE. The above code loops through all the sheets of the workbook, then match each sheet’s name with the active sheet’s name and hide it if it doesn’t match.


There must be one sheet visible all the time.

Now there one thing that you need to understand you can’t hide all the sheets. And check for the sheet that you want to hide. The above code uses the FOR EACH LOOP + IF STATEMENT to loop through each sheet in the workbook. MsgBox "Sheet not found", vbCritical, "Error" You can also use a small code like the following to check the sheet that you want to hide exits or not. Helpful Links: Run a Macro – Macro Recorder – Visual Basic Editor – Personal Macro Workbook Check Sheet Before Hiding This code refers to the cell A1 and uses the value from it to refer to the sheet that you want to hide. Excel VBA Hide & Unhide Columns With Userform. Press question mark to learn the rest of the keyboard shortcuts. Note: Properties and their values in VBA are case sensitive.Hide a Sheet Based on the Value from a CellĪlright, if you want to use a cell value instead of directly using the sheet name in the code, you can refer to that cell. The userform that we created to hide the columns in the workbook and unhide the hidden columns contains also a button to minimize userform. Here I have set the Hidden property as False. In the ElseIf condition, the macro checks if cell C2 is clicked. The Hidden property can also be used to hide and unhide rows. The Hidden property in VBA returns a Variant that indicates if the columns are hidden. Once I have the entire column or the range, I am using the Hidden property to hide or unhide the column. This property returns an object that represents the entire column. See the first condition, where I have used the EntireColumn property. Related: How to Change the Column Width and Row Height in Excel using VBA However, I want it to take an action, when the user clicks in either cell B2 or C2. VBA will call this event whenever a user clicks on a cell (any cell) in Sheet1. I have written my macro inside the Worksheet_SelectionChange event.
