VBA Code to add in blank rows at a specific point, then do a formula in that blank row

lichldo

Board Regular
Joined
Apr 19, 2022
Messages
65
Office Version
  1. 365
Platform
  1. MacOS
Hello - I'm looking for some help to create a VBA code macro. I have a report that I can export from our project/finance system that I need to manipulate a bit. This is a sample of what the raw data looks like (have of course modified some of the data for privacy) -
Dataviewer (75).csv
ABCDEFGHIJKLMNOPQRS
1CompanyOpp StatusClient/ProspectClientJobJob descriptionOpp WeightingClient LeadJob LeadPhase LeadAccount DirectorProject Director23-Jul23-Aug23-Sep23-Oct23-Nov23-DecTotal
2UKStatusClientClient Name1/00112120Job Decription75%xxxName 1xxxxxxxxxxxx8000150001000080003000250046500
3UKStatusClientClient Name1/00111891Job Decription100%xxxName 1xxxxxxxxxxxx40004000400000012000
4UKStatusClientClient Name1/00111645Job Decription100%xxxName 2xxxxxxxxxxxx5000000005000
5UKStatusClientClient Name1/00111951Job Decription100%xxxName 2xxxxxxxxxxxx5000000005000
6UKStatusClientClient Name1/00112020Job Decription100%xxxName 2xxxxxxxxxxxx3000000003000
7UKStatusClientClient Name1/00112240Job Decription25%xxxName 3xxxxxxxxxxxx00350003500025000095000
8UKStatusClientClient Name1/00112099Job Decription50%xxxName 3xxxxxxxxxxxx080001500050000500000123000
9UKStatusClientClient Name1/00112121Job Decription75%xxxName 3xxxxxxxxxxxx100012000500000018000
10UKStatusClientClient Name1/00112122Job Decription75%xxxName 3xxxxxxxxxxxx010001200050000018000
11UKStatusClientClient Name1/00111830Job Decription100%xxxName 3xxxxxxxxxxxx147440000014744
12UKStatusClientClient Name1/00111912Job Decription100%xxxName 3xxxxxxxxxxxx15000350003500020000125007000124500
13UKStatusClientClient Name1/00112231Job Decription100%xxxName 3xxxxxxxxxxxx-5000-5600-5200-688000-22680
14UKStatusClientClient Name1/00112244Job Decription0%xxxName 4xxxxxxxxxxxx364187163.14000043581.14
15UKStatusClientClient Name1/00112108Job Decription25%xxxName 4xxxxxxxxxxxx22291.177327.7000029618.87
16UKStatusClientClient Name1/00112172Job Decription100%xxxName 4xxxxxxxxxxxx14796.765483.510201061.89201061.89207154.67629558.72
17UKStatusClientClient Name1/00112052Job Decription100%xxxName 4xxxxxxxxxxxx26406.1326406.1326406.1326406.1326406.1326406.13158436.78
18UKStatusClientClient Name1/00112054Job Decription100%xxxName 4xxxxxxxxxxxx26406.1326406.1326406.1300079218.39
Raw Data


