exclude TOTAL row & add data before TOTAL row and change calculation TOTAL row

Alaa mg

Active Member
Joined
May 29, 2021
Messages
375
Office Version
  1. 2019
Hello
I need copy data from CUSTOMERS sheet to BALANCES sheet before TOTAL row with exclude TOTAL row for CUSTOMERS sheet when copy data ,also change TOTAL row in BALANCES sheet after bring data from CUSTOMERS sheet

ALL.xlsm
ABCDEF
1ITEMDETAILSNAMEDEBITCREDITBALANCE
21OPENING BALANCE 27/07/2023LLA6,000.007,000.00-1,000.00
32OPENING BALANCE 27/07/2024MLA6,000.0010,000.00-4,000.00
4TOTAL12,000.0017,000.00-5,000.00
BALANCES


ALL.xlsm
ABCDE
1ITEMNAMEDEBITCREDITBALANCE
21ALAA4,000.001,000.003,000.00
32MAAL2,000.001,200.00800.00
4TOTAL6,000.002,200.003,800.00
CUSTOMERS


result

ALL.xlsm
ABCDEF
1ITEMDETAILSNAMEDEBITCREDITBALANCE
21OPENING BALANCE 27/07/2023LLA6,000.007,000.00-1,000.00
32OPENING BALANCE 27/07/2023MLA6,000.0010,000.00-4,000.00
43OPENING BALANCE 27/07/2023ALAA4,000.001,000.003,000.00
54OPENING BALANCE 27/07/2023MAAL2,000.001,200.00800.00
6TOTAL18,000.0019,200.00-1,200.00
BALANCES

so in TOTAL row should sum D:F and when bring data from CUSTOMERS sheet should add OPEN BALANCE word & DATE in column B
my real project will be 3500 rows for each sheet
thanks
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
See if this could work for you.

First, put this formula in the TOTAL row of column D in 'BALANCES' and copy across to column F

Alaa mg.xlsm
ABCDEF
1ITEMDETAILSNAMEDEBITCREDITBALANCE
21OPENING BALANCE 27/07/2023LLA60007000-1000
32OPENING BALANCE 27/07/2024MLA600010000-4000
4TOTAL1200017000-5000
BALANCES
Cell Formulas
RangeFormula
D4:F4D4=SUM(D2:INDEX(D:D,ROW()-1))


Now run this code

VBA Code:
Sub Add_New_Data()
  Dim nr As Long, rws As Long
  Dim rCust As Range
  
  With Sheets("CUSTOMERS")
    Set rCust = .Range("B2", .Range("E" & Rows.Count).End(xlUp).Offset(-1))
    rws = rCust.Rows.Count
  End With
  Application.ScreenUpdating = False
  With Sheets("BALANCES")
    nr = .Range("A" & Rows.Count).End(xlUp).Row
    .Rows(nr).Resize(rws).Insert
    With .Range("A" & nr).Resize(rws)
      rCust.Copy Destination:=.Offset(, 2)
      .Offset(, 1).Value = "OPENING BALANCE " & Format(Date, "dd/mm/yyyy")
      .FormulaR1C1 = "=R[-1]C+1"
      .Value = .Value
    End With
  End With
  Application.ScreenUpdating = True
End Sub

Here is my sheet after I have done that.

Alaa mg.xlsm
ABCDEF
1ITEMDETAILSNAMEDEBITCREDITBALANCE
21OPENING BALANCE 27/07/2023LLA60007000-1000
32OPENING BALANCE 27/07/2024MLA600010000-4000
43OPENING BALANCE 30/07/2023ALAA400010003000
54OPENING BALANCE 30/07/2023MAAL20001200800
6TOTAL1800019200-1200
BALANCES
Cell Formulas
RangeFormula
D6:F6D6=SUM(D2:INDEX(D:D,ROW()-1))
 
Upvote 0
Hi Peter
unfortunately the BLANACES sheet will create by another macro without any formula , if I put the formula my macro will delete it.
is there way without write formula ,please?
 
Upvote 0
Try this

Rich (BB code):
Sub Add_New_Data_v2()
  Dim nr As Long, rws As Long
  Dim rCust As Range
  
  With Sheets("CUSTOMERS")
    Set rCust = .Range("B2", .Range("E" & Rows.Count).End(xlUp).Offset(-1))
    rws = rCust.Rows.Count
  End With
  Application.ScreenUpdating = False
  With Sheets("BALANCES")
    nr = .Range("A" & Rows.Count).End(xlUp).Row
    .Rows(nr).Resize(rws).Insert
    With .Range("A" & nr).Resize(rws)
      rCust.Copy Destination:=.Offset(, 2)
      .Offset(, 1).Value = "OPENING BALANCE " & Format(Date, "dd/mm/yyyy")
      .FormulaR1C1 = "=R[-1]C+1"
      .Value = .Value
    End With
    With .Range("D" & nr + rws).Resize(, 3)
      .FormulaR1C1 = "=sum(R2C:R[-1]C)"
      .Value = .Value
    End With
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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