PDA

View Full Version : Excel 2003 Macro VBA Hide / Unhide Multiple Sheets


kyhelpdesk
Oct 1, 2008, 10:45 AM
I'd like to assign a macro button to hide / unhide a range of sheets in a workbook.

I have tried various codes from web but have not been successful. (acutally one of the codes worked if I selected 'run' from the macro menu but does not run if button is clicked).

I have tried 'recording' and assigning to button, but still, button does not function. I cannot tell if it is an poorly written macro or an issue with, for some reason, my button is not enabled (BTW, it shows enabled in properties).

For this example lets use sheets labeled: JAN, FEB, MAR, APRIL, MAY, JUNE. What would code look like if I want to hide FEB - APRIL & leave others visible. Then, unhide same list.

I don't know much about VB but can folllow the logic of the code.

Thanks,
KhD

JBeaucaire
Oct 1, 2008, 03:54 PM
Use the Control Toolbox toolbar to create a Command Button.
Right-click the new button, and select Properties
Put a new CAPTION called TURN FEB OFF
Close the Properties Window
Now, let's have some fun: Right-click the new button again and select View Code
A blinking cursor is ready for your new code. Add the code below:


Application.ScreenUpdating = False
If CommandButton10.Caption = "TURN FEB OFF" Then
CommandButton10.Caption = "TURN FEB ON"
CommandButton10.ForeColor = &HFFFF&
Sheets("FEB").Visible = False
Else: CommandButton10.Caption = "TURN FEB OFF"
CommandButton10.ForeColor = &HC0&
Sheets("FEB").Visible = True
Edit the CommandButton10 to match the numeric value of your new button so it works...edit all the references.
Now click the Exit Design Mode button on the Control Toolbox toolbar.
Try clicking your new button

If this works to your satisfaction, edit the VBA above to do all the sheets, or make individual buttons. Your call.

stevesmith27
Mar 13, 2011, 03:40 AM
Thank you for the code.

Being new to Excel and using codes.

What do you mean by;
Edit the CommandButton10 to match the numeric value of your new button.

Look forward to your reply.

Steve

JBeaucaire
Mar 13, 2011, 04:46 AM
Each time you add a new CommandButton to your form or worksheet, it is given a default name. The names are sequential, the first button added is CommandButton1, then CommandButton2, etc.

In my sample code above I demonstrated how the Caption (text showing on the button) can be tested/evaluated and then the button code can do specific things based on what that caption is. In this way, instead of having two buttons, one to hide and one to unhide a sheet, you can have a single button that "toggles" and does both jobs.

Anyway, the sample code was showing how to edit the CommandButton10, and I was simply pointing out that when you edit the code for any particular CommandButton, make sure you reference all the commands in the macro TO the correct button by changing the 10 to the correct index number o the button being worked on.

stevesmith27
Mar 13, 2011, 06:18 AM
You are a real star. It worked 1st time.
I am sure I will will have more question, but...

Thank you very much

Steve

stevesmith27
Mar 14, 2011, 12:10 AM
Now I have the buttons working to hide/unhide worksheets. Next can I have the same button not only unhide but also at the same time go to a 2nd worksheet, then another button on the now open worksheet to close and return to the 1st worksheet

Cheers

Steve