Using Individual Columns to Map Data to a Table Array - vba

Suppose I have two columns, C1 and C2, each of which contains real valued data. I would like to create a two-dimensional table from these columns where each row is a specific range of values in C1 (e.g. 400-500) and each column a specific range of values in C2 (e.g. 10-14). This is easy enough by sorting C1 and C2 and determining reasonable ranges. The key issue I is that I have a third column C3 whose values I would like to fill the table with. In particular, I would like to be able to select C3 and have it check the associated values in C1 and C2 and use this information to place the value in the appropriate cell at the intersection of those two values of the table. Is this something that would require VBA?

If I understand correctly, your C3 has (C1, C2) as coordinates. Your original coordinates in C1 is a number but in the matrix it is a non-overlapping range, e.g., C1 can be 413 but this falls under the range 401-500 in your new matrix. It is possible to do this without VBA by using VLOOKUP for data mapping and transformation.
Of course, as the method gets more and more complex, to facilitate future reuse, amendments and readability, it is recommended to use VBA, where it is simpler to add comments and see the algorithm at work in a continuous flow.
If you want to avoid using VBA, what you could do is derive the range in which C1 falls using VLOOKUP range lookup, and combine this range and the C2 value into one cell, separated by a comma, then sort data in C3 by this new column in ascending order. Note that when using VLOOKUP range lookup, it will always give a response, as it will look for the maximum value that is equal to or less than your C1 value, so it tends not to result in #N/A; you have to make sure that your ranges cover all possible values of C1.
Assuming your C1 ranges are found in Column A and your C2 values are all found in Row 1, you could use VLOOKUP to populate the matrix, then handle any #N/A thereafter.
So we have:
Table of C3 values
C1 values in Column B
C2 values in Column C
Combined coordinates in Column D in Sheet
Resulting C3 values in Column E
Table of C1 ranges
Minimum value of range in Column A
Actual range in Column B
Create the table of C1 ranges
Min val Range
0 0
1 1-100
101 101-200
201 201-300
301 301-400
401 401-500
501 501-600
etc.
Combine the C1 and C2 coordinates into Column D. For example, this translates C1 = 413, C2 = 21 to "400-500,21",
=VLOOKUP(B2,TableC1Ranges!$A$2:$B$100,2)&","&C2
Then Sort by Column D and use VLOOKUP in the matrix of C1 and C2 values
=VLOOKUP($A2&","&B$1,TableC3!$D$2:$E$1000,2,FALSE)

Related

Google Sheets Query to unpivot and fill null values

I am writing a complex Google Sheets formula, which I think I can simplify using the Query function. The Query formula takes two inputs: 1) data, 2) query. The data consists of about 20 columns where the odd-numbered columns are categories and even-numbered columns are the values of that category. (Note that there are a fixed number of rows, but various columns have variable number of non-blank entries.)
Column A
Column B
Column C
Column D
1st Category
Value B1
2nd Category
Value D1
Value B2
Value D2
Value B3
My dream output is as follows:
Categories
Values
1st Category
Value B1
1st Category
Value B2
1st Category
Value B3
2nd Category
Value D1
2nd Category
Value D2
...etc.
...etc.
Thanks for any thoughts !
Note: Common Table Expressions can't seem to be used in the Query function.
Add'l note: I don't mind and partially expect that I will have to repeat the query for the transformation of Column A and Column B ten times to get all the data, which is fine. The simpler first-step question is how to do that.
Sample Google Sheets for reference/work: https://docs.google.com/spreadsheets/d/132E5CYwcv-ovWbZTnYqRMMqrR2X2pDv28Pj2bdW7w1k/edit?usp=sharing
use:
=ARRAYFORMULA(QUERY(SPLIT(FLATTEN(IF(
TRANSPOSE(QUERY(TRANSPOSE(Data!B1:L5), "skipping 2", ))="",,
TRANSPOSE(QUERY(TRANSPOSE(Data!A1:L1), "skipping 2", ))&"×"&
TRANSPOSE(QUERY(TRANSPOSE(Data!B1:L5), "skipping 2", )))), "×"),
"where Col2 is not null order by Col1", ))

How to use INDEX and MATCH to based to cells?

I have two sheets of data in excel. I want to combine cells to bring the second cell value. Here is an example:
when I enter combining columns name and list must get value form V1 and V2.like this:
I don't have the time to type your data, so this is a rare case of a non-tested formula and may need some tweaking.
For the V1 result: (top screenshot starts in Sheet1 column A, bottom screenshot in Sheet2, column A). This formula goes into cell B2 in Sheet 2:
=Index( Sheet1!$E$2:$E$100, MATCH(Sheet2!a2,INDEX(Sheet1!$A$1:$A$100&" | "&Sheet1!$D$1:$D$100,0),0))
Adjust the first index range from column E to F for the V2 result in C2.
Copy down.
Don't use that approach with whole columns. It will be slow. Use ranges with defined rows.
Of course, if the codes in Sheet1 column A are unique, you don't need to combine them with the value in column D and can look up only the first three characters, like this for V1 (use 6 for V2)
=vlookup(Left(Sheet2!A2,3),Sheet1!$A:$F,5,0)

