Macro to populate amounts in list based on adjacent amount for last row and previous lastrow

Mussala

Board Regular
Joined
Sep 28, 2022
Messages
68
Office Version
  1. 2019
Platform
  1. Windows
Hello,
I would macro to create report column H:K with the same formatting and borders and headers .
so should brings the date based on column A before TOTAL row and put in column H and brings balance in TOTAL row from column E and put in column I and brings amount from column F is (always existed before TOTAL row) and put in column J and column K = column I+ column J and insert TOTAL row to sum each column


Q5‫‬.xlsx
ABCDEF
1DATEDESCRIBEDEBITCREDITBALANCEREAL
219/06/2023INV0014,220.004,220.00
319/06/2023INV0025,000.009,220.00
419/06/2023INV0031,920.0011,140.00
519/06/2023INV004270.0011,410.00
619/06/2023INV0054,220.0015,630.00
719/06/2023INV0063,700.0019,330.00
819/06/2023INV0072,460.0021,790.00
919/06/2023INV0081,000.0022,790.00
1019/06/2023EXPENSES44.0022,746.00
1119/06/2023EXPENSES30.0022,716.00
1219/06/2023EXPENSES65.0022,651.00
1319/06/2023EXPENSES11.0022,640.001,700.00
14TOTAL22,790.00150.0022,640.00
ASS



should be
Q5‫‬.xlsx
ABCDEFGHIJK
1DATEDESCRIBEDEBITCREDITBALANCEREALDATEBALANCEREALNET
219/06/2023INV0014,220.004,220.0019/06/202322,640.001,700.0024,340.00
319/06/2023INV0025,000.009,220.00TOTAL22,640.001,700.0024,340.00
419/06/2023INV0031,920.0011,140.00
519/06/2023INV004270.0011,410.00
619/06/2023INV0054,220.0015,630.00
719/06/2023INV0063,700.0019,330.00
819/06/2023INV0072,460.0021,790.00
919/06/2023INV0081,000.0022,790.00
1019/06/2023EXPENSES44.0022,746.00
1119/06/2023EXPENSES30.0022,716.00
1219/06/2023EXPENSES65.0022,651.00
ASS
Cell Formulas
RangeFormula
K2K2=I2+J2
I3:K3I3=SUM(I2)



another example

Q5.xlsx
ABCDEFGHIJK
1DATEDESCRIBEDEBITCREDITBALANCEREALDATEBALANCEREALNET
220/06/2023INV0014,220.004,220.0019/06/202322,640.001,700.0024,340.00
320/06/2023INV0025,000.009,220.00TOTAL22,640.001,700.0024,340.00
420/06/2023INV0031,920.0011,140.00
520/06/2023INV004270.0011,410.00
620/06/2023INV0054,220.0015,630.00
720/06/2023INV0063,700.0019,330.00
820/06/2023INV0072,460.0021,790.00
920/06/2023INV0081,000.0022,790.00
1020/06/2023EXPENSES44.0022,746.00
1120/06/2023EXPENSES30.0022,716.00
1220/06/2023EXPENSES65.0022,651.00
1320/06/2023EXPENSES11.0022,640.00
1420/06/2023INV0095,000.0027,640.00-3,300.00
15TOTAL27,790.00150.0027,640.00
ASS
Cell Formulas
RangeFormula
K2K2=I2+J2
I3:K3I3=SUM(I2)



should be
Q5.xlsx
ABCDEFGHIJK
1DATEDESCRIBEDEBITCREDITBALANCEREALDATEBALANCEREALNET
220/06/2023INV0014,220.004,220.0019/06/202322,640.001,700.0024,340.00
320/06/2023INV0025,000.009,220.0020/06/202327,640.00-3,300.0024,340.00
420/06/2023INV0031,920.0011,140.00TOTAL50,280.00-1,600.0048,680.00
520/06/2023INV004270.0011,410.00
620/06/2023INV0054,220.0015,630.00
720/06/2023INV0063,700.0019,330.00
820/06/2023INV0072,460.0021,790.00
920/06/2023INV0081,000.0022,790.00
1020/06/2023EXPENSES44.0022,746.00
1120/06/2023EXPENSES30.0022,716.00
1220/06/2023EXPENSES65.0022,651.00
1320/06/2023EXPENSES11.0022,640.00
1420/06/2023INV0095,000.0027,640.00-3,300.00
15TOTAL27,790.00150.0027,640.00
ASS
Cell Formulas
RangeFormula
K2:K3K2=I2+J2
I4:K4I4=SUM(I2:I3)

I just would macro , I don't want solution by Power Query or Pivot Table at all.
thanks
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try the following macro. Consider that each time you run the macro, one row will be incremented in columns H to K.
The macro also assumes that the headers in H1 to K1 already exist.
The macro puts the result of the formulas, but if you want the formulas to remain, then remove from the macro the 2 lines that say ".Value = .Value"

