copy lastrow for each name in range from sheet to another

Abdo

Board Regular
Joined
May 16, 2022
Messages
183
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hi
I want showing the lastrow for each name from sheet to another based on column C in sheet filter with ignore columns A,B,D
sheet filter
f .xlsm
ABCDEFG
1DATEINVOICE NOCLIENT NODESCRIBEDEBITCREDITBALANCE
21/1/2022-ABDEND1OPENNING20,000.00-20,000.00
31/4/2022PA-B3ABDEND1PA20,000.00-40,000.00
41/5/2022SA-B35ABDEND1SA-1,000.0039,000.00
51/2/2022PA-B36ABDEND2PA1,000.00-1,000.00
61/7/2022PA-B37ABDEND2PA1,000.00-2,000.00
71/13/2022PA-B45ABDEND2PA1,000.00100.002,900.00
81/3/2022PA-B38ABDEND3openning-200.00-200.00
91/6/2022PA-B39ABDEND3PA5,000.00-4,800.00
101/8/2022PA-B40ABDEND3PA5,000.00-9,800.00
111/9/2022PA-B41ABDEND3PA5,000.00-14,800.00
121/10/2022PA-B42ABDEND3PA2,000.00-16,800.00
131/11/2022PA-B43ABDEND3PA1,000.00100.0017,700.00
141/12/2022PA-B44ABDEND4PA1,000.00100.00900.00
filter

the result in sheet OUTPUT from row2 with the same borders & formatting . with considering any changing or adding new data in sheet filter should update in sheet output .
f.xlsm
ABCDE
1ITEMCLIENT NODEBITCREDITBALANCE
21ABDEND1-1,000.0039,000.00
32ABDEND21,000.00100.002,900.00
43ABDEND31,000.00100.0017,700.00
54ABDEND41,000.00100.00900.00
output


thanks
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
See if this works for you:

VBA Code:
Option Explicit
Sub abdo()

Dim wb As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim lr As Long

Set wb = ThisWorkbook
Set ws1 = wb.Worksheets("filter")
Set ws2 = wb.Worksheets("output")

lr = ws1.Cells(Rows.Count, "C").End(xlUp).Row

    With ws1
    
        .Range("C2:C" & lr).Copy
        ws2.Cells(2, 2).PasteSpecial Paste:=xlPasteFormats
        ws2.Cells(2, 2).PasteSpecial Paste:=xlPasteValues
        
        .Range("E2:G" & lr).Copy
        ws2.Cells(2, 3).PasteSpecial Paste:=xlPasteFormats
        ws2.Cells(2, 3).PasteSpecial Paste:=xlPasteValues
        
    End With
             
End Sub
 
Upvote 0
Since I misinterpreted what you asked, I assume a new helper column on column H, with the unique values for Client No.
Someone else could provide a faster way of doing this.

I am sorry for thinking I could help you out, but as I said, I misinterpreted what you asked.

See if this works for you

VBA Code:
Option Explicit
Sub abdo()

Dim wb As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim lr1 As Long, lr2 As Long, lr3 as long, i As Long
Dim filtervalue As String

Set wb = ThisWorkbook
Set ws1 = wb.Worksheets("filter")
Set ws2 = wb.Worksheets("output")

filtervalue = ws1.Cells(i, 8)

lr1 = ws1.Cells(Rows.Count, "C").End(xlUp).Row
lr2 = ws2.Cells(Rows.Count, "B").End(xlUp).Row + 1
lr3 = ws2.cells(Rows.Count, "C").End(xlup).Row +1

For i = 2 To lr1

    With ws1
       
        .AutoFilter 3, filtervalue
   
        .Range("C2:C" & lr1 -1).Copy
        ws2.Cells(lr2, 2).PasteSpecial Paste:=xlPasteFormats
        ws2.Cells(lr2, 2).PasteSpecial Paste:=xlPasteValues
       
        .Range("E2:G" & lr1 - 1).Copy
        ws2.Cells(lr3, 3).PasteSpecial Paste:=xlPasteFormats
        ws2.Cells(lr3, 3).PasteSpecial Paste:=xlPasteValues
       
    End With
   
        .AutoFilter
       
Next i
            
End Sub
 
Upvote 0
I believe this is what you want.
I assume you create a new sheet named "names" with unique values from column C, starting on row 2.
VBA Code:
Option Explicit
Sub abdo()

Dim wb As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
Dim lr1 As Long, lr2 As Long, lr3 As Long, lr4 As Long, lr5 As Long, lr6 As Long, i As Long
Dim filtervalue As String, rngCC As String, rngEG As String

    With ThisWorkbook
    
        Set ws1 = .Worksheets("filter")
        Set ws2 = .Worksheets("output")
        Set ws3 = .Worksheets("names")
        
    End With
    
lr1 = ws1.Cells(Rows.Count, "A").End(xlUp).Row
lr2 = ws3.Cells(Rows.Count, "A").End(xlUp).Row

