Using VBA to hide columns doesn't hide them the same as "hide"

Jimbomack

New Member
Joined
Aug 27, 2020
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi,
I have a worksheet that contains data in columns A through ZY. I have the worksheet window split between columns M and N. All columns from N through ZY are titled with dates. I want to use a macro to hide certain columns from N through to a user selected column that is based on the date title. The user enters a date in a cell and runs the Macro and all columns from N through to that dated column are then hidden. The reason for hiding these columns is so that when the user drags from a column to the left of N, over the hidden columns, the hidden columns remain hidden, unlike if the window was simply split at column M-N.
When I "hide" the columns using the traditional drop down in Excel, it works fine as desired. Those columns never show up, regardless of where I drag my mouse.

The Macro below works like a charm in hiding the columns, but when I drag across the boundary over the hidden columns, the hidden columns reappear. It's like the VBA code for hiding columns is not the same as the basic "Hide" command found in the drop down in Excel. I've read/proven that "Veryhidden" does not work for columns.
What am I doing wrong?
Thx

Sub hidecolumns()
Dim X As Variant
X = Range("j2").Value
Columns("N").Columns(X).Select
Selection.Columns.Hidden = True
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
What's the value of J2 when you run this macro?
 
Upvote 0
it is a column address, such "AD" for example. So if J2 = AD, then the macro hides columns N through AD.

As mentioned, it hides the columns, but not the same as when using the "Hide" command from the drop down.

Thx
 
Upvote 0
Try to avoid using Select.

The hidden in VBA works the exact same as hiding it manually.

VBA Code:
Sub hideColumn()
Dim X As String
With Sheets("Sheet1") 'Change this to your sheet name
    X = .Range("J2").Value
    .Columns("N").EntireColumn.Hidden = True
    .Columns(X).EntireColumn.Hidden = True
End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub hidecolumns()
Dim X As Variant
X = Range("j2").Value
Columns("N:" & X).Hidden = True
End Sub
 
Upvote 0
How about
VBA Code:
Sub hidecolumns()
Dim X As Variant
X = Range("j2").Value
Columns("N:" & X).Hidden = True
End Sub
what if the column is before N? I tried that and it hid B:N but i think he would only want columns B and N hidden and the rest to be shown.
 
Upvote 0
Hi,
This one worked perfectly.

Sub hidecolumns()
Dim X As Variant
X = Range("j2").Value
Columns("N:" & X).Hidden = True
End Sub

It hides the columns, and when I drag/select across the hidden columns, they stay hidden.
I'm new to VBA programming and could not figure out how to combine the variable into one line like you did: "Columns("N:" & X).Hidden = True". The difference seems to be the use of "selection", and that I used " Columns("N").Columns(X).Select" Something in this sequence does not hide the columns like the "Hide" command, but simply scrolls the columns I want hidden under the A-N set of columns, similar to a split window effect.

Super thankful to all of you.
Sincerely.
Any suggestions on how to best learn more VBA. I'm an old dog, using youtube videos like Learnit, WiseOwl ect. Love this stuff.
Cheers
 
Upvote 0
Using this Columns("N").Columns(X) does not hide columns N to whatever, it only hides one column, so if you have AD in J2 it will actually hide column AQ.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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