Sum values and insert new row based on conditions

swkbcf23

New Member
Joined
Sep 19, 2023
Messages
2
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello all,

I have the following table of data that I would like to transform with a macro. Overall I'd like to sum the values in column F by date (column A). And insert a new line with that total after the last summed value for each date.

Due DateProd. Order No.IteP No.DescriptionQuantityHours
9/11/2023P101899100000Fake Sku 17321.22
9/11/2023P101900100001Fake Sku 24080.76
9/11/2023P101901100002Fake Sku 33,3006.11
9/11/2023P101902100003Fake Sku 45,3985.62
9/11/2023P101903X100004Fake Sku 59,900
9/11/2023P101904X100005Fake Sku 6450
9/12/2023P101905100006Fake Sku 13000.56
9/12/2023P101906100007Fake Sku 22,3704.39
9/12/2023P101907100008Fake Sku 32,5112.62
9/12/2023P101908100009Fake Sku 43,0243.15
9/12/2023P101909100010Fake Sku 51610.67
9/12/2023P101910X100011Fake Sku 66,750
9/12/2023P101911X100012Fake Sku 73,600
9/13/2023P101912100013Fake Sku 102,8055.19
9/13/2023P101913100014Fake Sku 115,4125.64
9/13/2023P101914100015Fake Sku 12250.1
9/13/2023P101915100016Fake Sku 132070.86
9/13/2023P101916X100017Fake Sku 142,250
9/13/2023P101917X100018Fake Sku 151,350
9/13/2023P101918X100019Fake Sku 16150
9/13/2023P101919X100020Fake Sku 176,600

Again, I would like to transform that data by summing the values in column F by date in column A. I want to insert this sum result underneath the last instance of data in column F that is summed for each date. In otherwords, I'd like the new row to be inserted before the first instance of an item number (in Column C) starting with "X" for each date. I also want to add the date to the newly inserted row in column A and the word "Total" to column E. The result would look like this:

Due DateProd. Order No.IteP No.DescriptionQuantityHours
9/11/2023P101899100000Fake Sku 17321.22
9/11/2023P101900100001Fake Sku 24080.76
9/11/2023P101901100002Fake Sku 33,3006.11
9/11/2023P101902100003Fake Sku 45,3985.62
9/11/2023Total13.71
9/11/2023P101903X100004Fake Sku 59,900
9/11/2023P101904X100005Fake Sku 6450
9/12/2023P101905100006Fake Sku 13000.56
9/12/2023P101906100007Fake Sku 22,3704.39
9/12/2023P101907100008Fake Sku 32,5112.62
9/12/2023P101908100009Fake Sku 43,0243.15
9/12/2023P101909100010Fake Sku 51610.67
9/12/2023Total11.39
9/12/2023P101910X100011Fake Sku 66,750
9/12/2023P101911X100012Fake Sku 73,600
9/13/2023P101912100013Fake Sku 102,8055.19
9/13/2023P101913100014Fake Sku 115,4125.64
9/13/2023P101914100015Fake Sku 12250.1
9/13/2023P101915100016Fake Sku 132070.86
9/13/2023Total11.79
9/13/2023P101916X100017Fake Sku 142,250
9/13/2023P101917X100018Fake Sku 151,350
9/13/2023P101918X100019Fake Sku 16150
9/13/2023P101919X100020Fake Sku 176,600

I have tried using the following code but it is not working correctly; it works for the first date but then just keeps inserting blank rows instead of going through the other data.


VBA Code:
Sub InsertTotalRows()
    Dim LastRow As Long
    Dim CurrentDate As Date
    Dim TotalHours As Double
    Dim ws As Worksheet
    Dim i As Long
    
    
    Set ws = ActiveWorkbook.Sheets("Sheet1")
    
    ' Initialize variables
    LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    CurrentDate = ws.Cells(2, 1).Value
    TotalHours = 0
    
    ' Loop through rows
    For i = 2 To LastRow
        If ws.Cells(i, 1).Value = CurrentDate And ws.Cells(i, 6).Value <> "" Then
            ' Add the hours to the total
            TotalHours = TotalHours + ws.Cells(i, 6).Value
        Else
            ' Insert a new row and add the total
            ws.Rows(i).Insert Shift:=xlDown
            ws.Cells(i, 1).Value = CurrentDate
            ws.Cells(i, 5).Value = "Total"
            ws.Cells(i, 6).Value = TotalHours
            
            ' Reset variables
            CurrentDate = ws.Cells(i, 1).Value
            TotalHours = 0
        End If
    Next i
    
    ' Insert the last total row
    ws.Rows(LastRow + 1).Insert Shift:=xlDown
    ws.Cells(LastRow + 1, 1).Value = CurrentDate
    ws.Cells(LastRow + 1, 5).Value = "Total"
    ws.Cells(LastRow + 1, 6).Value = TotalHours
