compare brands between two sheets and show minus and positive values based on date (today)

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
613
Office Version
  1. 2019
Hello

I want comparing BRAND column between two sheets .

the result will be in ACTUAL sheet in H:J columns based on DATE (TODAY)

so in ACTUAL sheet should merge duplicates BRANDS after that will subtract merged QTY from QTY for the same BRAND in STOCK sheet .

if there is brand =0 between two sheets when subtract from each other no need show . just if show plus or deficit values.
if there are no brands in ACTUAL sheet and there are in STOCK sheet then should show minus values in ACTUAL sheet.

should show based on date(today)
note: when date(today) is 29/08/2024 then should keep data were date(today) "28/08/2024"
rather than don't delete old dates when populate data for date (today).

TGR.xlsm
ABC
1ITEMBRANDQTY
21BS 1200R20 G580 JAP200.00
32BS 1200R20 G580 THI150.00
43BS 205/70R15C R623 THI120.00
54DT 1200R20 DS 50 THI100.00
65BS 750R16 8PR R230 JAP200.00
76BS 750R16 10PR VSJ JAP100.00
87BSJ 1400R20 VSJ JAP120.00
98BSJ 1400R20 R180 JAP700.00
109BS 315/R22.5 R184 THI500.00
1110BS 1200R24 G580 JAP150.00
12
STOCK



TGR.xlsm
BCDEFGHIJ
1DATEINVOICE NOCLIENT NOBRANDQTY
228/08/2024BSJ1000BBBFT-00BS 1200R20 G580 JAP150.00
328/08/2024BSJ1001BBBFT-00BS 1200R20 G580 JAP20.00
428/08/2024BSJ1001BBBFT-01BS 1200R20 G580 THI160.00
528/08/2024BSJ1001BBBFT-01DT 1200R20 DS 50 THI80.00
628/08/2024BSJ1002BBBFT-02DT 1200R20 DS 50 THI20.00
ACTUAL




result
TGR.xlsm
BCDEFGHIJ
1DATEINVOICE NOCLIENT NOBRANDQTYDATEBRANDQTY
228/08/2024BSJ1000BBBFT-00BS 1200R20 G580 JAP150.0028/08/2024BS 1200R20 G580 JAP-30.00
328/08/2024BSJ1001BBBFT-00BS 1200R20 G580 JAP20.0028/08/2024BS 1200R20 G580 THI10.00
428/08/2024BSJ1001BBBFT-01BS 1200R20 G580 THI160.0028/08/2024BS 205/70R15C R623 THI-120.00
528/08/2024BSJ1001BBBFT-01DT 1200R20 DS 50 THI80.0028/08/2024BS 750R16 8PR R230 JAP-200.00
628/08/2024BSJ1002BBBFT-02DT 1200R20 DS 50 THI20.0028/08/2024BS 750R16 10PR VSJ JAP-100.00
728/08/2024BSJ 1400R20 VSJ JAP-120.00
828/08/2024BSJ 1400R20 R180 JAP-700.00
928/08/2024BS 315/R22.5 R184 THI-500.00
1028/08/2024BS 1200R24 G580 JAP-150.00
ACTUAL
 

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.
Try:

VBA Code:
Sub compare_brands()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim n As Double, i As Long, k As Long, lr As Long
  Dim a As Variant, b As Variant
  
  Set sh1 = Sheets("STOCK")
  Set sh2 = Sheets("ACTUAL")
  
  a = sh1.Range("B2", sh1.Range("C" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To 3)
  
  lr = sh2.Range("D" & Rows.Count).End(3).Row
  For i = 1 To UBound(a, 1)
    n = WorksheetFunction.SumIfs(sh2.Range("E2:E" & lr), sh2.Range("A2:A" & lr), _
          Date, sh2.Range("D2:D" & lr), a(i, 1)) - a(i, 2)
    If n <> 0 Then
      k = k + 1
      b(k, 1) = Date
      b(k, 2) = a(i, 1)
      b(k, 3) = n
    End If
  Next
  
  sh2.Range("G" & Rows.Count).End(3)(2).Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub

🤗
 
Upvote 0
Solution
Hi Dante ,again !
I tested , but shows error results ,for instance 1200R20 G580 JAP = -200 based on your code .
the right should merge duplicates first =150+20=170 after that should subtract like this 170-200=-30.
 
Upvote 0
The macro is correct, but you should try with today's dates:

1725028543582.png


If the records on the "ACTUAL" sheet are dated August 28, which is how you tested, then none will be considered.
Try records dated August 30 or the day you take the test ;)
 
Upvote 0
OK
see the last highlighted by red what I got , but the right should be -50, not -150 as show.
TGR.xlsm
BCDEFGHIJ
1DATEINVOICE NOCLIENT NOBRANDQTYDATEBRANDQTY
228/08/2024BSJ1000BBBFT-00BS 1200R20 G580 JAP150.0028/08/2024BS 1200R20 G580 JAP-30.00
328/08/2024BSJ1001BBBFT-00BS 1200R20 G580 JAP20.0028/08/2024BS 1200R20 G580 THI10.00
428/08/2024BSJ1001BBBFT-01BS 1200R20 G580 THI160.0028/08/2024BS 205/70R15C R623 THI-120.00
528/08/2024BSJ1001BBBFT-01DT 1200R20 DS 50 THI80.0028/08/2024BS 750R16 8PR R230 JAP-200.00
628/08/2024BSJ1002BBBFT-02DT 1200R20 DS 50 THI20.0028/08/2024BS 750R16 10PR VSJ JAP-100.00
730/08/2024BSJ1003BBBFT-03BS 1200R24 G580 JAP50.0028/08/2024BSJ 1400R20 VSJ JAP-120.00
830/08/2024BSJ1004BBBFT-04BS 1200R24 G580 JAP50.0028/08/2024BSJ 1400R20 R180 JAP-700.00
928/08/2024BS 315/R22.5 R184 THI-500.00
1028/08/2024BS 1200R24 G580 JAP-150.00
1130/08/2024BS 1200R20 G580 JAP-200.00
1230/08/2024BS 1200R20 G580 THI-150.00
1330/08/2024BS 205/70R15C R623 THI-120.00
1430/08/2024DT 1200R20 DS 50 THI-100.00
1530/08/2024BS 750R16 8PR R230 JAP-200.00
1630/08/2024BS 750R16 10PR VSJ JAP-100.00
1730/08/2024BSJ 1400R20 VSJ JAP-120.00
1830/08/2024BSJ 1400R20 R180 JAP-700.00
1930/08/2024BS 315/R22.5 R184 THI-500.00
2030/08/2024BS 1200R24 G580 JAP-150.00
ACTUAL
 
Upvote 0
1725036727301.png


That's correct, for me the result is -50

In the Stock sheet you must have 150:

varios 29ago2024.xlsm
ABC
1ITEMBRANDQTY
21BS 1200R20 G580 JAP200
32BS 1200R20 G580 THI150
43BS 205/70R15C R623 THI120
54DT 1200R20 DS 50 THI100
65BS 750R16 8PR R230 JAP200
76BS 750R16 10PR VSJ JAP100
87BSJ 1400R20 VSJ JAP120
98BSJ 1400R20 R180 JAP700
109BS 315/R22.5 R184 THI500
1110BS 1200R24 G580 JAP150
STOCK
 
Upvote 0
my bad !:eek:
sorry I waste your time.🙏🙏
I was rushing by re-copy my data in different range (not matched structure data as in OP)
every thing is ok and work as I want.(y)
many thanks Dante.;)
 
Upvote 1

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,660
Latest member
Zatman

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