Test if cell contains "#REF!" - excel-formula

I have an Excel workbook, with many pivot tables on each sheet. I'm using the pivot tables to display lots of "counts" from my analytic table on another sheet. For example, if the attribute A three levels each, I'm making tables of how many rows fall into A1, A2, and A3.
I also need to create separate, non-pivot tables, which reference the original pivot tables in order to fill in the values which are missing from them. So from the above example, if there are currently no rows with attribute A2, that row wouldn't exist in the pivot table. However, later when I get an updated data sheet I can expect there to be rows with attribute A2. I'd like to create a reference in my second table which can evaluate to 0 when there is not a row for A2 in the pivot table, but when everything gets refreshed takes on the now existing value for A2.
When I try to reference the row using GETPIVOTDATA, I get #REF! if the cell doesn't exist in the pivot table. Essentially, I've just had trouble figuring out how to say:
if (GETPIVOTDATA equals #REF!)
cell equals 0
else
cell equals GETPIVOTDATA

Maybe =IFERROR(*your formula*,0). In other words, if your formula returns an error display 0.
IFERROR detects #N/A, #VALUE!, #REF!, #DIV/0!, #NUM!, #NAME?, or #NULL! but if coming from a PT that might be acceptable, though not exactly what you ask.

You can test it by =ISREF(...) or =ERROR.TYPE(...) (should be equal 4 for ref type error) formulas.

Related

Function in VBA for multiple filter

Can someone help me write VBA function to get data from another worksheet using multiple filter?
Data looks something like this.
I want to write a function that extract the A1 or A2 or A3 value based on the dropdown I select. If I select A3 it should pick data from A3 column. My Filter criteria on other columns are Item, id and location. Column for Item, id and location are static. While column for A1, A2, A3 are dynamic. I want to put criteria on Ite, id and location. These are the three criteria and result should be from the fourth column. i.e. either A1 or A2 or A3 based on what I select.
I tried but couldn't figure out. Can someone help me on this, please?
You don't need VBA for this but you do need to make a few preparations. I will show here what I did. There are other ways and you can choose the way you prefer.
I created a table exactly as you posted. Instead of a table you can just create a named range or you can replace the names of either in the formulas with the range's coordinates. I didn't name the table but recommend that you do if you use a table. In my example the table's name is Table1.
Within the table I created a named range comprising the cells D1:F1. I called this range "Data" but any other name will do as well. You may also move the named range entirely elsewhere if you want different captions for the columns for one reason or another. As you will see, the names are insignificant they are used to create the numbers 1, 2 and 3 from the location where they are within the named range Data.
Now I created a validation drop-down referring to a List of =Data. The effect is that I have a drop-down with A1, A2 and A3 in it. I created this drop-down in A10 of a different sheet from the one on which I have Table1.
Now I used the following formulas to extract data from row 2 of the table.
=INDEX(Table1[Item],2)
=INDEX(Table1[Location],2) or =INDEX(Table1,2,3) and
=INDEX(Table1,2,3+MATCH(A10,Data,0))
Observe that every "2" in the above formulas refers to the 2nd row in the named range Table1. I didn't set up a range of that name but that is something Excel threw in when I created the table. However, you would like to pull data from other rows as well.
For that purpose you can use the ROW() function. This function returns the number of the row in which it resides. If it's in row 10 it will return 10, in row 11 it returns 11 etc. It's a counter. Therefore, if you entered my formulas in row 10 you can replace all the "2"s with Row()-8 and as you copy up or down you will get data from different rows, same columns.
=INDEX(Table1[Item],Row()-8)
=INDEX(Table1[Location],Row()-8) or =INDEX(Table1,Row()-8,3) and
=INDEX(Table1,Row()-8,3+MATCH(A10,Data,0))
If your first formula isn't in row 10 you must adjust the number to be deducted according to where your formula was entered.

Excel data constraint error with custom formula

I wrote a formula for an excel data constraint that works in a cell. Using this formula for an excel data constraint always evaluates to false which means I keep getting an error in my custom formula for my data constraint.
I'm stuck since the formula works in a cell but not in a custom data constraint. I want the formula to only allow input that is in another table. So when a user types in a value it will give an error if it doesn't exists in another table.
Here is my formula so far
IF(ISERROR(VLOOKUP('Table1'!A14, Table2[#All], 2,
FALSE)),FALSE,TRUE)
Table1 is the table I want the data constraint on. Table 2 column 1 is the table I want to check if the value I enter exists in there. If the value doesn't exists in another table then I want to block the user from entering data into that cell.
Problem is formula in validation is not taking Table as range. So, use named range instead of Table. See the below formula. Here MyRng is a named range of range Sheet1!$F$3:$G$8
=IF(COUNTIF(INDEX(MyRng,,1),B3)>0,TRUE,FALSE)

Return an entire table according to value selected by user in Excel

I have a cell configured with a drop-down list, from which the user would pick an option. Depending on what the user selects, I want a table to be displayed in a given part of the Excel file.
This table has three columns and some 15 rows, with the corresponding headers.
I tried with VLOOKUP and INDEX, but they are not working, as they only returns one value.
Let's suppose the drop-down cell is in tab "Data", cell A1. The table is in tab "Table", from cells A1 to C16. I'd like the returned data to be displayed in any part of tab "Data".
Any advice will help.
You can do this using a combination of the Offset, Indirect, Row and Column functions as well as the Vlookup, with the lookup listing the location of the first cell in the table for each value in A1. For example, the Vlookup looks for the value in A1 in the columns G:H, and returns the text description of where that table starts. The Indirect changes the text string to a reference to that location, and then the Offset returns the value a certain number of rows or columns away. The formula entered where you want the data table to be displayed is:
=OFFSET(INDIRECT(VLOOKUP($A$1,$G:$H,2,FALSE)),ROW()-3,COLUMN()-2)
I think this may be clearer by looking at the pictures below.

Excel - returning the values from a range starting with the first non-blank value

I have a pivot table that shows the number of patients that collected medication in different months, grouped in columns according to month of first treatment.
To the right of the Pivot Table is another table where I want to show the columns from the Pivot Table side by side, with the first cell in each column being the first non-blank cell in that particular column. Put in a different way, I want to show the information in the Pivot Table but without the blank spaces above the first non-blank value in each column.
In the spreadsheet above, I have experimented with the following formula in the rightmost table:
=INDEX(B7:B20;MATCH(TRUE;INDEX(B7:B20<>"";0);0))
As you can see from the attached image, this formula does not give me what I'm looking for. It keeps returning the same first non-blank value in each column until it hits the row in the Pivot Table where that value shows up. Also, after going through all the cells, it keeps returning the cell in the last column of the Pivot Table over and over again.
Any ideas on how to correct this?
Based on your provided information, use this formula in cell M6 and then copy over and down:
=IF(ROWS(M$5:M5)>COUNT(B$6:B$19),"",INDEX(B$6:B$19,SMALL(INDEX((ROW(B$6:B$19)-5)*(ISNUMBER(B$6:B$19)),),COUNTBLANK(B$6:B$19)+ROWS(M$5:M5))))
And it looks like your regional argument delimiter is a semicolon instead of a comma, so here's the semicolon version:
=IF(ROWS(M$5:M5)>COUNT(B$6:B$19);"";INDEX(B$6:B$19;SMALL(INDEX((ROW(B$6:B$19)-5)*(ISNUMBER(B$6:B$19)););COUNTBLANK(B$6:B$19)+ROWS(M$5:M5))))
From what I understand you want to skip blanks. This formula should work for text and numbers in column B
=IFERROR(INDEX(B$7:B$30,SMALL(IF(1-(B$7:B$30=""),IF(1-(B$7:B$30=""),ROW(B$7:B$30)-ROW(B$7)+1)), ROWS($F$7:F7))),"")
Use CTRL+Shift+Enter when entering it, as it is an array-formula

Update table with another table based on cell value

After searching, I understand I can copy data into a table one at a time using VLOOKUP. However, is it possible to replace an entire table with another if a cell contains a certain value?
For example,
I have a table1. I want to replace table1 with table2 if cell a1="table2"
The reason is I will have many tables and depending on what I put in a cell, A1, I want table 1 to be what ever table I choose. I also want it to be easily updated when I add even more tables.
Edit for more information:
I have one table lets call it table1. Another table, Table2, with columns Name and Values. Also I plan on having more tables, so Table3 with columns Name and Values. I have a cell somewhere on the page, or another page. If I write Table2 in this cell. I would like the entire Table1 to be replaced by table2. Ultimately copying the data in.
I have tried structured tables, but it only copies from the same row. If I put it in another row, i get null basically. Vlookup, I think requires me to have a formula for each row of the table. I might be using this formula wrong.
Without knowing more, one solution is INDIRECT().
Example workbook sheets:
Table1
Table2
Table3
Assume all Table# tabs have the same cell range.
Make a new tab Input:
[A] [B]
[1] User Input
[2] Table # 1
[3]
[4] Reference ="'Table"&B2&"'!"
The formula in B4 evaluates to 'Table1'!. Note that the sheet reference includes the apostophes in case the sheet name has spaces.
Make a new tab Summary Table:
[A]
[1] =INDIRECT(Inputs!$B$4&ADDRESS(ROW(),COLUMN()))
This formula displays the value in the specified sheet name (from Inputs!B4) and the current row and column (so when this formula is entered in A1, it will display the value of Table1!A1). Then fill this formula across the rows and columns necessary to display the table specified in the Input tab.
Note of course that you could combine the Input and Summary Table tabs, but you would need to adjust the ROW() and/or COLUMN() formulas. This is also the case if your source tables do not begin in cell A1.

Resources