Delete data for 20000 rows when contains words in two columns

Abdo

Board Regular
Joined
May 16, 2022
Messages
245
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hello ,
I have about 20000 rows divided multiple ranges . what I want to exclude lastrow contains TOTAL word in column A and exclude row contains FIRST DURETION word in column D for second row , sometimes not all ranges contain FIRST DURETION word .
after that should merge duplicated amounts in column E,F based on column D and sum whole columns D,E and show total as showing in column A,B to the bottom .every time add new ID in column D so will increase ID to show total in column A,B to the bottom.

copy to bottom based on cell.xlsm
ABCDEFG
1DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
201/01/2024-ABDEND1FIRST DURETION20,000.0020,000.00
302/01/2024PA-B3ABDEND1PA20,000.0040,000.00
403/01/2024SA-B35ABDEND1SA1,000.0039,000.00
5TOTAL40,000.001,000.0039,000.00
6
7
8DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
914/01/2024PA-B352ABDEND10PA140.00140.00
1015/01/2024PA-B352ABDEND10PA100.000.00
11TOTAL240.00240.000.00
12
13
14DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
1514/04/2024PA-B442ABDEND100FIRST DURETION1,040.001,040.00
1615/04/2024PA-B442ABDEND100SA1,000.0040.00
1716/04/2024PA-B442ABDEND100PA2,000.002,040.00
18TOTAL3,040.001,000.002,040.00
19
20
21DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
2230/09/2024PA-B1342ABDEND1000FIRST DURETION10,040.0010,040.00
2301/10/2024PA-B1342ABDEND1000SA40.0010,000.00
2402/10/2024PA-B1342ABDEND1000SA8,000.002,000.00
2503/10/2024PA-B1342ABDEND1000PA500.002,500.00
26TOTAL10,540.008,040.002,500.00
splitting




before running macro
copy to bottom based on cell.xlsm
ABCDEFGHI
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
RESULT1



after running
copy to bottom based on cell.xlsm
ABCDEF
1ITEMINVOICE NOCLIENT NODESCRIBEDEBITCREDIT
21PA-B3ABDEND1PA20,000.00
32SA-B35ABDEND1SA1,000.00
43PA-B352ABDEND10PA240.00
54PA-B442ABDEND100SA1,000.00
65PA-B442ABDEND100PA2,000.00
76PA-B1342ABDEND1000SA8,040.00
87PA-B1342ABDEND1000PA500.00
9
10PA TOTAL22,740.00
11SA TOTAL1,040.00
RESULT1


