Macro to compare old prices with new prices for stock and show(-/+)

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2016
Platform
  1. Windows
Hello,
in QW sheet will be multiple columns contain prices and every time I will add new column contain new prices .
in STOCK sheet contains data in columns A:E
what I want create report in columns G:L with formatting and borders by brings the prices from last column in QW sheet and put in column J , as to column K=column I* column J
column L =column K- column E
in lastrow for column G when amount is minus in column L then populate LOSE word ,amount is plus in column L then populate PROFIT word.
if there is empty cell in last column in QW when brings price from previous column(not the first column) for instance if there is price in columnL,M,N and if the id doesn't existed price in column N then will brings from column M .
when create report will match BATCH column for two sheets , also if there is ID in STOCK sheet and it's not existed in QW sheet then will show as in STOCK sheet.


subtra.xlsm
JKLM
1ITEMBATCH01/11/202402/11/2024
21APP PP FRUIT12.0011.00
32BAN FF FRUIT9.00
43CU CMC VEG11.0011.00
54FRU BANN MU22.0023.00
65PI PIA FRUIT21.0021.00
76PO PTT VEG12.0011.00
87TO TMA VEG5.004.00
98TE TEE FOOD23.0018.00
109TRB POT BNG22.0024.00
QW




subtra.xlsm
ABCDEFGHIJKLMNO
1ITEMBATCHQTYUNIT PRICETOTAL
21TO TMA VEG10.005.0050.00
32PO PTT VEG20.0010.00200.00
43BAN FF FRUIT100.0012.001,200.00
54APP PP FRUIT100.0010.001,000.00
65PI PIA FRUIT12.0022.00264.00
76TE TEE FOOD100.0023.002,300.00
87CU CMC VEG120.0021.002,520.00
98TRB POT BNG100.0022.002,200.00
109FRU BANN MU124.0022.002,728.00
11
12
13
14
STOCK



result
subtra.xlsm
ABCDEFGHIJKLMNO
1ITEMBATCHQTYUNIT PRICETOTALITEMBATCHQTYUNIT PRICETOTALD/P
21TO TMA VEG10.005.0050.001TO TMA VEG10.004.0040.00-10.00
32PO PTT VEG20.0010.00200.002PO PTT VEG20.0011.00220.0020.00
43BAN FF FRUIT100.0012.001,200.003BAN FF FRUIT100.009.00900.00-300.00
54APP PP FRUIT100.0010.001,000.004APP PP FRUIT100.0011.001,100.00100.00
65PI PIA FRUIT12.0022.00264.005PI PIA FRUIT12.0021.00252.00-12.00
76TE TEE FOOD100.0023.002,300.006TE TEE FOOD100.0018.001,800.00-500.00
87CU CMC VEG120.0021.002,520.007CU CMC VEG120.0011.001,320.00-1,200.00
98TRB POT BNG100.0022.002,200.008TRB POT BNG100.0024.002,400.00200.00
109FRU BANN MU124.0022.002,728.009FRU BANN MU124.0023.002,852.00124.00
11LOSE-1,578.00
12
13
STOCK
Cell Formulas
RangeFormula
L2:L10L2=K2-E2
L11L11=SUM(L2:L10)

I need macro , I don't PQ or PT , formulas
thanks
 
this is not existed in QW sheet
My comment in post #28 is about the fact that in your "right" results in post #27 you don't seem to have averaged any of the prices (or at least not most of them) :confused:

You have marked post #13 as the correct solution. Is that still the case?
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
First of all, is my code working for your initial requirement or not?
yes.
If it is working and change the UNIT PRICE to average, how do you want to calculate the TOTAL?
as in original requirement should QTY*UNIT PRICE.

Better upload your workbook with your EXACT desired results to avoid any confusion.

Dropbox
 
Upvote 0
My comment in post #28 is about the fact that in your "right" results in post #27 you don't seem to have averaged any of the prices (or at least not most of them) :confused:
I know , but I would fix the problem based on requirements OP first of all and I will ask about average prices later.;)
that was in yesterday but suddenly today doesn't work well for new ID in STOCK sheet , I no know why !
see the row11 how show value in last COL(L). . it supposes zero.
you can check the attaching in post#42.
sub.xlsm
ABCDEFGHIJKL
1ITEMBATCHQTYUNIT PRICETOTALITEMBATCHQTYUNIT PRICETOTALD/P
21TO TMA VEG10.005.0050.001TO TMA VEG10.004.5045.00-5.00
32PO PTT VEG20.0010.00200.002PO PTT VEG20.0010.50210.0010.00
43BAN FF FRUIT100.0012.001,200.003BAN FF FRUIT100.0010.501,050.00-150.00
54APP PP FRUIT100.0010.001,000.004APP PP FRUIT100.0010.501,050.0050.00
65PI PIA FRUIT12.0022.00264.005PI PIA FRUIT12.0021.50258.00-6.00
76TE TEE FOOD100.0023.002,300.006TE TEE FOOD100.0020.502,050.00-250.00
87CU CMC VEG120.0021.002,520.007CU CMC VEG120.0016.001,920.00-600.00
98TRB POT BNG100.0022.002,200.008TRB POT BNG100.0023.002,300.00100.00
109FRU BANN MU124.0022.002,728.009FRU BANN MU124.0022.502,790.0062.00
1110FRU TT MU124.0018.002,728.0010FRU TT MU124.0018.002,232.00-496.00
12LOSE-1,285.00
13
STOCK
 
Upvote 0
What value expect in J11
not only J11 also K11 should brings unit price from column D and in K11 =QTY*UNIT price and in L11 will be zero
 
