exclude zero value of the data in last row for each name

Abdo

Board Regular
Joined
May 16, 2022
Messages
183
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hi Guys,
I want to copy the data after the last row containing a zero for column G of each name of column C , and ignore all the data that precedes the last row with a zero for the same name , and if the last row contains a zero for column G for a specific name . if there are no data under it the last row with a zero , then it should be ignored and not copied But if there is absolutely no last row that contains a value of zero for a specific name, then all the data for the name is copied.
I put what I want in second sheet.
could be data about 6000 rows . now this will be simple as sample.
box1 (2).xlsm
ABCDEFG
1DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
201/01/2022-ABDEND1OPENNING20000-20000
304/01/2022PA-B3ABDEND1PA20000-40000
405/01/2022SA-B35ABDEND1SA-100039000
506/01/2022SA-B36ABDEND1SA-39000-
607/01/2022PA-B3ABDEND1PA1000100900
702/01/2022PA-B36ABDEND2PA1000-1000
807/01/2022PA-B37ABDEND2PA1000-2000
908/01/2022SA-B37ABDEND2PA-2000-
1009/01/2022PA-B38ABDEND2PA1000-1000
1110/01/2022PA-B39ABDEND2PA1500-2500
1211/01/2022PA-B40ABDEND3OPENNING-200-200
1312/01/2022PA-B41ABDEND3PA5000-4800
1413/01/2022PA-B42ABDEND3PA5000-9800
1514/01/2022PA-B43ABDEND3PA5000-14800
1615/01/2022PA-B44ABDEND3PA2000-16800
1716/01/2022PA-B45ABDEND3PA100010017700
1817/01/2022SA-B38ABDEND3SA-17700-
1918/01/2022PA-B46ABDEND4PA1000100900
2019/01/2022PA-B47ABDEND3PA2000500`1500
2120/01/2022PA-B48ABDEND3PA2000-3500
2221/01/2022PA-B49ABDEND4PA900900900
2322/01/2022PA-B50ABDEND5PA200-200
2423/01/2022PA-B51ABDEND6PA12002001000
2524/01/2022PA-B52ABDEND5PA200400-
2625/01/2022SA-B39ABDEND6SA-400600
2726/01/2022PA-B53ABDEND5PA220220-
2827/01/2022SA-B40ABDEND6SA-200400
2928/01/2022PA-B54ABDEND5PA22020200
3029/01/2022PA-B55ABDEND7PA100100
3130/01/2022PA-B56ABDEND8PA200-200
3231/01/2022SA-B41ABDEND7SA-100-
3301/02/2022SA-B42ABDEND8SA-200-
3402/02/2022PA-B57ABDEND9PA200200-
filter
Cell Formulas
RangeFormula
G2,G12,G7G2=E2-F2
G3,G13:G17,G8G3=G2+E3-F3


result


box1 (2).xlsm
ABCDEFG
1DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
207/01/2022PA-B3ABDEND1PA1000100900
309/01/2022PA-B38ABDEND2PA1000-1000
410/01/2022PA-B39ABDEND2PA1500-2500
519/01/2022PA-B47ABDEND3PA2000500`1500
620/01/2022PA-B48ABDEND3PA2000-3500
718/01/2022PA-B46ABDEND4PA1000100900
821/01/2022PA-B49ABDEND4PA900900900
928/01/2022PA-B54ABDEND5PA22020200
1023/01/2022PA-B51ABDEND6PA12002001000
1125/01/2022SA-B39ABDEND6SA-400600
1227/01/2022SA-B40ABDEND6SA-200400
CLEAN

note: should sort name in second sheet from samll to big based on column C
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Give this a try:
It assumes the sheet CLEAN already exists.
Change the sheet names to your sheet names.

