Group column based on today's date

TG2812

Board Regular
Joined
Apr 15, 2015
Messages
192
Hi All,

I'm currently trying to come up with a VBA module that aims at grouping all columns whose date are past (against today's date). Date to check against are located in row 2 of Sheet1.

I would like the macro to execute upon the workbook opening only.

This is what I did so far but VBA returns a "run mismatch error". Any idea what i did wrong?

Private Sub Workbook_Open(ByVal Target As Range)

Dim xCell As Range
If Target.Address <> Range("A1").Address Then Exit Sub
Application.ScreenUpdating = False
For Each xCell In Rows(2)
xCell.EntireColumn.Group = (xCell.Value < Target.Value)
Next
Application.ScreenUpdating = True
End Sub





Bear with my question, I'm a beginner in VBA world.

Thank you very much in advance for your time and answers.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
How about
Code:
Private Sub Workbook_Open()
   Dim Ws As Worksheet
   Dim Cl As Range
   
   Set Ws = Sheets("[COLOR=#ff0000]Master[/COLOR]")
   For Each Cl In Ws.Range("[COLOR=#ff0000]B1:V1[/COLOR]")
      If Cl.Value < Date Then Cl.EntireColumn.Group
   Next Cl
End Sub
Change values in red to suit
 
Upvote 0
Hi,

Your solution works great. However, each time I open the workbook, Excel creates another grouping on top of the one already created.
Do I have to ungroup upon workbook closure or is there any other way to work this around?

Many thanks in advance for your help.
 
Upvote 0
How about
Code:
Private Sub Workbook_Open()
   Dim Ws As Worksheet
   Dim Cl As Range

   Set Ws = Sheets("Master")
   Cells.ClearOutline
   For Each Cl In Ws.Range("B1:V1")
      If Cl.Value < Date Then Cl.EntireColumn.Group
   Next Cl
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

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