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

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
399
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
 
I've found my bad ! as you see in picture1 the word TOTAL is wrong TOATL that's why doesn't show data correctly . but even that still the code has problem in sheet DEBIT I added some data in sheet DISCOUNT but really strange !! some invoices doesn't brings values from sheet DISCOUNT to sheet DEBIT for invoices(INV00-003,INV00-006) you can note that in sheet DEBIT.
I hope to see my file soon when you have free time.
TOTAL.xlsm
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
My faul!
Instead of creating dictionary from "DISCOUNT", I have worked with sheet PURCHASE to add items into dictionary!
Try again!
 
Upvote 0
Solution
excellent !

seem to work very greatly , there is no problem so far and I hope continuing until I don't come back and upset you.

so I decided to post your code maybe the file will delete after time . just the others members take advantage from your code, of course after you .

many thanks for your assistance .:)
VBA Code:
Option Explicit
Sub TEST()
Dim lr&, i&, j&, k&, c&, rng, arr(), dic As Object, key
Set dic = CreateObject("Scripting.Dictionary")
With Sheets("DISCOUNT")
    lr = .Cells(Rows.Count, "B").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
            dic.Add rng(i, 2), rng(i, 10)
        Else
            dic(rng(i, 2)) = dic(rng(i, 2)) + rng(i, 10)
        End If
    Next
End With
With Sheets("PURCHASE")
    lr = .Cells(Rows.Count, "A").End(xlUp).Row
    rng = .Range("A2:J" & lr).Value
    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
            c = 0: 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
                    c = c + 1
                    k = k + 1
                    arr(k, 1) = "DISCOUNT": arr(k, 10) = dic(key)
                    Exit For
                End If
            Next
            If c = 0 Then
                k = k + 1
                arr(k, 1) = "DISCOUNT"
            End If
            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
            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
        End If
    Next
    .Range("A2:E1000000").ClearContents
    .Range("A2").Resize(k, 5).Value = arr
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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