Simply-fly Subtotal and add some condition

sachin483

Board Regular
Joined
Mar 31, 2015
Messages
157
Office Version
  1. 2019
Platform
  1. Windows
how to simply-fly this macro

i have recorded the macro while doing subtotal i want to simply-fly the process


1.Sorting should not be of specific range , it should be for full column range rows in data can increase or decrease ,column sorting order is ok .

2. There are 3 subtotal - for 1st subtotal only Grand Total must be there in the end for other 2 not required - how to remove

3. Color for all the 3 subtotal can be done

4.Subtotal is not delete after last subtotal


Code:
Sub subtotal()
'
' subtotal Macro
'

'
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("12").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("12").Sort.SortFields.Add Key:=Range("B2:B80"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("12").Sort.SortFields.Add Key:=Range("D2:D80"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("12").Sort.SortFields.Add Key:=Range("F2:F80"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("12").Sort.SortFields.Add Key:=Range("H2:H80"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("12").Sort
        .SetRange Range("A1:W80")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.subtotal GroupBy:=3, Function:=xlSum, TotalList:=Array(10, 11, 12 _
        , 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23), Replace:=True, PageBreaks:=False, _
        SummaryBelowData:=True
    Selection.Copy
    Application.Run "ASAPRunProc268"
    Application.CutCopyMode = False
    Selection.RemoveSubtotal
    Selection.subtotal GroupBy:=5, Function:=xlSum, TotalList:=Array(10, 11, 12 _
        , 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23), Replace:=True, PageBreaks:=False, _
        SummaryBelowData:=True
    Selection.Copy
    Application.Run "ASAPRunProc268"
    Application.CutCopyMode = False
    Selection.RemoveSubtotal
    Selection.subtotal GroupBy:=7, Function:=xlSum, TotalList:=Array(10, 11, 12 _
        , 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23), Replace:=True, PageBreaks:=False, _
        SummaryBelowData:=True
    Selection.Copy
    Application.Run "ASAPRunProc268"
    Application.CutCopyMode = False
    Selection.RemoveSubtotal
    Range("A1").Select
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I think it would be easier for others to help you out if you just gave us a list of actions you wanted a macro for. For example:

make a macro that does this
1. Sort Columns A:W in ascending order buy column B
2. Sort Columns A:W in ascending order by column D
3. Sort Columns A:W in ascending order by column F
4. Sort Columns A:W in ascending order by column H
5. In column A indicate a subtotal at the bottom of the sheet
6. In column B indicate a subtotal at the bottom of the sheet
7. In column A indicate a subtotal at the bottom of the sheet
8. Color the Subtotal cells yellow

I don't think this is what you want. But something along these lines will help us formulate a Macro to best suit your needs.

Sincerely,
Max
 
Upvote 0
please find enclosed sample for which i do sorting and subtotal in data sheet and the result sheet, After sorting and subtotal i colored all the totals and then concatenate with some character for reference purpose and put them in column number (I) then do the paste special

basic data

SORTING 1SUBTOTAL 1SORTING 2SUBTOTAL 2SORTING 3SUBTOTAL 3SORTING 4
DDZCDZDSCRRCDRDSCRACDADSCRTCDTDSCRNMRAPRMAYJUNJULAUGSEPOCTNOVDECJANFEBMARTOTAL
XXXX01DUMMY1BRX18DUMMY8BX01DUMMY1XX301DUMMY110204.58193.27195.53197.80200.06206.85195.53206.85206.85202.32204.58195.532409.76
XXXX01DUMMY1BRX18DUMMY8BX02DUMMY2XX302DUMMY2128.5827.6227.8128.0028.2028.7727.8128.7728.7728.3928.5827.84339.12
XXXX01DUMMY1BRX22DUMMY12BX03DUMMY3XX303DUMMY3232.4331.2431.4831.7231.9532.6731.4832.6732.6732.1932.4331.49384.43
XXXX01DUMMY1BRX22DUMMY12BX03DUMMY3XX304DUMMY4233.2532.0032.2632.5032.7433.4932.2633.4933.4932.9933.2532.25393.97
XXXX01DUMMY1BRX22DUMMY12BX150DUMMY16XX305DUMMY5225.4824.7124.8625.0125.1725.6424.8625.6425.6425.3325.4824.86302.69
XXXX01DUMMY1BRX22DUMMY12BX150DUMMY16XX306DUMMY6122.2621.6721.7921.9022.0222.3721.7922.3722.3722.1422.2521.77264.72
XXXX01DUMMY1BRX22DUMMY12BX04DUMMY4XX307DUMMY7119.6919.2519.3419.4219.5119.7719.3419.7719.7719.6019.6919.35234.50
XXXX01DUMMY1BRX22DUMMY12BX04DUMMY4XX308DUMMY8349.2947.1147.5447.9848.4249.7347.5449.7349.7348.8549.2947.53582.74
XXXX01DUMMY1BRX13DUMMY3BX05DUMMY5XX309DUMMY9133.5632.3032.5532.8133.0533.8132.5533.8133.8133.3133.5632.58397.67
XXXX01DUMMY1BRX13DUMMY3BX05DUMMY5XX310DUMMY10237.9236.4136.7137.0237.3238.2236.7138.2238.2237.6237.9236.73449.03
XXXX01DUMMY1BRX13DUMMY3BX05DUMMY5XX311DUMMY11125.5724.7924.9425.1025.2625.7324.9425.7325.7325.4225.5724.96303.74
XXXX01DUMMY1BRX13DUMMY3BX05DUMMY5XX312DUMMY12243.1741.3541.7142.0842.4443.5341.7143.5343.5342.8043.1741.70510.71
XXXX01DUMMY1BRX13DUMMY3BX06DUMMY6XX313DUMMY13122.8022.1722.3022.4222.5522.9222.3022.9222.9222.6822.8022.32271.08
XXXX01DUMMY1BRX13DUMMY3BX06DUMMY6XX314DUMMY14231.4730.3430.5630.7931.0131.6930.5631.6931.6931.2431.4730.56373.08
XXXX02DUMMY2BRX19DUMMY9BX07DUMMY7XX315DUMMY15122.1721.5921.7021.8221.9322.2921.7022.2922.2922.0522.1721.68263.69

<colgroup><col><col><col><col><col><col><col><col><col><col><col span="12"><col></colgroup><tbody>
</tbody>


result data

DDZCDZDSCRRCDRDSCRACDADSCRTCDTDSCRNMRAPRMAYJUNJULAUGSEPOCTNOVDECJANFEBMARTOTAL
XXXX01DUMMY1BRX13DUMMY3BX05DUMMY5XX309DUMMY9133.5632.3032.5532.8133.0533.8132.5533.8133.8133.3133.5632.58397.67
XXXX01DUMMY1BRX13DUMMY3BX05DUMMY5XX310DUMMY10237.9236.4136.7137.0237.3238.2236.7138.2238.2237.6237.9236.73449.03
XXXX01DUMMY1BRX13DUMMY3BX05DUMMY5XX311DUMMY11125.5724.7924.9425.1025.2625.7324.9425.7325.7325.4225.5724.96303.74
XXXX01DUMMY1BRX13DUMMY3BX05DUMMY5XX312DUMMY12243.1741.3541.7142.0842.4443.5341.7143.5343.5342.8043.1741.70510.71
XXDUMMY5 Total AR_DUMMY5 Total6140.22134.85135.91137.00138.07141.29135.91141.29141.29139.14140.21135.971661.15
XXXX01DUMMY1BRX13DUMMY3BX06DUMMY6XX313DUMMY13122.8022.1722.3022.4222.5522.9222.3022.9222.9222.6822.8022.32271.08
XXXX01DUMMY1BRX13DUMMY3BX06DUMMY6XX314DUMMY14231.4730.3430.5630.7931.0131.6930.5631.6931.6931.2431.4730.56373.08
XXXX01DUMMY1BRX13DUMMY3BX06DUMMY6XX404DUMMY104118.7718.3918.4618.5418.6218.8518.4618.8518.8518.6918.7718.49223.72
XXDUMMY6 Total AR_DUMMY6 Total473.0470.9071.3271.7572.1873.4571.3273.4573.4572.6173.0371.37867.88
XXDUMMY3 Total RE_DUMMY3 Total10213.25205.75207.23208.75210.25214.74207.23214.74214.74211.75213.25207.342529.02
XXXX01DUMMY1BRX18DUMMY8BX01DUMMY1XX301DUMMY110204.58193.27195.53197.80200.06206.85195.53206.85206.85202.32204.58195.532409.76
XXDUMMY1 Total AR_DUMMY1 Total10204.58193.27195.53197.80200.06206.85195.53206.85206.85202.32204.58195.532409.76
XXXX01DUMMY1BRX18DUMMY8BX02DUMMY2XX302DUMMY2128.5827.6227.8128.0028.2028.7727.8128.7728.7728.3928.5827.84339.12
XXDUMMY2 Total AR_DUMMY2 Total128.5827.6227.8128.0028.2028.7727.8128.7728.7728.3928.5827.84339.12
XXXX01DUMMY1BRX18DUMMY8BX45DUMMY45XX413DUMMY110371.8168.3069.0069.7070.4072.5069.0072.5072.5071.1071.8169.00847.62
XXDUMMY45 Total AR_DUMMY45 Total371.8168.3069.0069.7070.4072.5069.0072.5072.5071.1071.8169.00847.62
XXXX01DUMMY1BRX18DUMMY8BX46DUMMY46XX414DUMMY111365.6362.4863.1163.7464.3766.2563.1166.2566.2565.0065.6363.12774.95
XXDUMMY46 Total AR_DUMMY46 Total365.6362.4863.1163.7464.3766.2563.1166.2566.2565.0065.6363.12774.95
XXDUMMY8 Total RE_DUMMY8 Total17370.59351.67355.46359.24363.02374.38355.46374.38374.38366.80370.59355.504371.45
XXXX01DUMMY1BRX22DUMMY12BX03DUMMY3XX303DUMMY3232.4331.2431.4831.7231.9532.6731.4832.6732.6732.1932.4331.49384.43
XXXX01DUMMY1BRX22DUMMY12BX03DUMMY3XX304DUMMY4233.2532.0032.2632.5032.7433.4932.2633.4933.4932.9933.2532.25393.97
XXXX01DUMMY1BRX22DUMMY12BX03DUMMY3XX421DUMMY118117.4817.1717.2417.3017.3617.5417.2417.5417.5417.4217.4817.25208.56
XXDUMMY3 Total AR_DUMMY3 Total583.1680.4280.9781.5282.0683.7080.9783.7083.7082.6083.1680.99986.95
XXXX01DUMMY1BRX22DUMMY12BX04DUMMY4XX307DUMMY7119.6919.2519.3419.4219.5119.7719.3419.7719.7719.6019.6919.35234.50
XXXX01DUMMY1BRX22DUMMY12BX04DUMMY4XX308DUMMY8349.2947.1147.5447.9848.4249.7347.5449.7349.7348.8549.2947.53582.74
XXDUMMY4 Total AR_DUMMY4 Total468.9766.3666.8867.4167.9369.5066.8869.5069.5068.4568.9766.88817.25
XXXX01DUMMY1BRX22DUMMY12BX150DUMMY16XX305DUMMY5225.4824.7124.8625.0125.1725.6424.8625.6425.6425.3325.4824.86302.69
XXXX01DUMMY1BRX22DUMMY12BX150DUMMY16XX306DUMMY6122.2621.6721.7921.9022.0222.3721.7922.3722.3722.1422.2521.77264.72
XXXX01DUMMY1BRX22DUMMY12BX150DUMMY16XX395DUMMY95121.0820.5620.6620.7620.8721.1820.6621.1821.1820.9721.0820.68250.85
XXDUMMY16 Total AR_DUMMY16 Total468.8266.9367.3167.6868.0669.2067.3169.2069.2068.4468.8167.32818.26
XXDUMMY12 Total RE_DUMMY12 Total13220.95213.71215.16216.60218.05222.40215.16222.40222.40219.50220.94215.192622.46
XX
DUMMY1 Total ZR_DUMMY1 Total40804.79771.12777.85784.59791.32811.52777.85811.52811.52798.05804.78778.029522.93

<colgroup><col><col><col><col><col><col><col><col><col><col><col span="12"><col></colgroup><tbody>
</tbody>
 
Upvote 0
Run this, assuming your column headers are in row 1 and not row 2:
Code:
Sub sachin483()
Dim lrow As Long
Dim i As Long
Dim Sub1 As String
Dim Sub2 As String
Dim Sub3 As String
Dim ORow1 As Long
Dim ORow2 As Long
Dim ORow3 As Long


lrow = Cells(Rows.Count, 1).End(xlUp).Row

Range("H2:H" & lrow).Sort Key1:=Range("H2"), Order1:=xlAscending, Header:=xlYes
Range("F2:F" & lrow).Sort Key1:=Range("F2"), Order1:=xlAscending, Header:=xlYes
Range("D2:D" & lrow).Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlYes
Range("B2:B" & lrow).Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlYes
ORow1 = 0

'Subtotal 1
For i = Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
    Sub1 = Cells(i + 1, 7)
    If i = lrow Then Sub1 = Cells(i, 7)
    If Cells(i + 1, 7) = "" Then Sub1 = Cells(i, 7)
    
    If Cells(i - 1, 7) = Sub1 Then
        GoTo Nexti1
    Else

        If ORow1 = 0 Then ORow1 = lrow + 2
    

        Cells(i, 1).EntireRow.Insert
        
        
        Cells(ORow1, 1) = Cells(i - 1, 1)
        Cells(ORow1, 7) = Sub1 & " Total"
        Cells(ORow1, 9) = "AR_" & Sub1 & " Total"
    
        Cells(ORow1, 10) = WorksheetFunction.Sum(Range(Cells(ORow1 - 1, 10), Cells(i + 1, 10)))
        Cells(ORow1, 11) = WorksheetFunction.Sum(Range(Cells(ORow1 - 1, 11), Cells(i + 1, 11)))
        Cells(ORow1, 12) = WorksheetFunction.Sum(Range(Cells(ORow1 - 1, 12), Cells(i + 1, 12)))
        Cells(ORow1, 13) = WorksheetFunction.Sum(Range(Cells(ORow1 - 1, 13), Cells(i + 1, 13)))
        Cells(ORow1, 14) = WorksheetFunction.Sum(Range(Cells(ORow1 - 1, 14), Cells(i + 1, 14)))
        Cells(ORow1, 15) = WorksheetFunction.Sum(Range(Cells(ORow1 - 1, 15), Cells(i + 1, 15)))
        Cells(ORow1, 16) = WorksheetFunction.Sum(Range(Cells(ORow1 - 1, 16), Cells(i + 1, 16)))
        Cells(ORow1, 17) = WorksheetFunction.Sum(Range(Cells(ORow1 - 1, 17), Cells(i + 1, 17)))
        Cells(ORow1, 18) = WorksheetFunction.Sum(Range(Cells(ORow1 - 1, 18), Cells(i + 1, 18)))
        Cells(ORow1, 19) = WorksheetFunction.Sum(Range(Cells(ORow1 - 1, 19), Cells(i + 1, 19)))
        Cells(ORow1, 20) = WorksheetFunction.Sum(Range(Cells(ORow1 - 1, 20), Cells(i + 1, 20)))
        Cells(ORow1, 21) = WorksheetFunction.Sum(Range(Cells(ORow1 - 1, 21), Cells(i + 1, 21)))
        Cells(ORow1, 22) = WorksheetFunction.Sum(Range(Cells(ORow1 - 1, 22), Cells(i + 1, 22)))
        Cells(ORow1, 23) = WorksheetFunction.Sum(Range(Cells(ORow1 - 1, 23), Cells(i + 1, 23)))
    
        Range(Cells(ORow1, 7), Cells(ORow1, 23)).Interior.ColorIndex = 6
        Cells(ORow1, 7).Font.Bold = True
        
        ORow1 = i + 1
        
        
        If i = 2 Then Rows(2).Delete
        If i = 2 Then Cells(1, 2).End(xlDown).Offset(1, -1) = "XX"
    End If
   
Nexti1:
Next i

'Subtotal 2
lrow = Cells(Rows.Count, 7).End(xlUp).Row

Sub2 = Cells(lrow - 1, 5)
ORow2 = lrow + 1

For i = lrow To 1 Step -1
    If Cells(i, 5) <> "" And Cells(i, 5) <> Sub2 Then
        If i = 1 Then Rows(1).Insert
        If i = 1 Then ORow2 = ORow2 + 1
        
        Cells(ORow2, 1) = "XX"
        Cells(ORow2, 5) = Sub2 & " Total"
        Cells(ORow2, 9) = "RE_" & Sub2 & " Total"
            
         Cells(ORow2, 10) = WorksheetFunction.SumIf(Range(Cells(i + 2, 5),  Cells(ORow2 - 2, 5)), Sub2, Range(Cells(i + 2, 10), Cells(ORow2 - 2,  10)))
        Cells(ORow2, 11) =  WorksheetFunction.SumIf(Range(Cells(i + 2, 5), Cells(ORow2 - 2, 5)),  Sub2, Range(Cells(i + 2, 11), Cells(ORow2 - 2, 11)))
         Cells(ORow2, 12) = WorksheetFunction.SumIf(Range(Cells(i + 2, 5),  Cells(ORow2 - 2, 5)), Sub2, Range(Cells(i + 2, 12), Cells(ORow2 - 2,  12)))
        Cells(ORow2, 13) =  WorksheetFunction.SumIf(Range(Cells(i + 2, 5), Cells(ORow2 - 2, 5)),  Sub2, Range(Cells(i + 2, 13), Cells(ORow2 - 2, 13)))
         Cells(ORow2, 14) = WorksheetFunction.SumIf(Range(Cells(i + 2, 5),  Cells(ORow2 - 2, 5)), Sub2, Range(Cells(i + 2, 14), Cells(ORow2 - 2,  14)))
        Cells(ORow2, 15) =  WorksheetFunction.SumIf(Range(Cells(i + 2, 5), Cells(ORow2 - 2, 5)),  Sub2, Range(Cells(i + 2, 15), Cells(ORow2 - 2, 15)))
         Cells(ORow2, 16) = WorksheetFunction.SumIf(Range(Cells(i + 2, 5),  Cells(ORow2 - 2, 5)), Sub2, Range(Cells(i + 2, 16), Cells(ORow2 - 2,  16)))
        Cells(ORow2, 17) =  WorksheetFunction.SumIf(Range(Cells(i + 2, 5), Cells(ORow2 - 2, 5)),  Sub2, Range(Cells(i + 2, 17), Cells(ORow2 - 2, 17)))
         Cells(ORow2, 18) = WorksheetFunction.SumIf(Range(Cells(i + 2, 5),  Cells(ORow2 - 2, 5)), Sub2, Range(Cells(i + 2, 18), Cells(ORow2 - 2,  18)))
        Cells(ORow2, 19) =  WorksheetFunction.SumIf(Range(Cells(i + 2, 5), Cells(ORow2 - 2, 5)),  Sub2, Range(Cells(i + 2, 19), Cells(ORow2 - 2, 19)))
         Cells(ORow2, 20) = WorksheetFunction.SumIf(Range(Cells(i + 2, 5),  Cells(ORow2 - 2, 5)), Sub2, Range(Cells(i + 2, 20), Cells(ORow2 - 2,  20)))
        Cells(ORow2, 21) =  WorksheetFunction.SumIf(Range(Cells(i + 2, 5), Cells(ORow2 - 2, 5)),  Sub2, Range(Cells(i + 2, 21), Cells(ORow2 - 2, 21)))
         Cells(ORow2, 22) = WorksheetFunction.SumIf(Range(Cells(i + 2, 5),  Cells(ORow2 - 2, 5)), Sub2, Range(Cells(i + 2, 22), Cells(ORow2 - 2,  22)))
        Cells(ORow2, 23) =  WorksheetFunction.SumIf(Range(Cells(i + 2, 5), Cells(ORow2 - 2, 5)),  Sub2, Range(Cells(i + 2, 23), Cells(ORow2 - 2, 23)))
            
        Range(Cells(ORow2, 5), Cells(ORow2, 23)).Interior.ColorIndex = 17
            
            
            
        If i = 1 Then
            Cells(ORow2, 5).Font.Bold = True
            Rows(1).Delete
        Else
            Cells(ORow2, 5).Font.Bold = True
            Cells(i + 2, 1).EntireRow.Insert
        End If
        
        Sub2 = Cells(i, 5)
        ORow2 = i + 2
        
        
        
    End If
Next i



lrow = Cells(Rows.Count, 5).End(xlUp).Row

Sub3 = Cells(lrow - 2, 3)
ORow3 = lrow + 1

For i = lrow To 1 Step -1
    If Cells(i, 3) <> "" And Cells(i, 3) <> Sub3 Then
        
            If i = 1 Then Rows(1).Insert
            If i = 1 Then ORow3 = ORow3 + 1
        
            Cells(ORow3, 1) = "XX"
            Cells(ORow3, 3) = Sub3 & " Total"
            Cells(ORow3, 9) = "ZR_" & Sub3 & " Total"
            
             Cells(ORow3, 10) = WorksheetFunction.SumIf(Range(Cells(i + 2, 3),  Cells(ORow3 - 2, 3)), Sub3, Range(Cells(i + 2, 10), Cells(ORow3 - 2,  10)))
            Cells(ORow3, 11) =  WorksheetFunction.SumIf(Range(Cells(i + 2, 3), Cells(ORow3 - 2, 3)),  Sub3, Range(Cells(i + 2, 11), Cells(ORow3 - 2, 11)))
             Cells(ORow3, 12) = WorksheetFunction.SumIf(Range(Cells(i + 2, 3),  Cells(ORow3 - 2, 3)), Sub3, Range(Cells(i + 2, 12), Cells(ORow3 - 2,  12)))
            Cells(ORow3, 13) =  WorksheetFunction.SumIf(Range(Cells(i + 2, 3), Cells(ORow3 - 2, 3)),  Sub3, Range(Cells(i + 2, 13), Cells(ORow3 - 2, 13)))
             Cells(ORow3, 14) = WorksheetFunction.SumIf(Range(Cells(i + 2, 3),  Cells(ORow3 - 2, 3)), Sub3, Range(Cells(i + 2, 14), Cells(ORow3 - 2,  14)))
            Cells(ORow3, 15) =  WorksheetFunction.SumIf(Range(Cells(i + 2, 3), Cells(ORow3 - 2, 3)),  Sub3, Range(Cells(i + 2, 15), Cells(ORow3 - 2, 15)))
             Cells(ORow3, 16) = WorksheetFunction.SumIf(Range(Cells(i + 2, 3),  Cells(ORow3 - 2, 3)), Sub3, Range(Cells(i + 2, 16), Cells(ORow3 - 2,  16)))
            Cells(ORow3, 17) =  WorksheetFunction.SumIf(Range(Cells(i + 2, 3), Cells(ORow3 - 2, 3)),  Sub3, Range(Cells(i + 2, 17), Cells(ORow3 - 2, 17)))
             Cells(ORow3, 18) = WorksheetFunction.SumIf(Range(Cells(i + 2, 3),  Cells(ORow3 - 2, 3)), Sub3, Range(Cells(i + 2, 18), Cells(ORow3 - 2,  18)))
            Cells(ORow3, 19) =  WorksheetFunction.SumIf(Range(Cells(i + 2, 3), Cells(ORow3 - 2, 3)),  Sub3, Range(Cells(i + 2, 19), Cells(ORow3 - 2, 19)))
             Cells(ORow3, 20) = WorksheetFunction.SumIf(Range(Cells(i + 2, 3),  Cells(ORow3 - 2, 3)), Sub3, Range(Cells(i + 2, 20), Cells(ORow3 - 2,  20)))
            Cells(ORow3, 21) =  WorksheetFunction.SumIf(Range(Cells(i + 2, 3), Cells(ORow3 - 2, 3)),  Sub3, Range(Cells(i + 2, 21), Cells(ORow3 - 2, 21)))
             Cells(ORow3, 22) = WorksheetFunction.SumIf(Range(Cells(i + 2, 3),  Cells(ORow3 - 2, 3)), Sub3, Range(Cells(i + 2, 22), Cells(ORow3 - 2,  22)))
            Cells(ORow3, 23) =  WorksheetFunction.SumIf(Range(Cells(i + 2, 3), Cells(ORow3 - 2, 3)),  Sub3, Range(Cells(i + 2, 23), Cells(ORow3 - 2, 23)))
            
            Range(Cells(ORow3, 3), Cells(ORow3, 23)).Interior.ColorIndex = 40
            Cells(ORow3, 3).Font.Bold = True
            
            
            
        If i = 1 Then
        Rows(1).Delete
        Else
        Cells(i + 3, 1).EntireRow.Insert
        End If
        
        Sub3 = Cells(i, 3)
        ORow3 = i + 3
        
    End If

Next i



End Sub

I can't really think of a better way to do this outside of writing 3 separate loops. Which means that this could take a while to run depending on how much data you have. But I am able to recreate your 'result' tab that you provided from the data. (Just remember to delete the first row)

Please let me know if this worked!

Sincerely,
Max
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,384
Members
448,956
Latest member
JPav

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