Copy last row for each range contains names under headers

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
651
Office Version
  1. 2019
Hello
I would macro to deal with 300 names , and . every name contains range will be different ho many rows contain for each range , so the the total rows in sheet will be 11000 as maximum.
the result should be in REPORT sheet based on NAMES sheet
REPORT sheet in column B will brings last date before SUM row in NAMES sheet , column C will bring names from column D under NAMES header, as to columns D:F will brings from SUM row in NAMES sheet are existed in columns E:G .
in REPORT sheet contains SUM row if there is no empty row then should insert new row with the same formatting and borders and expand range in formulas in SUM row
ERROR MERGING.xlsm
ABCDEFG
1NAMES
2CCF-1000
3ITEMDATEINV.NOCASEDEBITCREDITBALANCE
4130/06/2023 --500.00-500.00
5215/07/2023RVCH2000030,000.00 -29,500.00
6315/07/2023RVCH2000210,000.00 -39,500.00
7415/09/2023BSTR_23448OUTSANDING -2,300.0037,200.00
8515/09/2023BSTR_23449OUTSANDING -1,920.0035,280.00
9615/09/2023BSTR_23450PAID50,400.00 -85,680.00
10715/09/2023BSJ_23444OUTSANDING1,720.00 -87,400.00
11815/09/2023BSJ_23446PAID -4,900.0082,500.00
12915/09/2023VSTR_23444PAID -500.0082,000.00
131015/09/2023VSTR_23446OUTSANDING3,600.00 -85,600.00
141115/09/2023RSS_23222OUTSANDING -860.0084,740.00
151215/09/2023VCH20005 -15,000.0069,740.00
161316/09/2023VSTR_23449PAID -3,760.0065,980.00
171416/09/2023RSS_23224PAID2,950.00 -68,930.00
18SUM98,670.0028,740.0069,930.00
19
20
21NAMES
22CCF-1001
23ITEMDATEINV.NOCASEDEBITCREDITBALANCE
24130/06/20232,000.00 -2,000.00
25215/07/2023RVCH2000125,000.00 -27,000.00
26315/09/2023VSTR_23445OUTSANDING1,000.00 -28,000.00
27416/09/2023BSTR_23452PAID7,200.00 -35,200.00
28SUM35,200.00 -35,200.00
NAMES


ERROR MERGING.xlsm
ABCDEF
1ITEMDATENAMESDEBITCREDITBALANCE
2
3SUM0.000.000.00
REPORT
Cell Formulas
RangeFormula
D3:E3D3=SUM(D2:D2)
F3F3=D3-E3




result

ERROR MERGING.xlsm
ABCDEF
1ITEMDATENAMESDEBITCREDITBALANCE
2116/09/2023CCF-100098,670.0028,740.0069,930.00
3216/09/2023CCF-100135,200.00 -35,200.00
4SUM133,870.0028,740.00105,130.00
REPORT
Cell Formulas
RangeFormula
D4:E4D4=SUM(D2:D3)
F4F4=D4-E4
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try:

VBA Code:
Sub CreateReport()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long, lr As Long
  Dim totD As Double, totC As Double, totB As Double
  
  a = Sheets("NAMES").Range("A1", Sheets("NAMES").Range("G" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a), 1 To 6)
  
  For i = 1 To UBound(a)
    If a(i, 4) = "NAMES" Then
      k = k + 1
      b(k, 1) = k
      b(k, 3) = a(i + 1, 4)
    End If
    If a(i, 1) = "SUM" Then
      b(k, 2) = a(i - 1, 2)
      b(k, 4) = Val(a(i, 5))
      b(k, 5) = Val(a(i, 6))
      b(k, 6) = Val(a(i, 5)) - Val(a(i, 6))
      totD = totD + b(k, 4)
      totC = totC + b(k, 5)
      totB = totB + b(k, 6)
    End If
  Next
  
  With Sheets("REPORT")
    .Range("A2:F" & Rows.Count).Clear
    .Range("A2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
    
    'FORMTA CELLS
    lr = .Range("A" & Rows.Count).End(3).Row + 1
    .Range("A" & lr).Value = "SUM"
    .Range("D" & lr).Value = totD
    .Range("E" & lr).Value = totC
    .Range("F" & lr).Value = totB
    .Range("A:F").HorizontalAlignment = xlCenter
    .Range("B:B").NumberFormat = "dd/mm/yyyy"
    .Range("D:F").NumberFormat = "#,##0.00;-#,##0.00;-"
    .Range("A2:F" & lr).Borders.LineStyle = xlContinuous
    .Range("A1").Copy
    .Range("A" & lr).PasteSpecial xlPasteFormats
  End With
  Application.CutCopyMode = False
End Sub

🤗
 
Upvote 0
Solution
Hi Dant again,
the code is great !:)

