copy values after TOTAL row and subtract discount from it for two sheets

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
354
Office Version
  1. 2016
Platform
  1. Windows
Hello
I have two sheets purchase ,debit
sheet PURCHASE contains invoices numbers in column B and summing values for each invoice by TOTAL in lastrow . sheets DEBIT also contains invoices numbers in column B . sheet DISCOUNT this is important when copy valuse from sheet DISCOUNT to sheets purchase, DEBIT . sheet PURCHASE should copy values to after TOTAL row from sheet DISCOUNT ( should summing values and merge dupilcates invoices in sheet DISCOUNT when copy to sheet PURCHASE ) and add NET row to subtract TOTAL from DISCOUNT and when show row NET but in sheet DEBIT should add after last row contains the same invoice no in column B with repeat client name and put word DISCOUNT in column B and put the value in column E .
Result (1) (1).xlsx
ABCDEFGHIJ
1ITEMINVOICE NOORDER NOCO-ITFOODTT-MMNORT-WWQTYPRICETOTAL
21INV00-001DEL00-001COR-FF1FRBANANATT400.00230.00TRY 92,000.00
32INV00-001DEL00-002COR-FF2FRAPPLELL100.00234.00TRY 23,400.00
43INV00-001DEL00-003COR-FF3FRPEARNN10.00233.00TRY 2,330.00
5TOATLTRY 117,730.00
61INV00-002DEL00-004COR-FF4FRBANANAQQ20.00300.00TRY 6,000.00
7TOATLTRY 6,000.00
81INV00-003DEL00-005COR-FF5VEGTOMATOSS22.00200.00TRY 4,400.00
9TOATLTRY 4,400.00
101INV00-004DEL00-006COR-FF6VEG1TOMATOAA22.00123.00TRY 2,706.00
112INV00-004DEL00-007COR-FF7VEG2TOMATOAA33.00234.00TRY 7,722.00
12TOATLTRY 10,428.00
PURCHASE
Cell Formulas
RangeFormula
J10:J11,J8,J6,J2:J4J2=I2*H2
J5J5=SUM(J2:J4)
J7,J9J7=J6
J12J12=SUM(J10:J11)





Result (1) (1).xlsx
ABCDE
1ITEMINVOICE NOCLIENTDEBITCREDIT
21INV00-001KALIL12000001000
32INV00-002KALIL2
43INV00-003KALIL31000001000
54INV00-004KALIL4
65INV00-005KALIL52500001000
76INV00-006KALIL6
8
DEBIT





Result (1) (1).xlsx
ABCDEFGHIJ
1ITEMINVOICE NODISCOUNTCO-ITFOODTT-MMNORT-WWQTYPRICETOTAL
21INV00-001DIS00-001COR-FF1FRBANANATT400.00220.00TRY 88,000.00
32INV00-001DIS00-002COR-FF2FRAPPLELL100.00213.00TRY 21,300.00
43INV00-002DIS00-003COR-FF4FRBANANAQQ20.00220.00TRY 4,400.00
DISCOUNT
Cell Formulas
RangeFormula
J2:J4J2=I2*H2

result in sheet PURCHASE

Result (1) (1).xlsx
ABCDEFGHIJ
1ITEMINVOICE NOORDER NOCO-ITFOODTT-MMNORT-WWQTYPRICETOTAL
21INV00-001DEL00-001COR-FF1FRBANANATT400.00230.00TRY 92,000.00
32INV00-001DEL00-002COR-FF2FRAPPLELL100.00234.00TRY 23,400.00
43INV00-001DEL00-003COR-FF3FRPEARNN10.00233.00TRY 2,330.00
5TOTALTRY 117,730.00
6DISCOUNTTRY 109,300.00
7NETTRY 8,430.00
81INV00-002DEL00-004COR-FF4FRBANANAQQ20.00300.00TRY 6,000.00
9TOTALTRY 6,000.00
10DISCOUNTTRY 4,400.00
11NETTRY 1,600.00
121INV00-003DEL00-005COR-FF5VEGTOMATOSS22.00200.00TRY 4,400.00
13TOTALTRY 4,400.00
141INV00-004DEL00-006COR-FF6VEG1TOMATOAA22.00123.00TRY 2,706.00
152INV00-004DEL00-007COR-FF7VEG2TOMATOAA33.00234.00TRY 7,722.00
16TOTALTRY 10,428.00
PURCHASE
Cell Formulas
RangeFormula
J14:J15,J12,J8,J2:J4J2=I2*H2
J5J5=SUM(J2:J4)
J7,J11J7=J5-J6
J16J16=SUM(J14:J15)


