VBA to automatically hide columns based on date

Lightkeepr

New Member
Joined
Apr 6, 2021
Messages
27
Office Version
  1. 365
Platform
  1. Windows
Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAP
1
2
3
4
5
6
7Jul-21Jun-21May-21Apr-21Mar-21Feb-21Jan-21Dec-20Nov-20Oct-20Sep-20Aug-20Jul-20Jun-20May-20Apr-20Mar-20Feb-20ORIGINAL
8
9
10
Sheet1


I have a sheet where I have columns of data by months and the month that column represents is always on row 7 of the sheet. Sometimes there is 2 months, sometimes there is 36 months. What I am trying to do is have the sheet always show the columns for the most recent six months (07/2021 back to 02/2021) and then the next five end of quarter months (in this example it would be 12/2020, 9/2020, 6/2020, 03/2020) and the ORIGINAL column.

I've been trying to think of a way to achieve this but get stuck. I had thought of putting in a helper cell in row 1 that would be something along the lines of
[in cell E1]
=IF(DATEDIF(E7,$E$7,"M")<=5,"SHOW","N")

and have the vba look at each column and if it listed 'SHOW' that column and the one next to it were visible and if it listed "N", that column and the one next to it were hidden.

But where I next get tripped up would be is looking at the other dates greater than the current + five back (or before Feb 2021) to establish which are the previous 5 end of quarter dates plus the "ORIGINAL" column.

Ideally I don't want to have a helper cell and it would have the VBA look at it and hide the columns Q:R, U:X, AA:AD, AG:AJ, AM:AN in this instance and if a new month was added in at column E:F and everything pushed to the right, it would reevaluate the dates and adjust which are shown and which are hidden.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

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