Excel: An array in an array? - excel-formula

So, I would like to return the contents of all rows where the value in column A is, let's say, 1.
My thought process is that I could use:
=INDEX(row_range,MATCH(1,A:A,0),0))
But Match will only return one value here, i.e. the number of the first row which contains a 1 in column A.
Is there a way of creating an array with the Match formula (thus returning the multiple row numbers, all of which contain '1' in column A) and then place that in the Index array so that it then runs through each of the Match-array values and creates a big long list of values in one array which I can then list out on a separate sheet?
Hope this makes sense...
Here is a demonstration of what I'm hoping for, if that helps! The idea would be that the array as shown would be created, which could then be extended down the column as per the part underneath.
https://i.stack.imgur.com/nCusM.png

Use the file you showed in your example (as "Sheet1") and put these formulas into indicated cells in Sheet2:
Into cell A2 put
=AGGREGATE(15;6;ROW(Sheet1!A:A)/((Sheet1!A:A=1)*1);ROW(A1))
this will give you all the rownumbers where value in A column of sheet1 equals 1.
Into cell B2 put
=COUNTA(INDIRECT("Sheet1!"&A2&":"&A2))-1
this will give you how many cells are filled in that row.
Into cell C2 put
=TEXTJOIN(",";TRUE;OFFSET(Sheet1!$A$1;A2-1;1;1;B2))
This will give you all the cells with data from that row concatenated. If you dont have this formula (first time in 2016 I believe) you can use OFFSET function to list the values in separate columns and then CONCATENATE them.
Copy these three down as many times as you want and into cell C1 put
=TEXTJOIN(",";TRUE;OFFSET(C2;0;0;COUNTIF(Sheet1!A:A;1);1))

Related

How to add a row after determining the values of the cells above are the same in VBA for Excel

Im am trying to write a macro that ascending sorts items in a row and then adds a row between matching items.
basically I am looking for something like this
a
a
a
>>new row inserted here because there are no more instances of "a"<<
b
b
I am envisioning that I will need two variables, cellOne and cellTwo which will hold the values of, for example, cells A1 and A2 respectively. As the loop runs the values of cellOne and cellTwo will increment so that each cell in the column is compared to the cell above to determine if they match. The part that I am having trouble with is figuring out how I would write a for loop that iterates through the column and increments the values of my variables so that the value of variable cellOne for example increments from A1 to A2 and so on while at the same time cellTwo increments from A2 to A3.
There may be an easier way to do what I am describing. In a nut shell all I am looking to do is to find a way to insert a new line after any given number of cells in a sorted column match before continuing on to repeat this process for however many more ranges of matching cells there might be in a column.

How to compare each cell with each other in MS Excel?

