Grouping Columns based on content

scotthannaford1973

Board Regular
Joined
Sep 27, 2017
Messages
115
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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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