Hide columns automatically using VBA

Prish

Board Regular
Joined
Mar 30, 2016
Messages
91
Hi,

I have the following code to hide columns, however if someone inserts a column before D, I want the hidden columns to shift as well, i.e Hide columns E:BA if one row was inserted.

Code:
Columns("D:AZ").Hidden = True
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I'm no genius at this but could you try referring to the column header such that if column header = x then hide the next number of columns?
 
Upvote 0
Select all of Columns D:AZ and give it a Defined Name (after selecting them, click in the Name Box, located to the left of the Formula Bar, and type a valid name) such as ColumnsToHide, then use this code line in place of the one you posted...

Range("ColumnsToHide").EntireColumn.Hidden = True

If anyone inserts or deletes columns in front of the range you named, the reference for that Defined Name will automatically update.
 
Upvote 0
Select all of Columns D:AZ and give it a Defined Name (after selecting them, click in the Name Box, located to the left of the Formula Bar, and type a valid name) such as ColumnsToHide, then use this code line in place of the one you posted...

Range("ColumnsToHide").EntireColumn.Hidden = True

If anyone inserts or deletes columns in front of the range you named, the reference for that Defined Name will automatically update.

Thanks, works perfectly.
 
Upvote 0

Forum statistics

Threads
1,215,482
Messages
6,125,061
Members
449,206
Latest member
Healthydogs

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