RESULT IN SHEET DEBIT

Result (1) (1).xlsx
ABCDE
1ITEMINVOICE NOCLIENTDEBITCREDIT
21INV00-001KALIL12000001000
3DISCOUNTKALIL1TRY 109,300.00
42INV00-002KALIL2
5DISCOUNTKALIL2TRY 4,400.00
63INV00-003KALIL31000001000
74INV00-004KALIL4
85INV00-005KALIL52500001000
96INV00-006KALIL6
DEBIT
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
This is VBA solution.
If you are new VBA user, try following steps:
* Alt-F11 to open VBA window
* Insert/module
* paste the code
* Hit F5 (or play button) to run
VBA Code:
Option Explicit
Sub TEST()
Dim lr&, i&, j&, k&, c&, rng, arr(), dic As Object, key, cell As Range
Set dic = CreateObject("Scripting.Dictionary")
With Sheets("PURCHASE")
    lr = .Cells(Rows.Count, "A").End(xlUp).Row
    rng = .Range("A2:J" & lr).Value
    For i = 1 To lr - 1
        If Not dic.exists(rng(i, 2)) And rng(i, 2) <> "" Then
            With Sheets("DISCOUNT")
                c = WorksheetFunction.SumIf(.Range("B:B"), rng(i, 2), .Range("J:J"))
            End With
            dic.Add rng(i, 2), c
        End If
    Next
    ReDim arr(1 To 1000000, 1 To 10)
    For i = 1 To UBound(rng)
        k = k + 1
        For j = 1 To 10
            arr(k, j) = rng(i, j)
        Next
        If rng(i, 1) = "TOTAL" Then
            k = k + 1
            For Each key In dic.keys
                If rng(i - 1, 2) = key Then
                    arr(k, 1) = "DISCOUNT": arr(k, 10) = dic(key)
                    Exit For
                End If
            Next
            k = k + 1
            arr(k, 1) = "NET": arr(k, 10) = arr(k - 2, 10) - arr(k - 1, 10)
        End If
            
    Next
    With .Range("A2:J1000000")
        .ClearContents
        .Font.Bold = False
    End With
    .Range("A2").Resize(k, 10).Value = arr
    For Each cell In .Range("A2:A" & k + 1)
        If InStr(1, "TOTALDISCOUNTNET", cell.Value) Then cell.Resize(1, 10).Font.Bold = True
    Next
    .Columns("H:J").NumberFormat = "#,##0.00"
    .Columns("A:J").AutoFit
End With
ReDim arr(1 To 1000000, 1 To 10)
With Sheets("DEBIT")
    lr = .Cells(Rows.Count, "A").End(xlUp).Row
    rng = .Range("A2:E" & lr).Value
    k = 0
    For i = 1 To UBound(rng)
        k = k + 1
        For j = 1 To 5
            arr(k, j) = rng(i, j)
        Next
        For Each key In dic.keys
            If rng(i, 2) = key And dic(key) > 0 Then
                k = k + 1
                arr(k, 2) = "DISCOUNT": arr(k, 3) = rng(i, 3): arr(k, 5) = dic(key)
                Exit For
            End If
        Next
    Next
    .Range("A2:E1000000").ClearContents
    .Range("A2").Resize(k, 5).Value = arr
End With
End Sub
 