VBA Code:
Sub KeepOpenTransactions()

    Dim shtSrc As Worksheet, shtDest As Worksheet
    Dim srcRowLast As Long
    Dim srcRng As Range, destRng As Range
    Dim srcArr As Variant, destArr As Variant
    Dim i As Long, iKeepFlag As Long, totalKeep As Long, j As Long, k As Long
    
    Set shtSrc = Worksheets("filter")
    Set shtDest = Worksheets("CLEAN")
    
    With shtSrc
        srcRowLast = .Range("A" & Rows.Count).End(xlUp).Row
        Set srcRng = .Range("A1:G" & srcRowLast)
        srcArr = srcRng.Value2
        ReDim Preserve srcArr(1 To UBound(srcArr), 1 To UBound(srcArr, 2) + 1)
    End With
    
    Set destRng = shtDest.Range("A1")
    
    Dim dictSrc As Object, dictKey As String

    Set dictSrc = CreateObject("Scripting.dictionary")
    
    ' Load Client names into Dictionary and get highest row for Client with zero balance
    For i = 2 To UBound(srcArr)
        dictKey = srcArr(i, 3)
        If Not dictSrc.exists(dictKey) Then
            dictSrc(dictKey) = 0
        End If
        
        If srcArr(i, 7) = 0 Then
            dictSrc(dictKey) = i
        End If
    Next i
    
    ' Flag Source Array lines to keep
    srcArr(1, 8) = iKeepFlag        ' Keep Heading
    totalKeep = 1
    iKeepFlag = 1
    For i = 2 To srcRowLast
        dictKey = srcArr(i, 3)
        If i > dictSrc(dictKey) Then
            srcArr(i, 8) = iKeepFlag
            totalKeep = totalKeep + 1
        End If
    Next i

    ' Move lines to keep to output array
    ReDim destArr(1 To totalKeep + 1, 1 To UBound(srcArr, 2) - 1)
    For i = 1 To UBound(srcArr)
        If srcArr(i, 8) = 1 Then
            j = j + 1
            For k = 1 To UBound(destArr, 2)
                destArr(j, k) = srcArr(i, k)
            Next k
        End If
    Next i
    
    destRng.CurrentRegion.ClearContents
    Set destRng = destRng.Resize(j, UBound(destArr, 2))
    destRng.Value2 = destArr
    
    srcRng.Rows(2).Copy
    destRng.PasteSpecial Paste:=xlPasteFormats
    srcRng.Rows(1).Copy Destination:=destRng.Rows(1)
    destRng.Columns.AutoFit
    
    shtDest.Sort.SortFields.Clear
    destRng.Sort Key1:=destRng.Cells(1, 3), Order1:=xlAscending, Header:=xlYes
    
End Sub
 
Upvote 1
Solution
thanks
but there are two problems :
1-in sheet clean doesn't show row2 for client ABDEND1 as in pic2 , why?
2- should clear data for CLEAN sheet before brings data because will change data in FILTER sheet , then should update automatically in CLEAN sheet
 
Upvote 0
thanks
but there are two problems :
1-in sheet clean doesn't show row2 for client ABDEND1 as in pic2 , why?
2- should clear data for CLEAN sheet before brings data because will change data in FILTER sheet , then should update automatically in CLEAN sheet
1-in sheet clean doesn't show row2 for client ABDEND1 as in pic2 , why?
At the top of the Code change the A1 to A2 in this line:
Rich (BB code):
    Set destRng = shtDest.Range("A2")

At the bottom of the code change the line in blue to what I have here:
Rich (BB code):
    srcRng.Rows(2).Copy
    destRng.PasteSpecial Paste:=xlPasteFormats
    srcRng.Rows(1).Copy Destination:=destRng.Offset(-1).Rows(1)
    destRng.Columns.AutoFit
    
    shtDest.Sort.SortFields.Clear
    destRng.Sort Key1:=destRng.Cells(1, 3), Order1:=xlAscending, Header:=xlYes
    
End Sub

2- should clear data for CLEAN sheet before brings data because will change data in FILTER sheet , then should update automatically in CLEAN sheet
Why do you say that it doesn't ? This line in the existing code does that.
VBA Code:
    destRng.CurrentRegion.ClearContents
 
Upvote 0
2- should clear data for CLEAN sheet before brings data because will change data in FILTER sheet , then should update automatically in CLEAN sheet
Why do you say that it doesn't ? This line in the existing code does that.
VBA Code:
    destRng.CurrentRegion.ClearContents
My apologies !
I thought to doesn't work
now has fixed . thanks very much for your solution.:)
 
Upvote 0

Forum statistics

Threads
1,215,154
Messages
6,123,327
Members
449,098
Latest member
thnirmitha

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