Grouping data to M-F workweeks.

jaahaa

New Member
Joined
Dec 27, 2023
Messages
1
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi,

The formula below is working, it groups data Monday to Friday workweeks. It separates the workweek by adding 3 empty rows after the
Friday. However, if there is multiple rows with a Friday date the formula adds 3 rows after each Friday. Is there a way to add the 3 empty rows on the last Friday row? Hope this makes sense.

VBA Code:
Sub aaaaSeparateWorkweeks()

Dim ws As Worksheet

Set ws = ActiveSheet



' Start from the last row and move upwards; this avoids rechecking rows after insertion

Dim lastRow As Long

lastRow = ws.Cells(ws.Rows.Count, "L").End(xlUp).row



Dim i As Long

For i = lastRow To 2 Step -1 ' Assuming the first row might contain headers

If IsDate(ws.Cells(i, "L").Value) Then

' Check if the day is a Friday (Weekday function returns 6 for Friday)

If Weekday(ws.Cells(i, "L").Value) = 6 Then

ws.Rows(i + 1).Resize(3).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

End If

End If

Next i



Application.CutCopyMode = False

MsgBox "Workweeks separated with 3 empty rows."

End Sub
 

Attachments

  • unnamed.png
    unnamed.png
    11 KB · Views: 8
Last edited by a moderator:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Check if row above (i-1) is empty before adding/jumping
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,970
Members
449,095
Latest member
Mr Hughes

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