Upvote 0
TRy.
VBA Code:
Sub CreateTable()
Application.ScreenUpdating = False
Dim Lrq&, Lrs&, Lcq&, T&, Ta&
Dim Frng1 As Range, Frng2 As Range

Lrq = Sheets("QW").Range("J" & Rows.Count).End(xlUp).Row
Lcq = Sheets("QW").Cells(1, Columns.Count).End(xlToLeft).Column
With Sheets("STOCK")
Lrs = .Range("A" & Rows.Count).End(xlUp).Row
.Range("G1").CurrentRegion.Clear
.Range("A1").CurrentRegion.Resize(, 3).Copy .Range("G1")
For T = 2 To Lrs
Set Frng1 = Sheets("QW").Range("K2:K" & Lrq).Find(.Range("H" & T).Value)
If Not Frng1 Is Nothing Then
    For Ta = Lcq To Range("L1").Column Step -1
    If Sheets("QW").Cells(Frng1.Row, Ta) <> "" Then
    .Range("J" & T) = Sheets("QW").Cells(Frng1.Row, Ta)
    Exit For
    End If
    Next Ta
Else
.Range("I" & T) = .Range("C" & T)
.Range("J" & T) = .Range("D" & T)
End If
Next T
.Range("K2:K" & Lrs).Fo rmula = "=I2*J2"
.Range("L2:L" & Lrs).Formula = "=K2-E2"
.Range("G1:L1").Copy .Range("J1")
.Range("J1:L1") = Array("UNIT PRICE", "TOTAL", "D/P")
.Range("L" & Lrs + 1).Formula = "=Sum(L2:L" & Lrs & ")"
If .Range("L" & Lrs + 1) < 0 Then .Range("G" & Lrs + 1) = "LOSS" Else .Range("G" & Lrs + 1) = "GAIN"
.Range("H1").CurrentRegion.Borders.LineStyle = xlContinuous
End With

Application.ScreenUpdating = True
End Sub
 
Upvote 0
see the row11 how show value in last COL(L). . it supposes zero.
It is not zero because, by my reckoning, the data in columns A:E is incorrect.
C11 = 124.00
D11 = 18.00
C11 * D11 = 124.00 * 18.00 = 2,232
So why does your E11 show 2,728? That doesn't make sense to me. :huh:

How did the values in column E of 'STOCK' get there?
I all the other rows besides row 11, the Column E value could be obtained by multiplying the Qty * Unit Price. Why is row 11 different?

KalilMe_sub.xlsm
CDEF
1QTYUNIT PRICETOTAL
210.005.0050.0050.00
320.0010.00200.00200.00
4100.0012.001,200.001,200.00
5100.0010.001,000.001,000.00
612.0022.00264.00264.00
7100.0023.002,300.002,300.00
8120.0021.002,520.002,520.00
9100.0022.002,200.002,200.00
10124.0022.002,728.002,728.00
11124.0018.002,728.002,232.00
STOCK
Cell Formulas
RangeFormula
F2:F11F2=C2*D2
 
Last edited:
Upvote 0
I have modified code. Pl try.
.
VBA Code:
Sub CreateTable()
Application.ScreenUpdating = False
Dim Lrq&, Lrs&, Lcq&, T&, Ta&
Dim Frng1 As Range, Frng2 As Range

Lrq = Sheets("QW").Range("J" & Rows.Count).End(xlUp).Row
Lcq = Sheets("QW").Cells(1, Columns.Count).End(xlToLeft).Column
With Sheets("STOCK")
Lrs = .Range("A" & Rows.Count).End(xlUp).Row
.Range("G1").CurrentRegion.Clear
.Range("A1").CurrentRegion.Resize(, 3).Copy .Range("G1")
For T = 2 To Lrs
Set Frng1 = Sheets("QW").Range("K2:K" & Lrq).Find(.Range("H" & T).Value)
If Not Frng1 Is Nothing Then
    For Ta = Lcq To Range("L1").Column Step -1
    If Sheets("QW").Cells(Frng1.Row, Ta) <> "" Then
    .Range("J" & T) = Sheets("QW").Cells(Frng1.Row, Ta)
    Exit For
    End If
    Next Ta
Else
.Range("J" & T) = .Range("D" & T)
End If
Next T
.Range("K2:K" & Lrs).Formula = "=I2*J2"
.Range("L2:L" & Lrs).Formula = "=K2-E2"
.Range("G1:L1").Copy .Range("J1")
.Range("J1:L1") = Array("UNIT PRICE", "TOTAL", "D/P")
.Range("L" & Lrs + 1).Formula = "=Sum(L2:L" & Lrs & ")"
If .Range("L" & Lrs + 1) < 0 Then .Range("G" & Lrs + 1) = "LOSS" Else .Range("G" & Lrs + 1) = "GAIN"
.Range("H1").CurrentRegion.Borders.LineStyle = xlContinuous
End With

Application.ScreenUpdating = True
End Sub
 
Upvote 0
So why does your E11 show 2,728? That doesn't make sense to me. :huh:
OMG!:eek::eek:
this is really bad, sorry!🙏
this happens when I test new ID by pull down without calculation.
so your suggestion still solve my requirements.
look you see I still post after you solve this thread. I want to you know after see kvsrinivasamurthy's , Fuji comments just guys try to share their solution and I inform them to know this is what I want or not.
moreover variety solutions could be useful with different way, that's it.:)
 
Upvote 0
Where is your desired result with the average unit cost?
 
Upvote 0

Forum statistics

Threads
1,223,881
Messages
6,175,159
Members
452,615
Latest member
bogeys2birdies

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