In large dataset - 250 rows and 1000 columns I need to compare each value in cell with each other in one column and iterate over all column. Heres simplified example of source data:
And this is what I need (formatting not necessary and 2 empty rows not necessary) - if match if found "1" is produced, if no match "2" is produced, if one or both were N/A - "3" is produced:
Comparison should only be "one sided" for example Terry and Joey is the same as Joey and Terry, thus further comparison of already compared pairs is not needed.
Is it possible to do this in Excel 2016 or are there better tools for this?
My thanks to all.
This alternative is a bit complex, but we all solve problems like this differently. If it helps you, please feel free to use it. If not, I can understand since some of these techniques are not particularly common and the resulting formula is a bit unreadable. I did it this way so that I would be able to organize the rows better and read the matching/unmatching indicators more easily. I started by creating a helper column rather than repeat the rows for each individual so that each row shows the two names being compared. This is the formula I used to compare using B8's information is:
=IF(OR(INDIRECT("R"&MATCH($I8,$A$1:$A$6,0)&"C"&COLUMN(),FALSE)="N/A",INDIRECT("R"&MATCH($A8,$A$1:$A$6,0)&"C"&COLUMN(),FALSE)="N/A"),3,IF(INDIRECT("R"&MATCH($I8,$A$1:$A$6,0)&"C"&COLUMN(),FALSE)<>INDIRECT("R"&MATCH($A8,$A$1:$A$6,0)&"C"&COLUMN(),FALSE),2,1))
I am going to try to explain the formula I used as follows:
Without using the helper column, the basic formula for cell B8 is this:
=IF(OR(B$2="N/A",B3="N/A"),3,IF(B$2<>B3,2,1))
and this would work for the range B8:H11. However, when I skip down to B13, the formula would need to change to:
=IF(OR(B$3="N/A",B4="N/A"),3,IF(B$3<>B4,2,1))
and this would work for the range B13:H15. Likewise B17, and B20 would be:
=IF(OR(B$4="N/A",B5="N/A"),3,IF(B$4<>B5,2,1))
=IF(OR(B$5="N/A",B6="N/A"),3,IF(B$5<>B6,2,1))
for their respective ranges. I shy away from formulas where I have to remember what I need to change for each section (heaven forbid I should write any notes or read them if I did).
In order to do this, I used the person column (A) and my helper column (I) to determine which rows to compare.
MATCH($I8,$A$1:$A$6,0)
gives the row of the person value in the Chart from A1:H6 in the comparison
MATCH($A8,$A$1:$A$6,0)
gives the row of the helper value in the Chart from A1:H6 in the comparison
Since the data being compared is always in the same column, I just use COLUMN() to determine which column to use.
In cell B8, MATCH($I8,$A$1:$A$6,0) will tell me it is row 2 and MATCH($A8,$A$1:$A$6,0) will tell me it is row 3. Thus, I want to use the values in Row 2, Column 2 compared against Row 3, Column 2.
To tell Excel to compare Row 2, Column 2 against Row 3, Column 2 is fairly simple, but creating a formula that you can copy from cell to cell without having to modify it each time is not as easy, since each section is a bit different and there could be blank rows in between sections. What I did was to use indirect cell notation using "R1C1" syntax rather than the more common "A1" cell referencing.
In other words in column B8 this:
INDIRECT("R"&MATCH($I8,$A$1:$A$6,0)&"C"&COLUMN(),FALSE)
gives the value in Row 2 (for Terry), Column 2 and
INDIRECT("R"&MATCH($A8,$A$1:$A$6,0)&"C"&COLUMN(),FALSE)
gives the value in Row 3 (for Joey), Column 2 and
In both of the above, I am concatenating and R and a C to the numbers returned by the MATCH() and COLUMN() functions and using the FALSE parameter to tell Excel to treat the concatenated result as "R1C1" notation. In other words, this:
OR(INDIRECT("R"&MATCH($I8,$A$1:$A$6,0)&"C"&COLUMN(),FALSE)="N/A",INDIRECT("R"&MATCH($A8,$A$1:$A$6,0)&"C"&COLUMN(),FALSE)="N/A")
translates to this:
OR(R2C2="N/A",R3C2="N/A")
I realize that the helper column is a burden you did not ask for and I realize that the formula is overly complicated, but I can freely copy this formula to any column that has the two names and it will do a comparison for that day of the week.
Here is a picture of what I am describing:
Added comments
Just to carry the above a bit further, suppose you had a Sheet1 which had the rows of data to be compared and suppose this were limited to 250 rows with the same 7 columns (rather than 1000). I could create another sheet similar to the above along with another helper cell (I put it in A1) to automatically populate the person column and the helper column like this:
New Helper Cell value: 1 (essentially saying to start at the top). This would populate Cell A2 with the following formula:
=IFERROR(IF(INDEX(Sheet1!$A$1:$A$250,NUMBERVALUE(RIGHT($A$1,3)+ROW()))=0,"",INDEX(Sheet1!$A$1:$A$250,NUMBERVALUE(RIGHT($A$1,3)+ROW()))),"")
Basically this is just this formula:
=INDEX(Sheet1!$A$1:$A$250,NUMBERVALUE(RIGHT($A$1,3)+ROW()))
but is checking first to see if it results in zero and then is replacing it with blanks if it is an error. Copying this cell down Column A will populate that column with the names starting at the first row after the data row specified by A. If you have more headings or other data you would need to add additional amountst to the +ROW() portion in both occurrences in the formula. Column I gets populated siimilarly with this:
=IF(A2="","",INDEX(Sheet1!$A$1:$A$250,NUMBERVALUE(LEFT($A$1,3)+1)))
However, this value does not vary from row to row.
Now that the helper columns are populated, you can populate the formula a bit differently from the above (which had used the same sheet) for example in B2:
=IF($A2="","",IF(OR(INDIRECT("Sheet1!R"&MATCH($I2,Sheet1!$A$1:$A$250,0)&"C"&COLUMN(),FALSE)="N/A",INDIRECT("Sheet1!R"&MATCH($A2,Sheet1!$A$1:$A$250,0)&"C"&COLUMN(),FALSE)="N/A"),3,IF(INDIRECT("Sheet1!R"&MATCH($I2,Sheet1!$A$1:$A$250,0)&"C"&COLUMN(),FALSE)<>INDIRECT("Sheet1!R"&MATCH($A2,Sheet1!$A$1:$A$250,0)&"C"&COLUMN(),FALSE),2,1)))
The main difference from the first formula is the off sheet references to "Sheet1" that were added and the extension of the formula to cover 250 rows.
Here is a picture with Cell A1 set to 1:
Here is a picture with Cell A1 set to 3:
Using this, your Sheet1 values remain where they are and you can create a generic comparison sheet to compare the values of various rows of Sheet1. These can be dynamically built by changing the value in A1 or you can create dozens of similar sheets, each differing by the value in A1.
Not sure if any of this makes sense.
Good Luck
Just use a function like this(exemple for cell B4):
=IF(B3=B2;1;IF(B3="N/A";3;2))
Print of it working
Do it for each line and just drag it from the begining to the end.
EDIT: You should do an or in the 2nd if to make surre neither is "N/A"
=IF(OR(B3="N/A";B2="N/A";3;IF(B3=B2);1;2))

