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.
Alternatively, you could:
Create a pivot table, selecting columns a and b as your source
Set serial number as a row field (drag it to the left of the table)
Set price as a data field (drag it to the middle)
Choose to display min of price rather than sum (the default)
Working out the formula was much more entertaining, though!
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.
We're then creating another column vector whose entries will be coerced to 1 if the corresponding entry in $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 part inside the MIN function starts out as the original vector of prices, but if the serial number from each row of column a is not the same as the one in the current row, we add on the largest price from anywhere in column B to the current row in column B.
This prevents any parts with different serial numbers from being counted as having the minimum price while leaving all prices for the current part unchanged.
This means we've constructed a column vector where all the entries are either zero or equal to the lowest price for the component in the row of the cell in which the formula is entered.
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.
Related
I've faced huge problem with my macro. I have data that contains colums with quantities and values of stock like this:
What I'm trying to achieve is:
to go through every row until the very last, locate quantity (colums with Q letter) and values (colums with V letters) below 0, then adding these quantities below zero to the maximum quantity within the row and adding these values below 0.
to find values within age category for every row that have no corresponding quantity (see cell B4 as example) and add these values to the maximum value within the row.
Why VBA for something you can achieve with a formula?
Let me show you how I calculate the maximum of a list of cells, referring to a column, whose name starts with a "V":
=MAXIFS(A2:F2,A1:F1,"V*")
Screenshot:
Explanation:
Take the maximum of the values on the second row (A2:F2)
The criteria you need to take into account refer to the first row (A1:F1)
The criteria is that it should start with a "V".
I'm new to excel and I came across this problem.
I have a table with 2 columns one with the name of the item(the same name can appear multiple times) and next to it the quantity of said item.
What I want to get is the item which has the most overll quantity (keeping in mind that the object can appear multiple times in the table and have different quantities in each of the apppearences).
You didn't specify how you want to highlight the value.
Assuming your items are in column A and your values in column B.
Add a helper formula in column C to sum all the items.
=SUMIF($A$1:$A$100,A1,$B$1:$B$100)
you can use conditional formatting to highlight the highest value by adding a rule that uses a formula and entering;
=B1=MAX($C$1:$C$100)
It highlights every entry of the item with the greatest value.
To return the name of the item with the highest value you can use;
=INDEX(A1:A100,MATCH(MAX(C1:C100),C1:C100,0),1)
There are 2 parts to this formula
=INDEX(A1:A100,MATCH(MAX(C1:C100),C1:C100,0),1)
The match part finds the row number of the highest value and the Index part returns the value in column A for that row.
This solution may not be ideal if you have a huge amount of data so you could also try a pivot table and sort it highest to lowest.
I have a table in excel that can be seen below.
I want to choose the highest number in the "RPD" row and multiply it with the values that are in the same column.
So in this case the formula would take July column and multiply all the values there.
The problem is that I want to do this automatically, so when the order of values in "RPD" row changes the calculation changes automatically, still chooses the highest value in "RPD" row and multiplies it with all the values in the same column.
I also want to do this for the second highest value and so on...
So I would end up with a new row where the first column has the values for july multiplied and so on.
Thanks in advance.
You may need to use the =LARGE(matrix, k) function; where matrix will be the RPD numbers and K-esim largest number. And fix the range of your matrix.
Change MAIOIR to LARGE (i'm using it in pt_BR).
For the 1st one, the formula will be:
=LARGE(C5:N5; B9)*C4*C6
Then will have to lock the parameters by using $
=LARGE($C$5:$N$5; $B9)*C$4*C$6
Output:
I have a set of data with multiple columns. I'm trying to transfer the data from columns to rows, but only for the unique members.
For instance, I would like the "Hours Worked" to populate under the correct month in the rows to the right based on unique "SSN"
I've tried =IF($P2=$C2,HLOOKUP(Q$1,$H:$H,MATCH(Q$1,$H:$H,0),FALSE),"")
Index/Match, etc but can't figure it out.
https://docs.google.com/spreadsheets/d/13jJJ0GXb6hFW7YEyyqrCn8cfotUeFurtm0U1EFj_kPY/edit?usp=sharing
Try the following formula in cell F2 and copy it over:
=SUMPRODUCT(--($E2=$A$2:$A$13),--(F$1=$C$2:$C$13),$B$2:$B$13)
The result of ($E2=$A$2:$A$13) is an array containing trues and falses, depending on whether the ID in column E is equal to the ID in column A. (F$1=$C$2:$C$13)does the same thing for the date. By adding -- in front of these arrays, we convert the trues and falses to ones and zeros, respectively. The third array contains the hours worked in each month. These three arrays are then multiplied (and summed up in case there were several entries for one ID in one month).
I have a design software which extracts data in to an Excel sheet format
The output is divided into 2 columns, each of these columns has more than 1000 rows.
To make use of this data I need to summarize it to a maximum of the 5 highest values from both of the 2 columns. Therefore, this doesn't mean that it's the maximum of one column and its corresponding value, but it may mean that the 2nd largest value of column 1 & the 4th largest value of column 2.
For example ( if we quoted some of the output data):
The values i should pick here are:
If there is any possible way to achieve that, it will be great
Thanks ..
example file: http://goo.gl/UIEFEv
example file 2: http://goo.gl/VSvuVf
Here's a formula solution. I used 20 rows and extracted the rows which contain the top 5 for each column - you can extend to as many rows as required.
With data in A1:B20 use this formula in D1 confirmed with CTRL+SHIFT+ENTER and copied across to E1 and down both columns:
=IFERROR(INDEX(A$1:A$20,SMALL(IF(($A$1:$A$20>=LARGE($A$1:$A$20,5))+($B$1:$B$20>=LARGE($B$1:$B$20,5)),ROW(A$1:A$20)-ROW(A$1)+1),ROWS(D$1:D1))),"")
Note: there are only eight rows extracted because some of the rows contain values in the top 5 for both columns. I added the highlighting in colums A and B to more clearly illustrate
see screenshot below
Edit:
From the comments below it seems that you want a combination of rows which contain the highest value for that column....and rows which contain the highest total for both columns.
In the original formula there are two conditions joined with "+", i.e.
($A$1:$A$20>=LARGE($A$1:$A$20,5))+($B$1:$B$20>=LARGE($B$1:$B$20,5)
The "+" gives you an "OR" type functionality, e.g. in this case rows are included if individual values are in the top 5 in that particular column. You can add other conditions, so if you want to also add any rows which are in the top 5 considering the total of both columns then you can add another "clause", i.e.
($A$1:$A$20>=LARGE($A$1:$A$20,5))+($B$1:$B$20>=LARGE($B$1:$B$20,5)+($A$1:$A$20+$B$1:$B$20>=LARGE($A$1:$A$20+$B$1:$B$20,5))
....and including that in the complete formula you get this version:
=IFERROR(INDEX(A$1:A$20,SMALL(IF(($A$1:$A$20>=LARGE($A$1:$A$20,5))+($B$1:$B$20>=LARGE($B$1:$B$20,5))+($A$1:$A$20+$B$1:$B$20>=LARGE($A$1:$A$20+$B$1:$B$20,5)),ROW(A$1:A$20)-ROW(A$1)+1),ROWS(D$1:D1))),"")
You could refine that further by using combinations of + and * (for AND), e.g. for the new condition you might only want to include rows with a total in the top 5 if one of the single values is in the top 10 for that column...
Explanation:
The above part shows how you can use + for the OR conditions. In the formula if those conditions are TRUE then the IF function returns the "relative row number" of the range (using ROW(A$1:A$20)-ROW(A$1)+1).
SMALL function then extracts the kth smallest value, k being defined by ROWS(D$1:D1) which starts at 1 in D1 (or E1) and increments by 1 each row.
INDEX function then takes the actual value from that row.
When you run out of qualifying rows SMALL function will return a #NUM! error which IFERROR here converts to a blank
The question is a little unclear but if what you mean is to get the 5 highest values of Column A and their corresponding values in Column B then the five highest values in Column B and the corresponding values in Column A then the (non automated) solution is pretty simple.
Click on a cell with a header title in it.
Click on 'Data' in the top menu.
Click on 'Filter' in the 'Sort & Filter' section.
Click on the button on Column A - select 'Sort Largest to Smallest'
Grab the top five values from both columns then click on the button in column B and repeat.