Grouping Columns based on content

scotthannaford1973

Board Regular
Joined
Sep 27, 2017
Messages
114
Office Version
  1. 2010
Platform
  1. Windows
h5SCvU
Hi all

okay - hoping you can help with some VBA here...

at the top of 52 columns I have the date of the
h5SCvU
first day in each week - as per attached screenshot; I want to group all past date columns together and all future date columns. Obviously I can do this manually, but I'd like to automate it so that, ideally:

- all columns more than 3 weeks in the past (i.e. <today()-21)
- all columns more than 6 weeks in the future (i.e. >Today()+42)

are grouped (2 groups, not one, obviously)

I am sure this could be done through some nice coding, but I wouldn't know where to start!

much appreciated!
if image doesn't appear: https://ibb.co/h5SCvU</today()-21)
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
See if this is the sort of thing you are after. Test in a copy of your workbook.

The red numbers might need a little tweaking to get exactly what you want.

Rich (BB code):
Sub Group_Weeks()
  Dim CurrCol As Long
  
  ActiveSheet.UsedRange.EntireColumn.ClearOutline
  CurrCol = Application.Match(CLng(Date), Rows(1))
  If CurrCol > 3 Then Columns(1).Resize(, CurrCol - 3).Group
  If CurrCol < 46 Then Columns(CurrCol + 6).Resize(, 53 - CurrCol - 6).Group
End Sub
 
Upvote 0
See if this is the sort of thing you are after. Test in a copy of your workbook.

The red numbers might need a little tweaking to get exactly what you want.

Rich (BB code):
Sub Group_Weeks()
  Dim CurrCol As Long
  
  ActiveSheet.UsedRange.EntireColumn.ClearOutline
  CurrCol = Application.Match(CLng(Date), Rows(1))
  If CurrCol > 3 Then Columns(1).Resize(, CurrCol - 3).Group
  If CurrCol < 46 Then Columns(CurrCol + 6).Resize(, 53 - CurrCol - 6).Group
End Sub

Fabulous - thanks, Peter - that works well; just need to work out now how to apply it to certain columns (say from G onwards) rather than to every column in the sheet. Much appreciated!
 
Upvote 0
Fabulous - thanks, Peter - that works well; just need to work out now how to apply it to certain columns (say from G onwards) rather than to every column in the sheet. Much appreciated!
You're welcome. Post back with more details if you can't make the modification required.
 
Upvote 0
You're welcome. Post back with more details if you can't make the modification required.

for anyone who spots Peter's great solution, if your column heading isn't in row 1, you can change the number in brackets to group by content of the correct row.
 
Upvote 0
for anyone who spots Peter's great solution, if your column heading isn't in row 1, you can change the number in brackets to group by content of the correct row.
To clarify, since there is more than one, that would be the red number in brackets, not the blue number in brackets. ;)

Rich (BB code):
Sub Group_Weeks()
  Dim CurrCol As Long
  
  ActiveSheet.UsedRange.EntireColumn.ClearOutline
  CurrCol = Application.Match(CLng(Date), Rows(1))
  If CurrCol > 3 Then Columns(1).Resize(, CurrCol - 3).Group
  If CurrCol < 46 Then Columns(CurrCol + 6).Resize(, 53 - CurrCol - 6).Group
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,580
Messages
6,125,652
Members
449,245
Latest member
PatrickL

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