thanks
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Power query method
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Filtered Rows" = Table.SelectRows(Source, each ([DATE] <> null and [DATE] <> "TOTAL" and [DATE] <> "DATE") and ([DESCRIBE] <> "FIRST DURETION")),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"BALANCE"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"DATE", type date}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Changed Type",{"DATE"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns1", {"INVOICE NO", "DESCRIBE", "CLIENT NO"}, {{"Debit1", each List.Sum([DEBIT]), type nullable number}, {"Credit1", each List.Sum([CREDIT]), type nullable number}}),
    #"Renamed Columns" = Table.RenameColumns(#"Grouped Rows",{{"Debit1", "Debit"}, {"Credit1", "Credit"}}),
    #"Added Index" = Table.AddIndexColumn(#"Renamed Columns", "Index", 1, 1, Int64.Type),
    #"Reordered Columns" = Table.ReorderColumns(#"Added Index",{"Index", "INVOICE NO", "DESCRIBE", "Debit", "Credit"}),
    #"Renamed Columns1" = Table.RenameColumns(#"Reordered Columns",{{"Index", "ITEM"}})
in
    #"Renamed Columns1"

Book2
ABCDEF
1ITEMINVOICE NOCLIENT NODESCRIBEDebitCredit
21PA-B3ABDEND1PA20000
32SA-B35ABDEND1SA1000
43PA-B352ABDEND10PA240
54PA-B442ABDEND100SA1000
65PA-B442ABDEND100PA2000
76PA-B1342ABDEND1000SA8040
87PA-B1342ABDEND1000PA500
Table1


In power query copy the table and Group for totals
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Filtered Rows" = Table.SelectRows(Source, each ([DATE] <> null and [DATE] <> "TOTAL" and [DATE] <> "DATE") and ([DESCRIBE] <> "FIRST DURETION")),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"BALANCE"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"DATE", type date}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Changed Type",{"DATE"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns1", {"INVOICE NO", "DESCRIBE"}, {{"Debit1", each List.Sum([DEBIT]), type nullable number}, {"Credit1", each List.Sum([CREDIT]), type nullable number}}),
    #"Renamed Columns" = Table.RenameColumns(#"Grouped Rows",{{"Debit1", "Debit"}, {"Credit1", "Credit"}}),
    #"Grouped Rows1" = Table.Group(#"Renamed Columns", {"DESCRIBE"}, {{"PA TOTAL", each List.Sum([Debit]), type nullable number}, {"SA TOTAL", each List.Sum([Credit]), type nullable number}}),
    #"Removed Columns2" = Table.RemoveColumns(#"Grouped Rows1",{"DESCRIBE"}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Removed Columns2", {}, "Attribute", "Value")
in
    #"Unpivoted Columns"


Book2
AB
1AttributeValue
2PA TOTAL22740
3SA TOTAL10040
Table1 (2)
 
Upvote 0
Code:
Sub test()
    Dim a, i&, ii&, s$, PA#, SA#, t&
    With Sheets("splitting")
        a = .[a1].CurrentRegion.Resize(.Cells.SpecialCells(11).Row, 6)
        a(1, 1) = "DATE"
    End With
    With CreateObject("Scripting.Dictionary")
        For i = 2 To UBound(a, 1)
            If a(i, 4) Like "[PS]A" Then
                t = 5 + IIf(a(i, 4) = "SA", 1, 0)
                If t = 5 Then PA = PA + a(i, t) Else SA = SA + a(i, t)
                s = Join(Array(a(i, 2), a(i, 3), a(i, 4)), Chr(2))
                If Not .exists(s) Then
                    .Item(s) = .Count + 2
                    a(.Item(s), 1) = .Count
                    For ii = 2 To UBound(a, 2)
                        a(.Item(s), ii) = a(i, ii)
                    Next
                Else
                    a(.Item(s), t) = a(.Item(s), t) + a(i, t)
                End If
            End If
        Next
        i = .Count + 1
    End With
    With Sheets("result1").Columns("a:f")
        .Clear
        .HorizontalAlignment = xlCenter
        With .Resize(i)
            .Value = a: .Borders.Weight = 2
        End With
        With .Rows(i + 2).Resize(2, 2)
            .Value = Evaluate("{""PA TOTAL""," & PA & ";""SA TOTAL""," & SA & "}")
            .Borders.Weight = 2
        End With
        With Union(.Rows(1), .Rows(i + 2).Resize(2, 1))
            .Interior.Color = 1137094
            .Font.Bold = True
        End With
        .Columns.AutoFit
    End With
End Sub
 
Upvote 0
@Fuji
appreciate for writing macro for me, I will test it and come back later.;)
 
Upvote 0
@Fuji
code is great , but I think you forgot read this
every time add new ID in column D so will increase ID to show total in column A,B to the bottom.
based on your code , you specify only two ID in column D PA,SA but in reality I have many IDs (PA,SA,BTRY, BUUUT MMY,....)
so I want dealing with many ID in column D . in OP i just gave sample.
thanks you.
 
Upvote 0
Then you must upload the data showing all the DESCRIBE in relation to Credit/Debit.
 
Upvote 0
Then you must upload the data showing all the DESCRIBE in relation to Credit/Debit.
OK maybe this helps.
fj.xlsm
ABCDEFG
1DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
201/01/2024-ABDEND1FIRST DURETION20,000.0020,000.00
302/01/2024PA-B3ABDEND1PA20,000.0040,000.00
403/01/2024SA-B35ABDEND1SA1,000.0039,000.00
5TOTAL40,000.001,000.0039,000.00
6
7
8DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
914/01/2024PA-B352ABDEND10PA140.00140.00
1015/01/2024PA-B352ABDEND10PA100.000.00
11TOTAL240.00240.000.00
12
13
14DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
1514/04/2024PA-B442ABDEND100FIRST DURETION1,040.001,040.00
1615/04/2024PA-B442ABDEND100SA1,000.0040.00
1716/04/2024PA-B442ABDEND100PA2,000.002,040.00
18TOTAL3,040.001,000.002,040.00
19
20
21DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
2230/09/2024PA-B1342ABDEND1000FIRST DURETION10,040.0010,040.00
2301/10/2024PA-B1342ABDEND1000SA40.0010,000.00
2402/10/2024PA-B1342ABDEND1000SA8,000.002,000.00
2503/10/2024PA-B1342ABDEND1000PA500.002,500.00
26TOTAL10,540.008,040.002,500.00
27
28
29
30DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
3101/10/2024PA-B13421ABDEND1001BTRY1,000.001,000.00
3202/10/2024PA-B13421ABDEND1001BTRY2,000.003,000.00
3303/10/2024PA-B13421ABDEND1001BUUUT MMY2,000.001,000.00
3404/10/2024PA-B13421ABDEND1001BUUUT MMY1,000.000.00
35TOTAL3,000.003,000.000.00
36
37
38DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
3901/10/2024vbgf-00ABDEND1002VGHJ BHGH MTYU1,000.00-1,000.00
4002/10/2024vbgf-00ABDEND1002VGHJ BHGH MTYU2,000.00-3,000.00
4103/10/2024vbgf-00ABDEND1002VFGTRR REDG MHH2,500.00-500.00
4204/10/2024vbgf-00ABDEND1002VFGTRR REDG MHH15,000.0014,500.00
4305/10/2024vbgf-00ABDEND1002BUUUT MMY1,000.00
4406/10/2024vbgf-00ABDEND1002BUUUT MMY1,000.00
45TOTAL17,500.005,000.0012,500.00
46
47
48DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
4901/10/2024vbgf-01ABDEND1003BUTRT RET1,000.001,000.00
5002/10/2024vbgf-01ABDEND1003BUTRT RET1,500.002,500.00
5103/10/2024vbgf-01ABDEND1003VGHJ BHGH MTYU1,000.001,500.00
52TOTAL2,500.001,000.001,500.00
splitting




fj.xlsm
ABCDEF
1ITEMINVOICE NOCLIENT NODESCRIBEDEBITCREDIT
21PA-B3ABDEND1PA20,000.00
32SA-B35ABDEND1SA1,000.00
43PA-B352ABDEND10PA240.00
54PA-B442ABDEND100SA1,000.00
65PA-B442ABDEND100PA2,000.00
76PA-B1342ABDEND1000SA8,040.00
87PA-B1342ABDEND1000PA500.00
98PA-B13421ABDEND1001BTRY3,000.00
109PA-B13421ABDEND1001BUUUT MMY3,000.00
1110vbgf-00ABDEND1002VGHJ BHGH MTYU3,000.00
1211vbgf-00ABDEND1002VFGTRR REDG MHH17,500.00
1312vbgf-00ABDEND1002BUUUT MMY2,000.00
1413vbgf-01ABDEND1003BUTRT RET2,500.00
1514vbgf-01ABDEND1003VGHJ BHGH MTYU1,000.00
16
17PA TOTAL22,740.00
18SA TOTAL1,040.00
19BTRY TOTAL3,000.00
20BUUUT MMY TOTAL5,000.00
21VGHJ BHGH MTYU TOTAL4,000.00
22VFGTRR REDG MHH TOTAL17,500.00
23BUTRT RET TOTAL2,500.00
result
 
Upvote 0
Code:
Sub test()
    Dim a, i&, ii&, s$, PA#, SA#, t&, dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
    With Sheets("splitting")
        a = .[a1].CurrentRegion.Resize(.Cells.SpecialCells(11).Row, 6)
        a(1, 1) = "DATE"
    End With
    With CreateObject("Scripting.Dictionary")
        For i = 2 To UBound(a, 1)
            If (a(i, 4) <> "") * (a(i, 4) <> "FIRST DURETION") * (a(i, 4) <> "DESCRIBE") Then
                dic(a(i, 4)) = dic(a(i, 4)) + a(i, 5) + a(i, 6)
                s = Join(Array(a(i, 2), a(i, 3), a(i, 4)), Chr(2))
                If Not .exists(s) Then
                    .Item(s) = .Count + 2
                    a(.Item(s), 1) = .Count
                    For ii = 2 To UBound(a, 2)
                        a(.Item(s), ii) = a(i, ii)
                    Next
                Else
                    For ii = 5 To 6
                        a(.Item(s), ii) = a(.Item(s), ii) + a(i, ii)
                        If a(.Item(s), ii) = 0 Then a(.Item(s), ii) = Empty
                    Next
                End If
            End If
        Next
        i = .Count + 1
    End With
    With Sheets("result1").Columns("a:f")
        .Clear
        .HorizontalAlignment = xlCenter
        With .Resize(i)
            .Value = a: .Borders.Weight = 2
        End With
        With .Rows(i + 2).Resize(dic.Count, 2)
            .Value = Application.Transpose(Array(dic.keys, dic.items))
            .Columns(1) = .Parent.Evaluate(.Columns(1).Address & "&"" TOTAL""")
            .Borders.Weight = 2
        End With
        With Union(.Rows(1), .Rows(i + 2).Resize(dic.Count, 1))
            .Interior.Color = 1137094
            .Font.Bold = True
        End With
        .Columns.AutoFit
    End With
End Sub
 
Upvote 0
Solution
awesome!
may you change TOTAL for each item to the bottom in location columns D,E, please?
also should show ITEM in A1 not DATE as the code does it. and show any amount in result1 sheet like #,##0.00"
copy to bottom based on cell.xlsm
ABCDEF
1ITEMINVOICE NOCLIENT NODESCRIBEDEBITCREDIT
21PA-B3ABDEND1PA20,000.00
32SA-B35ABDEND1SA1,000.00
43PA-B352ABDEND10PA240.00
54PA-B442ABDEND100SA1,000.00
65PA-B442ABDEND100PA2,000.00
76PA-B1342ABDEND1000SA8,040.00
87PA-B1342ABDEND1000PA500.00
98PA-B13421ABDEND1001BTRY3,000.00
109PA-B13421ABDEND1001BUUUT MMY3,000.00
1110vbgf-00ABDEND1002VGHJ BHGH MTYU3,000.00
1211vbgf-00ABDEND1002VFGTRR REDG MHH17,500.00
1312vbgf-00ABDEND1002BUUUT MMY2,000.00
1413vbgf-01ABDEND1003BUTRT RET2,500.00
1514vbgf-01ABDEND1003VGHJ BHGH MTYU1,000.00
16
17PA TOTAL22,740.00
18SA TOTAL10,040.00
19BTRY TOTAL3,000.00
20BUUUT MMY TOTAL5,000.00
21VGHJ BHGH MTYU TOTAL4,000.00
22VFGTRR REDG MHH TOTAL17,500.00
23BUTRT RET TOTAL2,500.00
RESULT1
 
Upvote 0

Forum statistics

Threads
1,226,112
Messages
6,189,040
Members
453,521
Latest member
Chris_Hed

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