Return a name matching two values

I have a table of data in one worksheet and some analysis in a second. In the 'Data' worksheet each row contains a name and then a range of values (numerical). What I need to do is look across each row and where, for example, columns PN and PM both contain a 1 return me the name in column M. This list needs to then grow as I add more data to the table in 'Data'.
Hopefully that makes sense. I am doing something similar with a single value lookup using the below but cannot get it to work for multiple values:
=IF(ISERROR(INDEX(Data!$A$4:$QQ$4999,SMALL(IF(Data!$A$4:$QQ$4999=$J$6,ROW(Data!$A$4:$QQ$4999)),ROW(1:1))-1,13)),"",INDEX(Data!$A$4:$QQ$4999,SMALL(IF(Data!$A$4:$QQ$4999=$J$6,ROW(Data!$A$4:$QQ$4999)),ROW(1:1))-3,13))
I copy this down and then when the data is updated the blank cells are automatically populated. The new formula I need is similar to this, but rather than looking up one value J6 in the above, I need to lookup two.
I would use a helper column. Pick an unused column, say column QR and in cell QR4 enter:
=IF(AND(PM4=1,PN4=1),1,0)
In QR5 enter:
=IF(AND(PM5=1,PN5=1),1+MAX($QR$4:QR4),0)
and copy downward.
This column assigns a simple sequential value for each "collectable" row. For example:
Then in the other sheet, pick any cell and enter:
=IFERROR(INDEX(Data!M$4:M$4999,MATCH(ROWS($1:1),Data!QR$4:QR$4999,0)),"")
and copy down to collect the data:

Use formula to copy cell values to another sheet if certain condition is satisfied

