Reputation: 83
Column A represents serial numbers of different parts, but these parts come from different suppliers so they have different prices. Column B holds the price for each part.
I need a macro that will essentially go down Column A, compare the column B price for duplicates, and return the min price for the part in column C.
Upvotes: 0
Views: 2490
Reputation: 9569
Alternatively, you could:
Working out the formula was much more entertaining, though!
Upvotes: 1
Reputation: 9569
You can do this without even needing array formulas:
=SUMPRODUCT(($B$1:$B$5)*($B$1:$B$5=MIN($B$1:$B$5+(A1<>$A$1:$A$5)*MAX($B1:$B5)))) /SUMPRODUCT(1*($B$1:B$5=MIN($B$1:$B$5+(A1<>$A$1:$A$5)*MAX($B1:$B5))))
The important insight here is that you can do array operations inside a SUMPRODUCT, and inside functions inside a SUMPRODUCT. The first SUMPRODUCT gives the sum of all the prices that are equal to the minimum price for each component, and the second one gives the number of listings that have that price. Divide one by the other and you have the minimum price for each component!
First SUMPRODUCT:
($B$1:$B$5)
is a column vector of your prices.$B$1:$B$5
is equal to MIN($B$1:$B$5+(A1<>$A$1:$A$5)*MAX($B$1:$B$5))
and zero otherwise.The second SUMPRODUCT is similar to the first, except this time we just add up the number of times the condition is met without multiplying by the price.
Upvotes: 0