Find value in column, based on 2 criteria

I have a file with 3 columns. Column A contains 300,000 rows, with about 200 separate IDs, all duplicated at least 1,000 times. Column B contains the date for each of the rows. Column C contains the values that I need to extract.
Each of the 200 IDs in Col A can have multiple values (e.g. ID 1234 might have dates 1/1/2001, 1/3/2001, 1/2/2015, etc). Similarly, each date on Col B will have multiple IDs (e.g. 1/2/15 might have IDs of 1234, 1874, 1930, 6043, etc).
In a nutshell, I need to check the values in Col A and Col B to find the relevant ID in Col A and the maximum value in Col B, and return the value in the relevant cell in Col C.
I've looked at Index/Match examples, but they don't seem to be suitable. Is there any suggestions on a macro I could run, that would accomplish what is needed.
Use this array formula:
=INDEX($C$1:$C$300000,MATCH(1,IF(($A$1:$A$300000="1234")*($B$1:$B$300000=MAX(IF($A$1:$A$300000="1234",$B$1:$B$300000))),1,0),0))
Being an array formula it must be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
Change the "1234" to a reference cell with the appropriate ID.
You can accomplish this using array formulas. To start, you can retrieve the maximum date in column B when column A is 1234 using the below formula. Keep in mind that you have to use Ctrl-Shift-Enter when you finish typing an array formula.
{=MAX(IF($A$2:$A$24=1234,$B$2:$B$24))}
Note that you will need to change the ranges to include all of your data, rather than my test data on rows 2-24.
Now that you have a formula to retrieve the max date, you can put that inside an index/match and, again using Ctrl-Shift-Enter, use the below array formula to retrieve the value in column C for a row matching 1234 and the maximum date.
{=INDEX($C$2:$C$24,MATCH(1234&MAX(IF($A$2:$A$24=1234,$B$2:$B$24)),$A$2:$A$24&$B$2:$B$24,0))}

Check if one of multiple values is present in a column

I have a table in Excel 2013 that has has thousands of records of food items (Beef-frozen, beef-chilled, beef-brisket, beef-ribs, chicken-fillet, chicken-whole, fish-skinned, fish-whole, yogurt, lettuce-imported, lettuce-frozen, tomato-fresh,tomato, water, milk,...etc) stored in column A. Notice the value may contain other content than the food item name.
I created column B next to column A. I want column B to hold the category of the food item in column A. For example, if A1 has in it "Beef" or "Chicken" or "Fish" then B1 should equal "Meat". If A1 has in it "Tomato" or "Lettuce" or "Onion" then B1 should equal "Vegetable".
What is the best way to achieve it?
Assuming you have column headers, enter this formula in cell B2:
=REPT("Meat",MAX(IFERROR(MATCH({"*beef*","*chicken*","*fish*"},A2,),))) & REPT("Vegetable",MAX(IFERROR(MATCH({"*tomato*","*lettuce*","*onion*"},A2,),)))
This is an array formula and must be confirmed with Ctrl+Shift+Enter.
Now copy B2 and select B3 down as far as you need and paste.
Note: please look closely at the big gap in the middle of the formula. You'll see that this is really two separate formulas concatenated together with an ampersand. You can easily extend this formula in the same way by adding another phrase similar to the first two for a new category. In fact, you could add many more categories in this fashion.
Set up a two column table. Name it, for example FoodTable. Have the first column Named Word (for keyword) and the second column Type, for the type of product. Something like this:
Then, with your data in column A, enter the following formula in B1 and fill down:
=LOOKUP(2,1/ISNUMBER(FIND(FoodTable[Word],A1)),FoodTable[Type])
Results:

Flag Data in Excel Programatially

I have two sheets in an excel workbook. One of the sheets has 8 ID numbers. The other sheet has about 5000 rows, not every row matches one of the ID numbers on the other sheet. I want to flag the rows where an ID number is an exact match to the other sheet and extract them to a separate sheet.
At the moment I was thinking I could just type in
=IF(A2=sheet2!b3,1,0) OR IF(A2=sheet2!b4,2,0) OR IF(A2=sheet2!b5,3,0) OR IF(A2=sheet2!b6,4,0) OR IF(A2=sheet2!b7,5,0) OR IF(A2=sheet2!b8,6,0) OR IF(A2=sheet2!b9,7,0) OR IF(A2=sheet2!b10,8,0)
Then copy and paste them to a separate sheet, but this doesn't work for some reason.
Any help would be much appreciated.
I didn't fix your formula for all the 8 ID's, but you should be able to add additional checks.
=IF(A2=$B$3;1; IF(A2=$B$4;2; IF(A2=$B$5;3;0)))
This basically reads as:
compare A2 with B3, if they match return 1 (note the use of $ sign in order to always use the B3 cell, this is called absolute cell reference)
if it doesn't match then compare A2 with B4 and if that matches return 2
if it doesn't match then compare A2 with B5 and if that matches return 3
if it doesn't match return 0
If you wish to compare to all 8 IDs you should just nest the if's a bit more.
A simpler solution would be to use VLOOKUP.