Upvote 0
great !
actaullay I'm gald to see my request and try to help me.
your code works excellently , but I have two questions if you don't mind
first of all the code when run more than one time it will copy to the bottom repeatedly , shouldn't do that because I will add new data in sheet DISCOUNT then will call every time your code as I need it when update sheet DISCOUNT .
you can see the pictures , will repeat the rows DISCOUNT and NET for the same invoice if run the macro repeatedly .
Result (1) (1).xlsm
ABCDEFGHIJ
1ITEMINVOICE NOORDER NOCO-ITFOODTT-MMNORT-WWQTYPRICETOTAL
21INV00-001DEL00-001COR-FF1FRBANANATT400.00230.0092,000.00
32INV00-001DEL00-002COR-FF2FRAPPLELL100.00234.0023,400.00
43INV00-001DEL00-003COR-FF3FRPEARNN10.00233.002,330.00
5TOTAL117,730.00
6DISCOUNT109,300.00
7NET8,430.00
8DISCOUNT109,300.00
9NET8,430.00
10DISCOUNT109,300.00
11NET8,430.00
121INV00-002DEL00-004COR-FF4FRBANANAQQ20.00300.006,000.00
13TOTAL6,000.00
14DISCOUNT4,400.00
15NET1,600.00
16DISCOUNT4,400.00
17NET1,600.00
18DISCOUNT4,400.00
19NET1,600.00
201INV00-003DEL00-005COR-FF5VEGTOMATOSS22.00200.004,400.00
21TOTAL4,400.00
22DISCOUNT0.00
23NET4,400.00
24DISCOUNT0.00
25NET4,400.00
26DISCOUNT0.00
27NET4,400.00
281INV00-004DEL00-006COR-FF6VEG1TOMATOAA22.00123.002,706.00
292INV00-004DEL00-007COR-FF7VEG2TOMATOAA33.00234.007,722.00
30TOTAL10,428.00
31DISCOUNT0.00
32NET10,428.00
33DISCOUNT0.00
34NET10,428.00
35DISCOUNT0.00
36NET10,428.00
PURCHASE


Result (1) (1).xlsm
ABCDE
1ITEMINVOICE NOCLIENTDEBITCREDIT
21INV00-001KALIL12000001000
3DISCOUNTKALIL1109300
4DISCOUNTKALIL1109300
5DISCOUNTKALIL1109300
62INV00-002KALIL2
7DISCOUNTKALIL24400
8DISCOUNTKALIL24400
9DISCOUNTKALIL24400
103INV00-003KALIL31000001000
114INV00-004KALIL4
125INV00-005KALIL52500001000
136INV00-006KALIL6
DEBIT

second question I tested the code just for 4500 rows for sheet PURCHASE and 20 rows for sheet DEBIT but the code seems slow it gives me about 3.9 sec . is it normal?
thanks
 
Upvote 0
Hope it is better:
0.85s with 1000 rows of sample
VBA Code:
Option Explicit
Sub TEST()
Dim lr&, i&, j&, k&, c&, rng, arr(), dic As Object, key, t
Set dic = CreateObject("Scripting.Dictionary")
t = Timer
With Sheets("PURCHASE")
    lr = .Cells(Rows.Count, "A").End(xlUp).Row
    rng = .Range("A2:J" & lr).Value
    For i = 1 To lr - 1
        If Not dic.exists(rng(i, 2)) And rng(i, 2) <> "" Then
            With Sheets("DISCOUNT")
                c = WorksheetFunction.SumIf(.Range("B:B"), rng(i, 2), .Range("J:J"))
            End With
            dic.Add rng(i, 2), c
        End If
    Next
    ReDim arr(1 To 1000000, 1 To 10)
    For i = 1 To UBound(rng)
        If rng(i, 2) <> "" Then
            k = k + 1
            For j = 1 To 10
                arr(k, j) = rng(i, j)
            Next
        ElseIf rng(i, 1) = "TOTAL" Then
            k = k + 1: arr(k, 1) = rng(i, 1): arr(k, 10) = rng(i, 10)
            For Each key In dic.keys
                If rng(i - 1, 2) = key Then
                    k = k + 1
                    arr(k, 1) = "DISCOUNT": arr(k, 10) = dic(key)
                    Exit For
                End If
            Next
            k = k + 1
            arr(k, 1) = "NET": arr(k, 10) = arr(k - 2, 10) - arr(k - 1, 10)
        End If
    Next
    .Range("A2:J1000000").ClearContents
    .Range("A2").Resize(k, 10).Value = arr
    .Columns("A:J").AutoFit
