Excel - Formula or Macro to fill a cell based on another cell that links to yet another cell - excel

In Excel, I am trying to make a cell based of the values contained in two other cells.
I need Cells X and Y to have data based on Cells L and #, like so....
X Y L 1 2 3 4 5 6
A 6 1 1 6;1 6;1 7;1 7;2 7;2 8;1
B 7 2 4 6;1 6;1 7;1 7;2 7;2 8;1
So row A, has columns X and Y filled based of the values in the number columns. The specific number needed is what is filled in in column L.
I am not sure the best way to phrase this question. If my example doesn't make sense, I can try to clarify or provide more examples.
I have no idea if this can be done with fancy formulas or with a VBA macro or two. I am an excel noob.

If I've understood your question correctly you can do this with a combination of Left/Right, Index and search.
In my example images, the user inputs their value in column D, and then columns B and C use the formulea
=LEFT(INDEX($F2:$K2,1,$D2),(SEARCH(";",INDEX($F2:$K2,1,$D2))-1))
=RIGHT(INDEX($F2:$K2,1,$D2),(SEARCH(";",INDEX($F2:$K2,1,$D2))-1))
respectively
Here, the Index function returns the correct column to look at (i.e. the value chosen by the user, the Search function finds the position of the semi-colon, and the left/right functions return the values either side of the semi-colon.

Related

All combinations of 4 out of 7 columns with totals using excel

I have 7 columns to choose from and I need to pick 4 of those columns and generate a total for each row. I also need every combination of 4, which means I'll have 35 new columns with the totals for each of those combinations showing in each row. I need the code for this and if it can be done only using Excel. Here is an image of the columns and the grayed ones are the 7 columns I'm talking about. My knowledge of Excel is very limited. There are over 1,500 rows if that matters.
multi step approach that is going to use some helper rows. there may be a more elegant formula that will do this, and much slicker options in VBA, but this is a formula only approach.
Step 1 - Generate List of Column Combination
To generate the list 4 helper rows will need to be insert at the top of your data. either above or below you header row. These 4 rows will represent the column number you are going to pick. To keep the math simpler for me I just assumed the 1 for the first column and 7 for the last column. those numbers will get converted to later to account for column in between in your spreadsheet. For the sake of this example The first combination sum will occur in column AO and the first helper row will be row 1. The first combination will be hard coded and it will seed the pattern for the remainder of column combinations. Enter the following values in the corresponding cells:
AO1 = 1
AO2 = 2
AO3 = 3
AO4 = 4
In the adjacent column a formula will be placed and copied to the right. It will automatically augment the bottom value by 1 until it hits its maximum value at which point the value in the row above will increase by 1 and the the value of the current will be 1 more than the cell above. This will produce a pattern that covers all 35 combinations by the time column BW is reached. Place the formulas below in the appropriate cell and copy to the right:
AP1
=IF(AO2=5,AO1+1,AO1)
AP2
=IF(AO2=5,AP1+1,IF(AO3=6,AO2+1,AO2))
AP3
=IF(AO3=6,AP2+1,IF(AO4=7,AO3+1,AO3))
AP4
=IF(AO4=7,AP3+1,AO4+1)
Step2 - Sum The Appropriate Columns
I was hoping to use a some sort of array type operation to read through the column reference numbers above, but I could not get my head around it. Since it was just 4 entries to worry about I simply added each reference manually in a SUM function. Now the important thing to note is that we will be using the INDEX function over the 13 columns that cover the range of your columns so to convert the index number we figured out above, to something that will work to grab every second row, the number that was calculated will be multiplied by 2 and then 1 will be subtracted. That means 1,2,3,4 for the first column combination becomes 1,3,5,7. You can see this in the following formula. Place the following formula in the appropriate cell and copy down and to the right as needed.
AO5
=INDEX($AB5:$AN5,AO$1*2-1)+INDEX($AB5:$AN5,AO$2*2-1)+INDEX($AB5:$AN5,AO$3*2-1)+INDEX($AB5:$AN5,AO$4*2-1)
pay careful attention to the $ which will lock row or column reference and prevent them from changing as the formula is copied.
Now you may need to adjust the cell references to match your sheet.

Excel formatting/Highlight row based on the last date and equal cell value

My question relates with the following.
I have a matrix in Excel of values, I want to highlight the entire row if two conditions are met. Those conditions are:
a. For equal values on column A compare its date;
b. If the value is equal, highlight the row with the most recent date.
Graphically its the following:
A B
1 Y 04-08-2006
2 X 02-07-2008
3 X 12-05-2014
4 Z 09-08-2014
5 X 25-06-2016
6 Z 01-04-2018
7 Y 24-07-2018
8 X 25-09-2018
I want to highlight the 6, 7 and 8 row with different colors.
Can I do that with conditional formatting or its not possible and the only way is to write code in VBA?
Thank you.
You don't need VBA for this. Make a small Pivot table where your column A is the rows and columns B are the values set to max. Then add a columns that is a vlookup to add the max date for each group (formula show in image) and finally just add conditional formatting, one rule per group again as shown in the image (note in this case the formatting is applied to the range A1:C9
You can always hide column D and the pivot table or put them on another sheet if need be.

How do I convert rows to columns, and repeat the adjacent cells?

I'm sorry if the title is confusing, but a visual should hopefully help:
Here's what my sheet looks like:
A B C D
1 x y z t
2 q w e r
3 y u i o
I need to generate a separate sheet wherein:
A B C
1 x y t
2 x z t
3 q w r
4 q e r
...
Basically, I need the middle two columns of the original sheet to be transposed and the adjacent columns to it pulled into my new sheet as well (as duplicate rows).
I have the transpose working correctly, and when I pull the adjacent columns that works too. The issue is, I can't autofill the sheet. When I try to drag & autofill, instead of autofilling using row 2 from the original sheet, the new sheet will autofill using row 3 (which is the same row # in the new sheet).
Please let me know if this isn't making sense, I'll try to explain better! I'm not very well versed in the Google Spreadsheets scripts - I've tried before but they seem rather cumbersome. But I'm happy to try that as well.
I'm not sure where a transpose operation would come into play but a little conditional maths and the INDEX function¹ should suffice.
=INDEX($A$1:$D$3,(ROW(1:1)-1)/2+1,IF(COLUMN(A:A)=3,4,IF(AND(ISEVEN(ROW(1:1)),COLUMN(A:A)=2),3,COLUMN(A:A))))
The documentation I've linked to is Excel but the syntax is identical for these purposes.
Addendum for more columns
By adjusting the condition for the offset after the third column, more columns can be readily accounted for.
=INDEX($A$1:$J$3,(ROW(1:1)-1)/2+1,IF(COLUMN(A:A)>=3,COLUMN(A:A)+1,IF(AND(ISEVEN(ROW(1:1)),COLUMN(A:A)=2),3,COLUMN(A:A))))
The above collects 10 columns from the source matrix but should be auto-adjusting for pretty much any number of columns.
¹ The INDEX function accepts parameters for both row number and column number. Although typically only one of these is used, there is no restriction against supplying both against a 2D range of cells.
Put the row_number calculation,      =(ROW(1:1)-1)/2+1 into a cell and fill down. You will receive 1, 1, 2, 2, 3, 3, etc. This supplies the repeating row number from the A1:D3 range.
Put the column_number calculation,      =IF(COLUMN(A:A)=3,4,IF(AND(ISEVEN(ROW(1:1)),COLUMN(A:A)=2),3,COLUMN(A:A))) into a cell and fill right and down. You will receive 1, 2, 4 for the first row and 1, 3, 4 for the second. This pattern repeats itself for subsequent rows and supplies the offset column numbers from the A1:D3 range.
You can do this with 3 single arrayformulas that are dynamic to work with any number of rows of data:
Here is an image to demonstrate:
The three formulas are:
CELL A1:
=TRANSPOSE(SPLIT(JOIN(";",ARRAYFORMULA(REPT(Sheet1!A1:A&";",2))),";"))
CELL B1:
=TRANSPOSE(SPLIT(JOIN(";",ARRAYFORMULA(Sheet1!B1:B&";"&Sheet1!C1:C)),";"))
CELL C1:
=TRANSPOSE(SPLIT(JOIN(";",ARRAYFORMULA(REPT(Sheet1!D1:D&";",2))),";"))

How to return a value to the left of a table array with VLOOKUP?

I need help searching Column E for value=1 and return the value of column A for the same row. VLOOKUP isn't working because there are many columns being searched and there are several 1's in the lookup, and I couldn't seem to search just 1 column using VLOOKUP. Here's how my spreadsheet looks...
A B C D E
1 Name Weight WeightRank Height HeightRank
2 Mike 170 3 6.3 2
3 Richard 200 1 6.0 3
4 Charles 185 2 7.0 1
So I want to search column E for value=1 and return the corresponding value in column A, which in this example would search "HeightRank" for "value=1" and return "Charles"
I tried using =VLOOKUP(1,E:E,1) but that returns an error.
I tried using =VLOOKUP(1,A1:E3,1) but that returns an error.
INDEX(A:A,MATCH(1,E:E,0))
VLOOKUP doesn't work here -- it always searches in the first column of your table and returns a value of a column a given number of columns to the right.
The INDEX/MATCH combination is more flexible, letting you just choose the two columns you want. It's also easier to read (you don't have to count columns to see what it does) and it doesn't break if you insert or delete columns in between the ones you're using, which VLOOKUP does. If you use the trace-formula features, VLOOKUP also falsely implies that all the columns in between are precedents of your resulting formula. (Can you tell that I don't much like VLOOKUP? I just always use INDEX/MATCH and my life is easier for it.)
The OFFSET solution works but it's volatile, so you'll really bog down your worksheet if many cells depend on the result of your formula.
MATCH(x,E2:E4,0) returns the relative position of x in the range E2:E4. For example, MATCH(1,E2:E4,0) returns 3, because 1 is the value of the third cell in the range E2:E4.
OFFSET(A2,r,c) returns the cell r rows and c columns away from A2.
Thus you can say =OFFSET(A2,MATCH(1,E2:E4,0)-1,0) to return the value from column A corresponding to the cell in column E that contains 1.

Index/Match multiple columns in Excel

I have 2 sheets. Sheet 1 is set up similarly to:
Keyword Domain Rank
A Z 1
B Z 2
C Z 3
D Y 10
E Y 15
B Y 20
And sheet 2 is set up like:
Keyword (Domain Z) (Domain Y)
A 1 -
B 2 20
C 3 -
D - 10
I'm trying to have a formula that will compare the keywords in Sheet 2 with those in Sheet 1 and then return the rank that corresponds to the correct domain (that's specified in Sheet 2 for that column). I can't get any formula I use to evaluate. I've used 2 formulas so far:
=INDEX(Raw!$H$11:$H$322, MATCH(A3,IF(Raw!$D$11:$D$322=All!$B$2,Raw!$B$11:$B$322),0))
The above formula works, to a point. The problem is that it simple pulls the domain for the first instance of the keyword found, which doesn't always match the domain in the column of sheet 2. The second formula I've tried:
=INDEX(Raw!$H$11:$H$322, MATCH(B3,MATCH($C$2,Raw!$D$11:$D$322,0)))
To make the values appear in the Sheet 2 table, use the following formula:
=SUMPRODUCT(--($A$2:$A$7=E2),--($B$2:$B$7=$F$1),$C$2:$C$7)
This returns 0 for non-matches - you can either format the cells to display 0 how you want, or you can use the longer/uglier:
=IF(SUMPRODUCT(--($A$2:$A$7=E2),--($B$2:$B$7=$G$1),$C$2:$C$7)<>0,SUMPRODUCT(--($A$2:$A$7=E2),--($B$2:$B$7=$G$1),$C$2:$C$7),"-")
To calculate the rank on the first sheet based on the data from the second sheet:
=VLOOKUP(A2,$F$2:$H$5,MATCH(B2,$G$1:$H$1,0)+1,FALSE)
For sample purposes, this just put your sheet2 data in F1:H5.
This looks for the corresponding keyword and then uses match to pick the right column. I named the columns Z and Y, but if you need Domain included that can be done as well. Note that this causes an error since there is no E defined in your second table - is that the case? If so, it can be adjusted to account for no matches as follows (assuming Excel 2007):
=IFERROR(VLOOKUP(A6,$F$2:$H$5,MATCH(B6,$G$1:$H$1,0)+1,FALSE),"Rank Not Found")
You could also use PivotTable with Keywords in rows and Domain names in columns. It seems like that would do the job and be a more robust solution.

Resources