I have manually done what I would like the macro to be able to do for me. I need it to do the following actions -
1. Add a blank row in-between each "new section" of column I - so everytime there is a new name in column I, add in a new blank row
2. Within that, add in a blank row between anything less than 100% in G. So we will have a range of percentages in G, anything less than 100% we consider an "opp" so we want all of those together
3. Add in another blank row after all the 100% in G - since that will be at the end of that "Section", there will be 2 blank rows (including the one added from #1 in this list)
4. Then I need to total each section in the blank row for columns M thru S - so that we have totals for all the opps and totals for all the 100% jobs
5. Then I need to total everything together (without double counting the totals we've added) to see the total amount each I section has

Here is what that looks like when I did it manually -
Dataviewer (75).csv
ABCDEFGHIJKLMNOPQRS
1CompanyOpp StatusClient/ProspectClientJobJob descriptionOpp WeightingClient LeadJob LeadPhase LeadAccount DirectorProject Director23-Jul23-Aug23-Sep23-Oct23-Nov23-DecTotal
2
3UKStatusClientClient Name1/00112120Job Description75%xxxName 1xxxxxxxxx8000150001000080003000250046500
4Opp Total8000150001000080003000250046500
5UKStatusClientClient Name1/00111891Job Description100%xxxName 1xxxxxxxxx40004000400000012000
6Live Job Total40004000400000012000
7Job Lead Total12000190001400080003000250058500
8UKStatusClientClient Name1/00111645Job Description100%xxxName 2xxxxxxxxx5000000005000
9UKStatusClientClient Name1/00111951Job Description100%xxxName 2xxxxxxxxx5000000005000
10UKStatusClientClient Name1/00112020Job Description100%xxxName 2xxxxxxxxx3000000003000
11Live Job Total130000000013000
12Job Lead Total130000000013000
13UKStatusClientClient Name1/00112240Job Description25%xxxName 3xxxxxxxxx00350003500025000095000
14UKStatusClientClient Name1/00112099Job Description50%xxxName 3xxxxxxxxx080001500050000500000123000
15UKStatusClientClient Name1/00112121Job Description75%xxxName 3xxxxxxxxx100012000500000018000
16UKStatusClientClient Name1/00112122Job Description75%xxxName 3xxxxxxxxx010001200050000018000
17Opp Total1000210006700090000750000254000
18UKStatusClientClient Name1/00111830Job Description100%xxxName 3xxxxxxxxx147440000014744
19UKStatusClientClient Name1/00111912Job Description100%xxxName 3xxxxxxxxx15000350003500020000125007000124500
20UKStatusClientClient Name1/00112231Job Description100%xxxName 3xxxxxxxxx-5000-5600-5200-688000-22680
21Live Job Total24744294002980013120125007000116564
22Job Lead Total257445040096800103120875007000370564
23UKStatusClientClient Name1/00112244Job Description0%xxxName 4xxxxxxxxx364187163.14000043581.14
24UKStatusClientClient Name1/00112108Job Description25%xxxName 4xxxxxxxxx22291.177327.7000029618.87
25Opp Total58709.1714490.84000073200.01
26UKStatusClientClient Name1/00112172Job Description100%xxxName 4xxxxxxxxx14796.765483.510201061.89201061.89207154.67629558.72
27UKStatusClientClient Name1/00112052Job Description100%xxxName 4xxxxxxxxx26406.1326406.1326406.1326406.1326406.1326406.13158436.78
28UKStatusClientClient Name1/00112054Job Description100%xxxName 4xxxxxxxxx26406.1326406.1326406.1300079218.39
29Live Job Total67609.0258295.7752812.26227468.02227468.02233560.8867213.89
30Job Lead Total126318.1972786.6152812.26227468.02227468.02233560.8940413.9
What I Want
Cell Formulas
RangeFormula
M4:S4,M6:S6M4=SUM(M3)
M7:S7M7=SUM(M3,M5)
M11:S11,M29:S29,M21:S21M11=SUM(M8:M10)
M12:S12M12=SUM(M8:M10)
M17:S17M17=SUM(M13:M16)
M22:S22M22=SUM(M13:M16,M18:M20)
M25:S25M25=SUM(M23:M24)
M30:S30M30=SUM(M23:M24,M26:M28)
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try this with a copy of your data.

VBA Code:
Sub Insert_Totals()
  Dim i As Long
  Dim rA As Range, rFound As Range
  
  Application.ScreenUpdating = False
  For i = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
    If Range("I" & i).Value <> Range("I" & i - 1).Value Then Rows(i).Insert
  Next i
  For Each rA In Range("G2", Range("G" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
    With rA
      Set rFound = .Find(What:="100%", After:=.Cells(.Count), LookAt:=xlWhole, Searchdirection:=xlNext)
      If Not rFound Is Nothing Then
        Rows(.Row + .Count).Insert
        With .Offset(.Count, 5).Resize(2, 8)
          .Rows(1).FormulaR1C1 = Replace(Replace("=sumifs(R#C:R[-1]C,R#C@:R[-1]C@,1)", "#", rA.Row), "@", rA.Column)
          .Rows(2).FormulaR1C1 = Replace(Replace("=sumifs(R#C:R[-2]C,R#C@:R[-2]C@,""<>"")", "#", rA.Row), "@", rA.Column)
          .Columns(1).Value = Application.Transpose(Array("Live Job Total", "Job Lead Total"))
          .Font.Bold = True
        End With
        If rFound.Row <> .Row Then
          rFound.EntireRow.Insert
          With Range("L" & rFound.Row - 1).Resize(, 8)
            .FormulaR1C1 = "=sum(R" & rA.Row & "C:R[-1]C)"
            .Cells(1).Value = "Opp Total"
            .Font.Bold = True
          End With
        End If
      End If
    End With
  Next rA
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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