Auto Hide Columns based on date

ExcelRoy

Well-known Member
Joined
Oct 2, 2006
Messages
2,540
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Would it be possible to automatically hide columns based on todays date

I have a GANTT chart with dates in column (FM1:APX1) and would like to auto hide based on todays date

The week commencing todays date and 4 full weeks after tiodays date

Many thabnsk for any help
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Upvote 0
Hi vladimiratanasiu,

Many thanks for this
Can the contents of K4 be static and set as today and how do we change the range to suit this working week and the next 4 full weeks?

Thanks for the help
 
Upvote 0
Try this, adjusting in the macro your proper references.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim dt As Range
  If Intersect(Target, Range("K4")) Is Nothing Then Exit Sub
  Columns("M:W").Hidden = False
   For Each dt In Range("M4:W4")
    If dt.Value < [K4] Or dt.Value > [K4+28] Then Columns(dt.Column).Hidden = True
   Next dt
End Sub

Hide columns.xlsm
KLMNOPQRSTUVW
47-Jun-239-May-2316-May-2323-May-2330-May-236-Jun-2313-Jun-2320-Jun-2327-Jun-234-Jul-2311-Jul-2318-Jul-23
5-35-26-17-81101928374655
6-34-25-16-72112029384756
7-33-24-15-63122130394857
8-32-23-14-54132231404958
9-31-22-13-45142332415059
10-30-21-12-36152433425160
11-29-20-11-27162534435261
12-28-19-10-18172635445362
13-27-18-909182736455463
Sheet2
 
Last edited:
Upvote 0
Hi vladimiratanasiu,

Many thanks for this
Can the contents of K4 be static and set as today and how do we change the range to suit this working week and the next 4 full weeks?

Thanks for the help
If you want to be considered the present week too, change in the macro the value 28 with 35 and insert in K4 its first working day (e.g. 05.06.2023). This weekly table is just a sample, but you can use one as detailed as you need for.
Hide columns.xlsm
KLMNOPQRST
45-Jun-2326-May-2327-May-2328-May-2329-May-235-Jun-2312-Jun-2319-Jun-2326-Jun-23
5-35-26-17-81101928
6-34-25-16-72112029
7-33-24-15-63122130
8-32-23-14-54132231
9-31-22-13-45142332
10-30-21-12-36152433
11-29-20-11-27162534
12-28-19-10-18172635
13-27-18-909182736
Sheet2
 
Last edited:
Upvote 0
Hi vladimiratanasiu,

Many thanks, almost there, K4 is set as =now() and the desired result would show week beginning from a monday and the following 4 full weeks

Thanks
 
Upvote 0
Hi vladimiratanasiu,

Thought it important to add that U1:APX1 is single dates and not weeks

Thanks
 
Upvote 0
Try the below macro. My original table was larger (26.05-21.07.2023 - columns M-BQ). This one includes the period collapsed automatically, between the most recent Monday date (05.06) and 5 weeks after it (10.07). You need to adjust the references from the macro to your proper requirements.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim dt As Range
  If Intersect(Target, Range("K4")) Is Nothing Then Exit Sub
  Columns("M:BQ").Hidden = False
   For Each dt In Range("M4:BQ4")
    If dt.Value < (Date - Weekday(Date, vbMonday) + 1) Or dt.Value > DateAdd("d", 35, (Date - Weekday(Date, vbMonday) + 1)) Then Columns(dt.Column).Hidden = True
   Next dt
End Sub

Hide columns.xlsm
KLWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBR
1
2
3123456789101112131415161718192021222324252627282930313233343536
46/8/20236/5/20236/6/20236/7/20236/8/20236/9/20236/10/20236/11/20236/12/20236/13/20236/14/20236/15/20236/16/20236/17/20236/18/20236/19/20236/20/20236/21/20236/22/20236/23/20236/24/20236/25/20236/26/20236/27/20236/28/20236/29/20236/30/20237/1/20237/2/20237/3/20237/4/20237/5/20237/6/20237/7/20237/8/20237/9/20237/10/2023
55564738291100109118127136145154163172181190199208217226235244253262271280289298307316325334343352361370
65665748392101110119128137146155164173182191200209218227236245254263272281290299308317326335344353362371
75766758493102111120129138147156165174183192201210219228237246255264273282291300309318327336345354363372
Sheet2
Cell Formulas
RangeFormula
K4K4=NOW()
 
Upvote 0
I made a small adjustment to the code, considering that 35 days include the first Monday, too.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim dt As Range
  If Intersect(Target, Range("K4")) Is Nothing Then Exit Sub
  Columns("M:BQ").Hidden = False
   For Each dt In Range("M4:BQ4")
    If dt.Value < (Date - Weekday(Date, vbMonday) + 1) Or dt.Value > DateAdd("d", 34, (Date - Weekday(Date, vbMonday) + 1)) Then Columns(dt.Column).Hidden = True
   Next dt
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,121
Messages
6,123,177
Members
449,093
Latest member
bes000

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