Columns really will not hide/unhide with VBA code

joe-engr

New Member
Joined
Nov 25, 2010
Messages
2
I have searched the forum related to hiding & unhiding ranges, but none of the code examples/solutions have done the trick for me!

I want to be able to unhide a column in multiple sheets with the click of a button. I then have a second button which will hide these columns. Here is my code from the HIDE button:

Private Sub CommandButton3_Click()
Sheets("Do").Activate
Columns("F:F").EntireColumn.Hidden = True
Sheets("Re").Select
Columns("F:F").EntireColumn.Hidden = True
Sheets("Mi").Select
Columns("F:F").EntireColumn.Hidden = True
Sheets("Fa").Select
Columns("F:F").EntireColumn.Hidden = True
Sheets("So").Select
Columns("F:F").EntireColumn.Hidden = True
End Sub

When the button is clicked, I can see that the macro runs through the sheets (screen flashes quickly), but then Column F in each of the sheets is still visible. I am receiving no error.

May I please get some ideas?

Joe
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Welcome to the board.

Hmm, that's weird.. Works fine for me.

What happens when you try to do it manually?
 
Upvote 0
Try

Code:
Private Sub CommandButton3_Click()
Dim ws As Worksheet
For Each ws In Sheets(Array("Do", "Re", "Mi", "Fa", "So"))
    ws.Columns("F").Hidden = True
Next ws
End Sub
 
Upvote 0
Thanks for the welcome! I usually troubleshoot myself, and my research on the internet is sufficient for code help. I am admittedly still a novice in VBA.

When I do it manually, via right-clicking the column etc., it works fine. I am working with MS Office in the German language, but if the VBA code wanted German, I would think it would give me an error.

OK, now, I tried Peter's code, and it WORKED! I have no explanation for why the other didn't, but the result is now good.

Thank you both for the help.

I see how this for-next process is working, and I will add that and the other elements to my code knowledge base!

Joe
 
Upvote 0
Perhaps you have other code that unhides the column on sheet activate or deactivate?
 
Upvote 0
I have no explanation for why the other didn't, but the result is now good.
The other code didn't work because the code is in a particular sheet's module. Even though you Activate sheet 'Do', when you use
Rich (BB code):
Columns("F:F").EntireColumn.Hidden = True
that refers to the sheet belonging to the module the code is in, not the Active sheet.

If you had done this, I believe it would have worked
Rich (BB code):
Sheets("Do").Activate
ActiveSheet.Columns("F:F").EntireColumn.Hidden = True
Sheets("Re").Activate
ActiveSheet.Columns("F:F").EntireColumn.Hidden = True

However, I would still go with Peter's looping code that doesn't require each sheet to be activated to do the hiding/unhiding.
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,042
Members
449,063
Latest member
ak94

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