For i = 2 To lr2

    filtervalue = ws3.Cells(i, 1).Value
    
    With ws2
        
        lr3 = ws2.Cells(Rows.Count, "B").End(xlUp).Row + 1
        lr4 = ws2.Cells(Rows.Count, "C").End(xlUp).Row + 1
        
    End With
    
    With ws1.Range("A1:G" & lr1)
    
        .AutoFilter 3, filtervalue
        
        lr5 = ws1.Cells(Rows.Count, "C").End(xlUp).Row
        
        rngCC = "C" & lr5 & ":C" & lr5
        
        lr6 = ws1.Cells(Rows.Count, "E").End(xlUp).Row
        
        rngEG = "E2" & lr5 & ":G" & lr5
        
        With ws1
        
            .Range(rngCC).Copy
            ws2.Cells(lr3, 2).PasteSpecial Paste:=xlPasteValues
            
            .Range(rngEG).Copy
            ws2.Cells(lr4, 3).PasteSpecial Paste:=xlPasteValues
        
        End With
        
        .AutoFilter
        Application.CutCopyMode = False
        
    End With
    
    ws3.Activate
    
Next i

End Sub
 
Upvote 0
thanks based on last the code doesn't show any thing :unsure:
 
Upvote 0
You can try this one.
VBA Code:
Sub CreateReportUniqueLastRow()

   Dim vWS1 As Worksheet, vWS2 As Worksheet
   Dim vLastRow As String, vRng, vA1, vN As Long, vN2 As Long
   Dim vUnique As String, vAUnique

   Application.ScreenUpdating = False
   MsgBox "Wait to reach end"
   Set vWS1 = Sheets("filter")
   Set vWS2 = Sheets("output")
   vLastRow = vWS1.Cells(Rows.Count, "C").End(xlUp).Address
   Set vRng = vWS1.Range("A2", vWS1.Range(vLastRow).Offset(, 4))
   vA = vRng.Columns(3).Value
   For vN = 1 To UBound(vA)
      If InStr(1, vUnique, vA(vN, 1)) Then
      Else
         vUnique = vUnique & " §§§ " & vA(vN, 1)
      End If
   Next vN
   vAUnique = Split(vUnique, " §§§ ")
   vWS1.Cells(1, 1).Resize(, 7).Copy vWS2.Cells(1, 1).Resize(, 7)
   For vN2 = 1 To UBound(vAUnique)
      vFind = vRng.Columns(3).Find(vAUnique(vN2), , , xlWhole, , xlPrevious, True).Row
      vWS1.Cells(vFind, 1).Resize(, 7).Copy vWS2.Cells(vN2 + 1, 1).Resize(, 7)
   Next vN2
   MsgBox "End"
   Application.ScreenUpdating = True

End Sub
 
Upvote 0
@EXCEL MAX awesome your code !! , but I need somethings need fix ,please .
as I said in OP
with ignore columns A,B,D
sheet filter
I don't need theses columns
and based on the result in sheet output as is in OP should cancel date in column A and replace of it by numbers sequences 1,2,3....
last things I don't need the message box when run the macro it takes time to see the result .
 
Upvote 0
Sorry, I was hurry. Maybe now is little bit better.
VBA Code:
Sub CreateReportUniqueLastRow()

   Dim vWS1 As Worksheet, vWS2 As Worksheet
   Dim vLastRow As String, vUnique As String
   Dim vRng As Range, vRng1 As Range, vRng2 As Range
   Dim vN As Long, vN2 As Long
   Dim vAUnique As Variant, vA As Variant

   Application.ScreenUpdating = False
   Set vWS1 = Sheets("filter")
   Set vWS2 = Sheets("output")
   vLastRow = vWS1.Cells(Rows.Count, "C").End(xlUp).Address
   Set vRng = vWS1.Range("A2", vWS1.Range(vLastRow).Offset(, 4))
   vA = vRng.Columns(3).Value
   For vN = 1 To UBound(vA)
      If InStr(1, vUnique, vA(vN, 1)) Then
      Else
         vUnique = vUnique & " §§§ " & vA(vN, 1)
      End If
   Next vN
   vAUnique = Split(vUnique, " §§§ ")
   Set vRng1 = Union(vWS1.Cells(1, 1), vWS1.Cells(1, 3), _
    vWS1.Cells(1, 5).Resize(, 3))
   vRng1.Copy vWS2.Cells(1, 1).Resize(, 5)
   For vN2 = 1 To UBound(vAUnique)
      vFind = vRng.Columns(3).Find( _
        vAUnique(vN2), , , xlWhole, , xlPrevious, True).Row
      Set vRng2 = Union(vWS1.Cells(vFind, 1), vWS1.Cells(vFind, 3), _
        vWS1.Cells(vFind, 5).Resize(, 3))
      vRng2.Copy vWS2.Cells(vN2 + 1, 1).Resize(, 5)
      vWS2.Cells(vN2 + 1, 1) = vN2
      vWS2.Cells(vN2 + 1, 1).NumberFormat = "0"
      vWS2.Cells(vN2 + 1, 3).Resize(, 3).NumberFormat = "0.00"
   Next vN2
   vWS2.Columns("A:E").AutoFit
   vWS2.Columns("B").ColumnWidth = vWS1.Columns("C").ColumnWidth
   Application.ScreenUpdating = True

End Sub
 
Upvote 0
@EXCEL MAX great !
but why the code is not fast . gives 0.11 the data are not big :unsure:
I expect the running speed should be 0.02 0r 0.01
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,261
Messages
6,123,948
Members
449,134
Latest member
NickWBA

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