Help - Existing VBA routine to hide one column hides three?

EssKayKay

Board Regular
Joined
Jan 5, 2003
Messages
233
Office Version
  1. 2007
Platform
  1. Windows
I have a worksheet were I’m attempting to hide Column H via a VBA routine. I have two subs, one to hide Column H; the other to unhide it. However, when I run the code to hide, it hides Column H plus the next two columns I&J. Running the routine to unhide column H unhides all three columns that where hidden. I can “manually” hide just column H but not via VBA. That is: select Column H only > click on highlighted Column H > right click mouse and select Hide.

Here’s my test code:

----------------- Hide Column H -----------------------

Sub Macro1()
Columns("H:H").Select
Selection.EntireColumn.Hidden = True
Range("C2").Select
End Sub
--------------------------------------------------------------

---------------- Unhide Column H ---------------------

Sub Macro2()
Columns("H:H").Select
Selection.EntireColumn.Hidden = False
Range("C2").Select
End Sub
--------------------------------------------------------------
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
The issue might be due to the way Excel handles the selection of columns. When you select a column and hide it using VBA, Excel may also include adjacent columns in the selection.

Try:
VBA Code:
Sub HideColumnH()
    Columns("H:H").Hidden = True
End Sub

PS: Please use the code tags.
 
Upvote 0
Instead of having 2 macros, just toggle.
Code:
Range("H:H").EntireColumn.Hidden = Not Range("H:H").EntireColumn.Hidden
 
Upvote 0
The issue might be due to the way Excel handles the selection of columns. When you select a column and hide it using VBA, Excel may also include adjacent columns in the selection.

Try:
VBA Code:
Sub HideColumnH()
    Columns("H:H").Hidden = True
End Sub

PS: Please use the code tags.
Perfect. Thank you once again BigBeach. This is working great. Much appreciated.
I will try to use code tags in the future. It's just that this old guy isn't the most proficient but I will get better.
 
Upvote 0
Instead of having 2 macros, just toggle.
Code:
Range("H:H").EntireColumn.Hidden = Not Range("H:H").EntireColumn.Hidden
Thank you Jolivanes, I will keep this in mind. For now I am going to use two separate routines but I will play with your code and see what happens
Again, thanks,
Steve
 
Upvote 0

Forum statistics

Threads
1,215,098
Messages
6,123,082
Members
449,094
Latest member
mystic19

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