code to copy last row & 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
in earlier time I got help by @Alex Blakenburg in this thread
exclude zero value of the data in last row for each name
I'm not sure how adjusting this code , or alternative.
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("A2")
  
    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)
    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

I want to copy the data for the last row for each duplicate name in column C , and ignore all of the rest of data for the same name , and if the last row contains a zero for column G for a specific name also should ignore it .
I put what I want in second sheet.
box (1).xlsm
ABCDEFG
1DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
201/01/2022-ABDEND1OPENNING20,000.000.0020,000.00
304/01/2022PA-B3ABDEND1PA20,000.000.0040,000.00
405/01/2022SA-B35ABDEND1SA0.001,000.0039,000.00
506/01/2022SA-B36ABDEND1SA1,000.0039,000.001,000.00
607/01/2022PA-B3ABDEND1PA1,000.00100.001,900.00
702/01/2022PA-B36ABDEND2PA1,000.000.001,000.00
807/01/2022PA-B37ABDEND2PA1,000.000.002,000.00
908/01/2022SA-B37ABDEND2PA500.002,000.00500.00
1009/01/2022PA-B38ABDEND2PA1,000.000.001,500.00
1110/01/2022PA-B39ABDEND2PA1,500.000.003,000.00
1211/01/2022PA-B40ABDEND3OPENNING0.00200.00-200.00
1312/01/2022PA-B41ABDEND3PA5,000.000.004,800.00
1413/01/2022PA-B42ABDEND3PA5,000.000.009,800.00
1514/01/2022PA-B43ABDEND3PA5,000.000.0014,800.00
1615/01/2022PA-B44ABDEND3PA2,000.000.0016,800.00
1716/01/2022PA-B45ABDEND3PA1,000.00100.0017,700.00
1817/01/2022SA-B38ABDEND3SA1,000.0017,700.001,000.00
1919/01/2022PA-B47ABDEND3PA2,000.00500.002,500.00
2020/01/2022PA-B48ABDEND3PA2,000.001,000.003,500.00
2118/01/2022PA-B46ABDEND4PA1,000.00100.00900.00
2221/01/2022PA-B49ABDEND4PA900.00900.00900.00
2322/01/2022PA-B50ABDEND5PA200.000.00200.00
2426/01/2022PA-B53ABDEND5PA200.00100.00300.00
2528/01/2022PA-B54ABDEND5PA200.00300.00200.00
2623/01/2022PA-B51ABDEND6PA1,200.00200.001,000.00
2725/01/2022SA-B39ABDEND6SA0.00400.00600.00
2827/01/2022SA-B40ABDEND6SA0.00600.000.00
FILTER
Cell Formulas
RangeFormula
G2,G26,G23,G21,G12,G7G2=E2-F2
G27:G28,G24:G25,G22,G13:G20,G8:G11,G3:G6G3=G2+E3-F3



result

box(1).xlsm
ABCDEFG
1DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
207/01/2022PA-B3ABDEND1PA1,000.00100.001,900.00
310/01/2022PA-B39ABDEND2PA1,500.000.003,000.00
420/01/2022PA-B48ABDEND3PA2,000.001,000.003,500.00
521/01/2022PA-B49ABDEND4PA900.00900.00900.00
628/01/2022PA-B54ABDEND5PA200.00300.00200.00
CLEAN



could be data about 6000 rows .
I hope Alex see my thread or anybody could help.
thanks
 
Last edited by a moderator:

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

Forum statistics

Threads
1,215,091
Messages
6,123,062
Members
449,089
Latest member
ikke

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