End With
ReDim arr(1 To 1000000, 1 To 10)
With Sheets("DEBIT")
    lr = .Cells(Rows.Count, "A").End(xlUp).Row
    rng = .Range("A2:E" & lr).Value
    k = 0
    For i = 1 To UBound(rng)
        If rng(i, 2) <> "DISCOUNT" And rng(i, 2) <> "" Then
            k = k + 1
            For j = 1 To 5
                arr(k, j) = rng(i, j)
            Next
            k = k + 1
            arr(k, 2) = "DISCOUNT"
            For Each key In dic.keys
                If rng(i, 2) = key And dic(key) > 0 Then
                    arr(k, 3) = rng(i, 3): arr(k, 5) = dic(key)
                    Exit For
                End If
            Next
        End If
    Next
    .Range("A2:E1000000").ClearContents
    .Range("A2").Resize(k, 5).Value = arr
End With
MsgBox Timer - t
End Sub
 
Upvote 0
thanks but the code doesn't become to give right result in sheet PURCHSE will delete TOTAL row and in sheet DEBIT will insert empty DISCOUNT row even there some invoices are not existed in sheet DISCOUNT . should just deal with the invoices numbers are existed in sheet DISCOUNT and ignore insert empty DISCOUNT row for the invoices numbers are not existed in sheet DISCOUNT .
this is what I got
Result (1) (1).xlsm
ABCDEFGHIJ
1ITEMINVOICE NOORDER NOCO-ITFOODTT-MMNORT-WWQTYPRICETOTAL
21INV00-001DEL00-001COR-FF1FRBANANATT40023092000
32INV00-001DEL00-002COR-FF2FRAPPLELL10023423400
43INV00-001DEL00-003COR-FF3FRPEARNN102332330
51INV00-002DEL00-004COR-FF4FRBANANAQQ203006000
61INV00-003DEL00-005COR-FF5VEGTOMATOSS222004400
71INV00-004DEL00-006COR-FF6VEG1TOMATOAA221232706
82INV00-004DEL00-007COR-FF7VEG2TOMATOAA332347722
PURCHASE


Result (1) (1).xlsm
ABCDE
1ITEMINVOICE NOCLIENTDEBITCREDIT
21INV00-001KALIL12000001000
3DISCOUNTKALIL1109300
42INV00-002KALIL2
5DISCOUNTKALIL24400
63INV00-003KALIL31000001000
7DISCOUNT
84INV00-004KALIL4
9DISCOUNT
105INV00-005KALIL52500001000
11DISCOUNT
126INV00-006KALIL6
13DISCOUNT
DEBIT


I hope you can fix it .
 
Upvote 0
It's really strange !!!
ok I will create new file and inform you soon what happens for me , but I see you disable this
VBA Code:
'arr(k, 2) = "DISCOUNT"
actually I want to show word DISCOUNT in column B as the first code do that in sheet DEBIT , when I enable and run macro repeatedly then will show error application defined error in this line
VBA Code:
.Range("A2").Resize(k, 5).Value = arr
how can I fix it please?
 
Upvote 0
OK. I forgot to add:
VBA Code:
'arr(k, 2) = "DISCOUNT"
            For Each key In dic.keys
                If rng(i, 2) = key And dic(key) > 0 Then
                    k = k + 1
                    arr(k, 2) = "DISCOUNT": arr(k, 3) = rng(i, 3): arr(k, 5) = dic(key)
                    Exit For
                End If
you can see arr(k,2) = "DISCOUNT" has been moved from outside into dictionary keys loop
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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