End Sub

Would anyone be able to help me achieve this and/or troubleshoot my existing code (also open to new code)? I thought it would be simple at first but it's proving to be above my abilities at the moment. Thank you.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Please try the following on a copy of your worksheet (change worksheet name to suit)
VBA Code:
Option Explicit
Sub swkbcf23()
    Application.ScreenUpdating = False
    Const t As String = "Total"
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")      '<-- *** Change to actual sheet name ***
    Dim LRow As Long, i As Long
    LRow = ws.Cells(Rows.Count, "C").End(xlUp).Row
    For i = LRow To 2 Step -1
        If ws.Cells(i, 3) Like "X*" And Not ws.Cells(i - 1, 3) Like "X*" Then
            With ws.Rows(i)
                .EntireRow.Insert
                With .Offset(-1)
                    .Font.Bold = True
                    .Cells(1, 5) = t
                    .Cells(1, 6) = Application.Sum(Range(.Cells(1, 6), .Cells(1, 6).Offset(-1).End(xlUp)))
                End With
            End With
        End If
    Next i
    Application.ScreenUpdating = True
End Sub

Before:
Book1
ABCDEF
1Due DateProd. Order No.IteP No.DescriptionQuantityHours
29/11/2023P101899100000Fake Sku 17321.22
39/11/2023P101900100001Fake Sku 24080.76
49/11/2023P101901100002Fake Sku 33,3006.11
59/11/2023P101902100003Fake Sku 45,3985.62
69/11/2023P101903X100004Fake Sku 59,900
79/11/2023P101904X100005Fake Sku 6450
8
99/12/2023P101905100006Fake Sku 13000.56
109/12/2023P101906100007Fake Sku 22,3704.39
119/12/2023P101907100008Fake Sku 32,5112.62
129/12/2023P101908100009Fake Sku 43,0243.15
139/12/2023P101909100010Fake Sku 51610.67
149/12/2023P101910X100011Fake Sku 66,750
159/12/2023P101911X100012Fake Sku 73,600
16
179/13/2023P101912100013Fake Sku 102,8055.19
189/13/2023P101913100014Fake Sku 115,4125.64
199/13/2023P101914100015Fake Sku 12250.1
209/13/2023P101915100016Fake Sku 132070.86
219/13/2023P101916X100017Fake Sku 142,250
229/13/2023P101917X100018Fake Sku 151,350
239/13/2023P101918X100019Fake Sku 16150
249/13/2023P101919X100020Fake Sku 176,600
Sheet1


After:
Book1
ABCDEF
1Due DateProd. Order No.IteP No.DescriptionQuantityHours
29/11/2023P101899100000Fake Sku 17321.22
39/11/2023P101900100001Fake Sku 24080.76
49/11/2023P101901100002Fake Sku 33,3006.11
59/11/2023P101902100003Fake Sku 45,3985.62
6Total13.71
79/11/2023P101903X100004Fake Sku 59,900
89/11/2023P101904X100005Fake Sku 6450
9
109/12/2023P101905100006Fake Sku 13000.56
119/12/2023P101906100007Fake Sku 22,3704.39
129/12/2023P101907100008Fake Sku 32,5112.62
139/12/2023P101908100009Fake Sku 43,0243.15
149/12/2023P101909100010Fake Sku 51610.67
15Total11.39
169/12/2023P101910X100011Fake Sku 66,750
179/12/2023P101911X100012Fake Sku 73,600
18
199/13/2023P101912100013Fake Sku 102,8055.19
209/13/2023P101913100014Fake Sku 115,4125.64
219/13/2023P101914100015Fake Sku 12250.1
229/13/2023P101915100016Fake Sku 132070.86
23Total11.79
249/13/2023P101916X100017Fake Sku 142,250
259/13/2023P101917X100018Fake Sku 151,350
269/13/2023P101918X100019Fake Sku 16150
279/13/2023P101919X100020Fake Sku 176,600
Sheet1
 
Upvote 0
Solution

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

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