The macro:
VBA Code:
Sub populate_amounts()
  Dim nRow As Long, lr As Long
  Dim lRow As Long, lTot As Long
 
  Application.ScreenUpdating = False
 
  nRow = Range("A:A").Find("TOTAL", , xlValues, xlWhole, , , False).Row
  lr = Range("H" & Rows.Count).End(3).Row + 1
  lRow = IIf(Range("H" & lr - 1).Value = "TOTAL", lr - 1, lr)
  lTot = lRow + 1
 
  Range("A2").Copy
  Range("H" & lRow).PasteSpecial xlPasteFormats
  Range("C2").Copy
  Range("I" & lRow).Resize(2, 3).PasteSpecial xlPasteFormats
 
  Range("H" & lRow).Value = Range("A" & nRow - 1).Value
  Range("I" & lRow).Value = Range("E" & nRow).Value
  Range("F" & nRow - 1).Copy Range("J" & lRow)
  With Range("K" & lRow)
    .Formula = "=I" & lRow & "+J" & lRow
    .Value = .Value
  End With
  
  Range("A" & nRow).Copy Range("H" & lTot)
  With Range("I" & lTot & ":K" & lTot)
    .Formula = "=SUM(I2:I" & lRow & ")"
    .Value = .Value
    .Font.Bold = True
  End With

  Application.ScreenUpdating = True
End Sub

----- --
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 0
Perfect !
one row will be incremented in columns H to K.
umm,
I would ask if it's possible to avoid repeating to the bottom for the same date when run the macro every time.
if you have better idea to do that I'm all my ears.
if you don't have any idea. I have idea.
I just suggest to use DATE(TODAY) as condition
so if I have dates in column H 05/09/2024 , 06/09/2024, 07/09/2024
then should ignore data for dates 05/09/2024 , 06/09/2024 because depends on DATE(TODAY)
and will replace data for date 07/09/2024.
if you see it's hard to do that or impossible , just forget it .
also may you make bold for TOTAL word in last row ,please?
 
Upvote 0
I would ask if it's possible to avoid repeating to the bottom for the same date when run the macro every time.
Everything is possible (or almost everything), if you explain it from the beginning.


1725705307827.png

In your second example, what you do is put a second record (20/06/2023) after the first record (19/06/2023), by the way, that date is very far from DATE(TODAY). So, continuing with your second example, what you do is put a second record.

so if I have dates in column H 05/09/2024 , 06/09/2024, 07/09/2024
then should ignore data for dates 05/09/2024 , 06/09/2024 because depends on DATE(TODAY)
and will replace data for date 07/09/2024.
I highlighted "and will replace data for date 07/09/2024", because it seems like a good idea, but it would be more helpful if you wrote it in your OP.


also may you make bold for TOTAL word in last row ,please?
Returning to your examples, the word "TOTAL" that you have in column A is in BOLD, and that format is what the macro takes for the "TOTAL" in the last row.

I just suggest to use DATE(TODAY) as condition
Taking your suggestion, try the following macro:

VBA Code:
Sub populate_amounts()
  Dim nRow As Long, lr As Long
  Dim lRow As Long, lTot As Long
  Dim f As Range
  Dim nToday As Date
  
  Application.ScreenUpdating = False
  
  nRow = Range("A:A").Find("TOTAL", , xlValues, xlWhole, , , False).Row
  nToday = Range("A2").Value
  
  If nToday <> Date Then
    MsgBox "The date does not correspond to today"
    Exit Sub
  End If
  
  lr = Range("H" & Rows.Count).End(3).Row + 1
  Set f = Range("H:H").Find(nToday, , xlValues, xlWhole)
  If Not f Is Nothing Then
    lRow = f.Row
    lTot = lr - 1
  Else
    lRow = IIf(Range("H" & lr - 1).Value = "TOTAL", lr - 1, lr)
    lTot = lRow + 1
  End If
  
  Range("A2").Copy
  Range("H" & lRow).PasteSpecial xlPasteFormats
  Range("C2").Copy
  Range("I" & lRow).Resize(2, 3).PasteSpecial xlPasteFormats
  
  Range("H" & lRow).Value = Range("A" & nRow - 1).Value
  Range("I" & lRow).Value = Range("E" & nRow).Value
  Range("F" & nRow - 1).Copy Range("J" & lRow)
  With Range("K" & lRow)
    .Formula = "=I" & lRow & "+J" & lRow
    .Value = .Value
  End With
    
  Range("A" & nRow).Copy Range("H" & lTot)
  With Range("I" & lTot & ":K" & lTot)
    .Formula = "=SUM(I2:I" & lRow & ")"
    .Value = .Value
    .Offset(0, -1).Resize(1, 4).Font.Bold = True
  End With

  Application.ScreenUpdating = True
End Sub

🧙‍♂️
 
Upvote 0
Solution
thanks again and sorry I don't mentioned from the beginning.
your code works as I would but when DATE (TODAY) is in column A should add in column H:K
based on the code will not add .
 
Upvote 0
your code works as I would but when DATE (TODAY) is in column A should add in column H:K
based on the code will not add .
:unsure:

Did you try the code?
For me it works, if today's date exists it updates the values and also updates the Total.
If it doesn't exist, then it adds a new row, and of course also updates the Total row.

Before the macro:
1725722417118.png

After the macro:
1725722449934.png
You can see that the macro added the row with today (09/07/2024).
And it updates the totals and also puts them in bold.

;)
 
Last edited:
Upvote 0
sorry it was 2023 year , not 2024!:eek:
your code works excellently .
many thanks.;)
 
Upvote 1

Forum statistics

Threads
1,221,507
Messages
6,160,219
Members
451,631
Latest member
coffiajoseph

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