I'm doing a hlookup against a value that spans multiple columns. My data is similar to this:
A B C D
---------------------------
1| Col1 Col2
2| x y z w
3|
4|
In rows 3 and 4 (A3, B3, C3, D3, etc.), I'd like to put formulas that will do an hlookup somewhere else in the workbook. The trick is, I'd like it to look up "Col1" for columns A and B and "Col2" for columns C and D. "Col1" is in A1, but is really A1 and B1 merged. When I reference A1, "Col1" appears, but when I reference B1, the return value is blank.
Any ideas?
Here is another solution that can also work when the merged cells are of different widths, let me illustrate with an example:
Open a fresh Excel, merge B1, C1, D1
Type Col1 in the merged cell
In B2, type formula =B1, and in C2 =C1, in D2 =D1
You should see B2 to be Col1 while C2, D2 are 0
In B3, type the formula =A3, copy it
Right-click the merged cell B1:D1, select "paste special -> formulas"
You should see the merged cell being 0
Type Col1 in the merged cell
You should now see all B2, C2, D2 to be Col1, i.e. now you can reference the merged cell as you expect it to be.
If you can multiple merged cells, each of different widths, just paste the formula to all of them in one go.
The reason behind this works is because of a perculier design choice by Microsoft. It seems that when you paste formulas in merged cells, each underlying cell receives the formula (in contrast, if you enter a value, only the top-left cell gets it) So you can use it at your advantage and paste a formula that reference the cell next to it, and then overwrite the top-left cell with the value you want, then every cell underlying the merged cell will have that value.
To get access to the "Col1" and "Col2" labels, you can use the following:
=INDEX($1:$1,1,COLUMN()-MOD(COLUMN()-1,2))
Note: This assumes that you are grouping together the same number of cells. If it were three cells, you would just change the last number in the formula to a 3, and so on.
Edit: Here's how it works:
INDEX($1:$1,1, x ) returns the value of the cell in row 1, column x. If your table is not actually located in the top left corner of the worksheet, you can change this to the actual range that includes all of your merged labels. In this case, it would be:
INDEX($A$1:$D$1,1, x )
COLUMN() returns the column number of the current cell (1 in column A, 2 in column B, etc.)
MOD(COLUMN()-1,x) returns an offset from the current column to the column that holds the proper label
I've built a simple function in vba that will solve this problem:
Function mergedText(rngMergedCell As Range)
If rngMergedCell.MergeCells = True Then
mergedText = rngMergedCell.MergeArea(1, 1)
Else
mergedText = rngMergedCell
End If
End Function
If the cell is a merged cell, the function will return the value in the first element of the merged cell - this is where the merged cell stores its value
A more generic variant of e.James's proposal is :
={INDEX($A$1:A1, 1, MAX(NOT(ISBLANK($A$1:A1))*COLUMN($A$1:A1)-COLUMN($A$1)+1))}
This relies on the fact that the merged cells are empty except for the first one (unless you are in a case like Martin's proposal).
Note: The curly braces are there to mark an array formula (do not enter them, just press alt+return to validate the formula in the cell).
I realize I am late to this thread but I found a really simple answer to this.
If, for example, your label is merged across 4 columns a1:d1, and if you reference b1, you will return "". For dynamically finding the right labels use this fx in your new table:
=if(OriginalTable!B1="",ThisTable!A1,OriginalTable!B1)
I am sure you will realize that this will capture ranges in e1:h1 etc as you drag across.
That's it. Hope it helps someone.
Cells B1 and D2 contain no values, only A1 and C1 have something inside them.
So you'll just have to make sure that your formulas in columns A and B both refer to A1 as the lookup value, and that your formulas in columns C and D both refer to C1 for the lookup value.
With the new dynamic reference, there are more options now. Here is a generic function I wrote that will search to the left of the cell and return the first value. It is NOT optimized, but it does the job for me.
=LET(
TargetCell, A1,
TargetRow, ROW(TargetCell),
TargetCol, COLUMN(TargetCell),
RowReference, INDIRECT(TargetRow & ":" & TargetRow),
RowValues, TRANSPOSE(FILTER(RowReference,ISBLANK(RowReference)=FALSE)),
RowValueColumns, MATCH(RowValues, RowReference,0),
ReturnColumn, MAX(FILTER(RowValueColumns,RowValueColumns<=TargetCol)),
Return, INDIRECT(ADDRESS(TargetRow,ReturnColumn)),
Return
)
Related
I need to compare the B2 Cell value (6012469) with Column A i.e. cell A2 to A10 and return the b2 cell value (6012469) in column C where the value matches i.e. cell C5,C7, C9 AND C10 and NA in remaining cells where the value is not matched.
I have around 15000 rows of data is column A and need to automate the process.
I have attached the sample image for reference.
[1]: https://i.stack.imgur.com/DvzC3.png
Select any cell inside your data range in Column A. Go to your ribbon, click Home > Format as Table. It doesn't matter what colour scheme you choose.
Make sure that all your data in Col A plus Cols B & C are included in the table.
Then, in any cell of your table in Col C (e.g. in C2), paste this formula:
=IF(ISNUMBER(SEARCH($B$2,[#[Column A]])),$B$2,"")
Excel should automatically populate all of your Col C in the table with this formula. (If it doesn't, then you should see a little clickable prompt next to the cell asking if you want to overwrite all the other Col C cells with this formula.)
The table with the Col C formulas would look something like this:
Note: Of course, you could achieve something very similar without using a structured table, but not as conveniently as this because you would have to copy the formulas down to the other approx. 15000 rows. Even the Fill Down feature is not as convenient.
ADDITIONAL: a different formula in response to the asker's request that each cell in Column C match any of multiple values in Column B --
=IFERROR(INDEX($B$2:$B$4,MATCH(TRUE,ISNUMBER(SEARCH($B$2:$B$4,A2)),0)),"")
NOTE: if more than one Column B value matches a Column A cell, Column C will display Column B's topmost match
Image for reference:
If the cell on the right (B1) is blank, I want it to use the data in the left cell (A1).
I have tried:
=IF(ISBLANK(B1), A1, B1)
However, this only results in an error.
=IF(ISBLANK(B1), A1, "")
This results in a 0.
Without VBA: Circular reference problem.
If you want to check that if B column has data then no need to paste the column A data..And if column b is blank then you want the column A data to paste in column B , right?
If that's the scenario then you need to open iterative analysis..For this
Go to File>Options>Formula>Enable iterative analysis.
Try this:
=IF(OR(ISBLANK(B1), B1=""), A1, B1)
I have the following structured table in excel where 1000s of rows included.
I want to return the Last Cell Value of Column B for Each value in Column A.
For example:
For Cell A1 -> I want to return the Cell B5.
For Cell A6 -> I want to return the Cell B9.
I have tried with VLOOKUP, HLOOKUP, INDEX likewise so many formulas where I ended with more conflict situations. Can anyone please write down a Formula to give my requirement a life?
Array formula (Press Control + Shift + Enter while entering the formula) in cell C1 and copy it down.
=IF(A1="","",IFERROR(INDEX($A2:$B$20,MATCH(FALSE,ISBLANK($A2:$A$20),0)-1,2),LOOKUP(2,1/(B2:$B$20),B2:$B$20)))
if you don't mind using column C as a helper column you could use something like that:
If you won't use Array formula you can Use this solution:
like this image:
use column C as helper with this formula in first cell =OFFSET(A1;SUMPRODUCT(MAX(($A$1:$A1<>"")*(ROW($A$1:$A1))))-ROW(A1);0)
and use this formula in column D's first cell =IF(A1="";"";INDEX($B$1:$B$13;SUMPRODUCT(MAX((ROW($A$1:$A$13))*($C$1:$C$13=A1)))))
and copy fill-down to all cells.
Try this shorter, without helper column and non-array formula solution
In C1, formula copied down :
=IF(A1="","",INDEX(B1:B$9,MATCH(1,FREQUENCY(1,0+(A2:A$9<>"")),0)))
I have checked alot of INDEX and MATCH formula but can't nail this if someone can point me in the right direction to compare one cell in the C column against all cells in Column A and Column B and if it is in neither enter C2 in D2 so I have a list of numbers not in either column A or B.
So the idea is I check every C column cell against the other two columns and show the C cells that are in netiher.
Hopefully this pseudo code helps explain:
IF (cell C2 is not anywhere in column A OR not anywhere in column B) then return cell C2 in Cell D2
This one works for one column check:
=INDEX(C$2:C$23,MATCH(C2,A$2:A$23,0))
but I need two column check but the below does not work for me as it just says #N/A even for the row that was working for the single column check formula above. I I wondering if I can put an OR in the middle of the two matches or maybe you can suggest it a different way.
=INDEX(C$2:C$23,MATCH(C2,A$2:A$23,0),MATCH(C2,B$2:B23,0))
Thanks in advance.
Try using COUNTIF like this
=IF(COUNTIF(A$2:B$23,C2)=0,C2,"")
or if columns are not adjacent
=IF(COUNTIF(A$2:A$23,C2)+COUNTIF(B$2:B$23,C2)=0,C2,"")
Those will return C2 value if it's not in either column....otherwise a blank
....if you really want to use MATCH try this version....
=IF(COUNT(MATCH(C2,A$2:A$23,0),MATCH(C2,B$2:B$23,0))=0,C2,"")
And one more version using VLOOKUP:
=IF(AND(ISERROR(VLOOKUP($C$2,$A:$A,1,0)),ISERROR(VLOOKUP($C$2,$B:$B,1,0))),"",$C$2)
I have an Excel table with 4 columns and need to find the values that are the same in ALL 4 of the columns and put them aside. Trying to figure out how to accomplish this in Excel.
Example:
Column 1 Column 2 Column 3 Column 4
A1 B1 B1 A1
B1 C2 C1 B1
C2 D3 C2 C2
In this example, both B1 and C2 values comply and must be set aside.
Put this formula in cell D1 and drag and drop it till the end of your values:
=IF(AND(COUNTIF($B$1:$B$3,A1)>0,COUNTIF($C$1:$C$3,A1)>0,COUNTIF($D$1:$D$3,A1)>0),A1,"not found")
For the record, the french version:
=SI(ET(NB.SI($B$1:$B$3;A1)>0;NB.SI($C$1:$C$3;A1)>0;NB.SI($D$1:$D$3;A1)>0);A1;"not found")
To generate a list of values that appear in all four columns, try in cell F2:
=INDEX(A$1:A$3,MATCH(TRUE,COUNTIF(B$1:B$3,A$1:A$3)*COUNTIF(C$1:C$3,A$1:A$3)
*COUNTIF(D$1:D$3,A$1:A$3)*NOT(COUNTIF($F$1:F1,A$1:A$3))>0,0))
confirmed with CTRL+SHIFT+ENTER and fill down as far as needed (this assumes F1 is blank or contains unrelated data)
I couldn't get the above examples to work for myself, so I came up with another solution. Hopefully this will help someone else out there:
=IF(ISNUMBER(MATCH($A2, $B$2:$B$4, 0)), IF(ISNUMBER(MATCH($A2, $C$2:$C$4, 0)), IF(ISNUMBER(MATCH($A2, $D$2:$D$4, 0)), $A2, ""), ""), "")
This checks the value in "Column 1" ("A" column in excel) against the next 3 columns (B,C,D). If the value is present in all three it returns the A column value. If it is not present in all 3, it returns empty. If you would rather return something else, put that something in between the empty quotation marks.
For your data, enter in the lengths of the searched columns, B through D. For example, where it says, "$B$4", put in "$B$99" if the cells go down to 99, but it should work if you put in a generically high number, for example, 99999.
Put this in the row of the first value, so here that would be E2, and then copy it down to every row that has a value in column A.
The function works by just nesting IF statements, and can be expanded or contracted depending on how many columns you want to check against. The function can also be easily modified into a "check if in column B OR column C" function pretty easily.
here is an example for 1 value; I can't generalize since I did not see your data source
Sub test()
If (Range("D6").Value = Range("D6").Offset(0, 1).Value) Then
MsgBox "match"
End If
End Sub
you can also delete duplicates across multiple Columns by using DATA-> Remove Duplicates.