VBA Duplicate Value in Column And Perform Calculation

bluesky33

New member
Joined
Jan 22, 2021
Messages
4
Reaction score
0
Points
0
Excel Version(s)
2016
[FONT=&quot]I have two columns in my spreadsheet. If the value in column A repeats, then I want to get the corresponding value in column B for both these values and compare them. If there is a difference greater than say $10, write Yes in column C.[/FONT]

[FONT=&quot]Example, code 123456 appears twice in column A, and there is a difference of greater than $10 so I have to write "Yes" in column C. On the other hand, 999999 appears twice but there is no difference so I have to write "No".[/FONT]

[FONT=&quot]Item_code sale_price Difference[/FONT]
[FONT=&quot]123456 $ 123.56 Yes[/FONT]
[FONT=&quot]999999 $ 1,542.00 No[/FONT]
[FONT=&quot]598745 $ 8,455.00 NA[/FONT]
[FONT=&quot]123456 $ 130.99 Yes[/FONT]
[FONT=&quot]546892 $ 562.00 NA[/FONT]
[FONT=&quot]999999 $ 1,542.00 No[/FONT]

[FONT=&quot]Giving below the code, not sure why it is not identifying the next occurrence of the same value and getting the relative value from column B. It just keeps getting me the first occurrence. Any help is very much appreciated. [/FONT]

[FONT=&quot]Sub compare_dollars_Click()[/FONT]

[FONT=&quot]item_row = Worksheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row[/FONT]

[FONT=&quot]For item_counter = 2 To item_row[/FONT]

[FONT=&quot]get_input = Worksheets("Sheet1").Range("C" & item_counter).Value[/FONT]

[FONT=&quot]If WorksheetFunction.CountIf(Range("C:C"), get_input) > 1 Then[/FONT]

[FONT=&quot]var1 = Range("B" & item_counter)[/FONT]
[FONT=&quot]MsgBox ("Sale Price" & " " & var1 & " " & item_counter)[/FONT]
[FONT=&quot]End If[/FONT]

[FONT=&quot]check_threshold (get_input)[/FONT]

[FONT=&quot]Next item_counter[/FONT]

[FONT=&quot]End Sub[/FONT]


[FONT=&quot]Sub check_threshold(get_input)[/FONT]

[FONT=&quot]Set repeat_cell_address = Worksheets("Sheet1").Range("C:C").Find(get_input, lookat:=xlPart)[/FONT]

[FONT=&quot]newrow = repeat_cell_address.Row[/FONT]

[FONT=&quot]MsgBox (newrow)[/FONT]
[FONT=&quot]var2 = Range("B" & newrow)[/FONT]
[FONT=&quot]MsgBox ("Second occurrence of item code" & " " & var2 & " " & newrow)[/FONT]

[FONT=&quot]End Sub[/FONT]
 
Try with your revisited code, maybe it's closer to your needs.
Code:
Option Explicit                                   '<=added
Dim item_counter                                  '<=added

Sub compare_dollars_Click()

    Dim item_row, get_input, var1                 '<=added

    item_row = Worksheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row
    For item_counter = 2 To item_row
        get_input = Worksheets("Sheet1").Range("C" & item_counter).Value
        If WorksheetFunction.CountIf(Range("C:C"), get_input) > 1 Then
            var1 = Range("B" & item_counter)
            MsgBox ("Sale Price" & " " & var1 & " at row " & item_counter)
        End If
        check_threshold (get_input)
    Next item_counter
    
End Sub
Code:
Sub check_threshold(get_input)

    Dim repeat_cell_address, newrow, var2         '<=added

    Set repeat_cell_address = Worksheets("Sheet1").Range("C:C").Find(get_input, _
         after:=Worksheets("Sheet1").Range("C" & item_counter), lookat:=xlPart) '<=changed
    newrow = repeat_cell_address.Row
    MsgBox (newrow)
    var2 = Range("B" & newrow)
    MsgBox ("Second occurrence of item code" & " " & var2 & " at row " & newrow)
    
End Sub
 
Last edited:
Hi and welcome
Please,do not crosspost your question on multiple forums without including links here to the other threads on other forums.

Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).
If you have fewer than 10 posts here, you will not be able to post a link, but you must still tell us where else you have asked the question
As you are new here I will add it for you this once

https://www.mrexcel.com/board/threads/vba-duplicate-value-in-column-and-perform-calculation.1159276/
 
Thank you. I will try it and provide an update.
 
Hi, I tried the code and it is generating random item codes like 66, 2, 1 etc which are not in the item code list. It did however work for item code 999 and bring up the correct sale_price. Other numbers were arbitrary. Thanks.
 
Probably your real data has nothing to do with what you listed in post #1; I don't have a crystal ball.
Maybe changing the line of code:
Code:
Set repeat_cell_address = Worksheets("Sheet1").Range("C:C").Find(get_input, _
         after:=Worksheets("Sheet1").Range("C" & item_counter), lookat:=xlPart)
to this would make it work differently:
Code:
Set repeat_cell_address = Worksheets("Sheet1").Range("C:C").Find(get_input, _
         after:=Worksheets("Sheet1").Range("C" & item_counter), lookat:=xlWhole)
but I'm almost sure I didn't understand your request.
 
Last edited:
@ bluesky
please wrap your code with code tags ( select the code and press the #button)
 
Back
Top