I am trying to write a formula such that I can create a sub list in another sheet based on certain criteria.
I have two columns in sheet1 A & B. I want to copy a value from cell B to the new list in sheet2 only if corresponding cell in A satisfies certain criteria. Please tell how to do it.
You can't use a formula to copy cell values to another as they can only affect the one they are written in (generally)...
However, you can write a formula in Sheet2 to pull data from Sheet1 based on a condition...
For example, the array formula (entered with Ctrl+Shift+Enter):
=IFERROR(INDEX(Sheet1!$B$1:$B$4,SMALL(IF(ISERROR(FIND("o",LOWER(Sheet1!$A$1:$A$4))),9999,ROW(Sheet1!$A$1:$A$4)),ROW())),"")
In this example, I'm using the condition "the value in column A contains the letter 'o' - case insensitive"... Let me explain from the inside out...
ISERROR(FIND("o",LOWER(...))) would give us FALSE if the value has an 'o', TRUE otherwise
Rather than just giving a single value, we are working on the array of cells Sheet1!$A$1:$A$4 (which could be extended as you needed) - so this gives us an array of TRUE and FALSE values
IF(ISERROR(...),9999,ROW(...)) means that if there was no 'o' we will get the value 9999, otherwise we get the row number of the cell in the array - so this gives us an array of a mix of 9999 and row numbers
SMALL(...,ROW()) lets us pick out one of those values from the array - in this case the items in size order, and we are using ROW() as our counter (i.e. in Sheet2!A1 - ROW() is 1 and we get the smallest value from the array)... if we had a header row, we could do ROW()-1 for example... Effectively we are indexing through a sorted list of row numbers in Sheet1 that match our condition
INDEX(Sheet1!$B$1:$B$4,...) is going to give us the value from column B in Sheet1 relating to our position in the list of matching rows
Sometimes we have our 9999 (for all the cells that didn't match the condition - these are sorted to the end of the list with SMALL) and so the INDEX will give us an error... the index is outside the range. So we can replace it with a blank
Hope this makes some sense! Good luck! And remember to enter array formulas with Ctrl+Shift+Enter...

Excel: Find intersection of a row and a column

My question is how can I find an intersecting cell of a specific column and row number?
My situation is this: with some calculations I find two cells, lets say B6 and E1. I know that I need a row of the first one and a column of the second one. So I could just use ROW and COLUMN functions to get the numbers. After that, I need to find an intersecting cell. Which would be E6 in this example.
I would just use INDEX(A1:Z100;ROW;COLUMN) but I don't know the exact area that I'm going to need - it depends on other stuff. I could use something like A1:XFG65000 but that is way too lame. I could also use a combination of INDIRECT(ADDRESS()) but I'm pulling data from a closed workbook so INDIRECT will not work.
If this would help to know what is this all for - here's a concrete example:
I need to find limits of a section of a sheet that I would work with. I know that it starts from the column B and goes all the way down to the last non-empty cell in this column. This range ends with a last column that has any value in first row. So to define it - I need to find the intersection of this last column and the last row with values in B column.
I use this array formula to find the last column:
INDEX(1:1;MAX((1:1<>"")*(COLUMN(1:1))))
And this array formula to find the last row:
INDEX(B:B;MAX((B:B<>"")*(ROW(B:B)))
Last column results in E1 and last row results in B6. Now I need to define my range as B1:E6, how can I get E6 out of this all to put into the resulting formula? I've been thinking for a while now and not being and Excel expert - I couldn't come up with anything. So any help would really be appreciated. Thanks!
You can use an Index/Match combination and use the Match to find the relevant cell. Use one Match() for the row and one Match() for the column.
The index/match function to find the last cell in a sheet where
column B is the leftmost table column
row 1 is the topmost table row
data in column B and in row 1 can be a mix of text and numbers
there can be empty cells in column B and row 1
the last populated cell in column B marks the last row of the table
the last populated cell in row 1 marks the last column of the table
With these premises, the following will return correct results, used in a Sum() with A1 as the starting cell and Index to return the lower right cell of the range:
=SUM(A1:INDEX(1:1048576,MAX(IFERROR(MATCH(99^99,B:B,1),0),IFERROR(MATCH("zzzz",B:B,1),0)),MAX(IFERROR(MATCH(99^99,1:1,1),0),IFERROR(MATCH("zzzz",1:1,1),0))))
Since you seem to be on a system with the semicolon as the list delimiter, here is the formula with semicolons:
=SUM(A1:INDEX(1:1048576;MAX(IFERROR(MATCH(99^99;B:B;1);0);IFERROR(MATCH("zzzz";B:B;1);0));MAX(IFERROR(MATCH(99^99;1:1;1);0);IFERROR(MATCH("zzzz";1:1;1);0))))
Offset would seem to be the way to go
=OFFSET($A$1,ROW(CELL1)-1,COLUMN(CELL2)-1)
(The -1 is needed because we already have 1 column and 1 row in A1)
in your example, =OFFSET($A$1,ROW(B6)-1,COLUMN(E1)-1) would give the value in E6
There is also ADDRESSS if you want the location: =ADDRESS(ROW(B6),COLUMN(E1)) gives the answer $E$6
The following webpage has a much easier solution, and it seems to work.
https://trumpexcel.com/intersect-operator-in-excel/
For example, in a cell, type simply: =C:C 6:6. Be sure to include one space between the column designation and the row designation. The result in your cell will be the value of cell C6. Of course, you can use more limited ranges, such as =C2:C13 B5:D5 (as shown on the webpage).
As I was searching for the answer to the same basic question, it astounded me that there is no INTERSECT worksheet function in Excel. There is an INTERSECT feature in VBA (I think), but not a worksheet function.
Anyway, the simple spacing method shown above seems to work, at least in straightforward cases.

Resources