Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

create report for different prices

0

Hi  guys

I  search  for  way  to  correct  my  report for  items contains  different price .

so  my  aim will creat report from column J:P  and  getting  the item  based on matching in column B for  last  QTY(D)   and  last  two  PRICES(E) .

so the  code  should  just  search for  duplicated item contains  different prices  for  last  two duplicates items (if  the duplicates item  contains  same price  or  there  is  no  duplicated item contains more than  one  price   then  should  ignore it) 

currently  doesn't  gives  correct values .so  I  put  the  right  result  how could  be 

thanks

Answer
Discuss

Discussion

Abdo

Are you looking for when the item changes price for the SAME quantity (e.g. 110 for BTR-111) or for any quantity (so 110 or 111 or 112 for same)? You created the dictionary for the former (so Cell D15 =111 and D17=112 would NOIT be seen as a duplicate). Please clarify in your question wording.
John_Ru (rep: 6142) Mar 29, '23 at 9:38 am
HI John,
Are you looking for when the item changes price for the SAME quantity (e.g. 110 for BTR-111) or for any quantity (so 110 or 111 or 112 for same)? 
should  be  112   for  the  last  QTY  as  I opst  the  right result 
Abdo M (rep: 16) Mar 29, '23 at 9:50 am
Thanks. I asked you to reply in your original question so that others could see your aim without digging down in the Discussion. Pretty sure the Rules refer to that. Anyway  please see my Answer / file. 
John_Ru (rep: 6142) Mar 29, '23 at 12:06 pm
Add to Discussion

Answers

0
Selected Answer

Abdo,

In the Discussion point above, you clarified that you're looking for any change in price.

The problem is that you create your dictionary item to INCLUDE the quantity from a(i, 4)) so different quantities attach to new dictionary items. Better (in my opinion) to test if the price has changed from that created (or modified) in the dictionary item. That's done in the attached file and code below (changes in bold):

Sub test()
    Dim a, e, w, i As Long, n As Long, txt As String
    a = Cells(1).CurrentRegion.Value
    With CreateObject("Scripting.Dictionary")
        For i = 2 To UBound(a, 1)
            ' exclude the quantity a(i, 4) from dictionary item
            'txt = Join(Array(a(i, 2), a(i, 3), a(i, 4)), Chr(2))
            txt = Join(Array(a(i, 2), a(i, 3), Chr(2), Chr(2)))
            If Not .exists(txt) Then
                '2=Item, 3=ID, 4=Brand,5=Qty
                ReDim w(1 To 7)
                w(2) = a(i, 2): w(3) = a(i, 3):  w(4) = a(i, 4): w(5) = a(i, 5)
            Else
                w = .Item(txt)
                ' only update if the price is different
                If w(5) <> a(i, 5) Then
                    ' write current quantity to element 4
                    w(4) = a(i, 4)
                    ' write previous price to element 6
                    w(6) = w(5)
                    ' add last price to element 5
                    w(5) = a(i, 5)
                    w(7) = (w(5) - w(6)) * w(4)
                End If
            End If
            .Item(txt) = w
        Next i
        For Each e In .keys
            If (IsEmpty(.Item(e)(7))) Or (.Item(e)(7) = 0) Then
                .Remove e
            Else
                n = n + 1: w = .Item(e): w(1) = n: .Item(e) = w
            End If
        Next
        If n > 0 Then a = Application.Index(.items, 0, 0)
    End With
    With [j2]
        .CurrentRegion.Offset(1).ClearContents
        .CurrentRegion.Offset(1).Borders.LineStyle = xlNone
        If n > 0 Then
            .Resize(n, 7) = a
            .CurrentRegion.Borders.Weight = 2
        End If
    End With
End Sub

Note that my sequence is different from yours (columns M and N are now titled "Last Price" and "Previous" respectively.). Column P is positive if the Last price is higher than the previous.

Also it reports the item BTR-122 + FD/BTOOL22 where the price changed- that was excluded from your "Right Result" table.

Hope this fixes things for you (or you can swap the columns M and N  to suit you).

Discuss

Discussion

 great !
this  is  exactly  what  I  want  it .
thanks  very  much , John 
Abdo M (rep: 16) Mar 29, '23 at 4:32 pm
Glad that worked for you, Abdo. Thanks for selecting my Answer. 
John_Ru (rep: 6142) Mar 29, '23 at 4:54 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login