I would fix color for Sum word in last row , when change SUM word in different location will leave color in previous location . should delete formatting from previous cell in column A was highlighed and bold font
 
Upvote 0
sorry I know my bad !

just curiosity how can I repeat copy to the bottom for the same name instead of replace, please?
thank you
 
Upvote 0
just curiosity how can I repeat copy to the bottom for the same name instead of replace, please?
I don't understand, you didn't put it in your examples nor did you mention it in your OP.

I'll try to guess 🧙‍♂️


VBA Code:
Sub CreateReport()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long, lr As Long
  Dim totD As Double, totC As Double, totB As Double
  
  a = Sheets("NAMES").Range("A1", Sheets("NAMES").Range("G" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a), 1 To 6)
  
  For i = 1 To UBound(a)
    If a(i, 4) = "NAMES" Then
      k = k + 1
      b(k, 1) = k
      b(k, 3) = a(i + 1, 4)
    End If
    If a(i, 1) = "SUM" Then
      b(k, 2) = a(i - 1, 2)
      b(k, 4) = Val(a(i, 5))
      b(k, 5) = Val(a(i, 6))
      b(k, 6) = Val(a(i, 5)) - Val(a(i, 6))
      totD = totD + b(k, 4)
      totC = totC + b(k, 5)
      totB = totB + b(k, 6)
    End If
  Next
  
  With Sheets("REPORT")
    '.Range("A2:F" & Rows.Count).Clear
    lr = .Range("A" & Rows.Count).End(3).Row + 1
    .Range("A" & lr).Resize(UBound(b, 1), UBound(b, 2)).Value = b
    
    'FORMTA CELLS
    lr = .Range("A" & Rows.Count).End(3).Row + 1
    .Range("A" & lr).Value = "SUM"
    .Range("D" & lr).Value = totD
    .Range("E" & lr).Value = totC
    .Range("F" & lr).Value = totB
    .Range("A:F").HorizontalAlignment = xlCenter
    .Range("B:B").NumberFormat = "dd/mm/yyyy"
    .Range("D:F").NumberFormat = "#,##0.00;-#,##0.00;-"
    .Range("A2:F" & lr).Borders.LineStyle = xlContinuous
    .Range("A1").Copy
    .Range("A" & lr).PasteSpecial xlPasteFormats
  End With
  Application.CutCopyMode = False
End Sub

If it is not what you need, you will have to be more specific with examples, but from the first post.
 
Upvote 0
when I run the code every time repeat copy to the bottom and put duplicate name under each other of them like this
Lastrow
ABCDEF
1ITEMDATENAMESDEBITCREDITBALANCE
2116/09/2023CCF-100098,670.0028,740.0069,930.00
3216/09/2023CCF-100098,670.0028,740.0069,930.00
4316/09/2023CCF-100098,670.0028,740.0069,930.00
5416/09/2023CCF-100098,670.0028,740.0069,930.00
6516/09/2023CCF-100135,200.00-35,200.00
7616/09/2023CCF-100135,200.00-35,200.00
8716/09/2023CCF-100135,200.00-35,200.00
9816/09/2023CCF-100135,200.00-35,200.00
10SUM535,480.00114,960.00420,520.00
REPORT
 
Upvote 0
I still don't understand what you need.
As I said in the previous post, it wasn't in your OP.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,127
Members
453,021
Latest member
Justyna P

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