Reference a variable value in another cell as part of formula - excel

SHORT QUESTION
Ive got a formula =IF(ISBLANK(Q.1!C71),"",Q.1!C71) im trying to be able to set a control cell / variable so instead of C71 it would reference c*$B$1*, *$B$1* being my reference / control cell / variable in the same sheet as this formula, any ideas how to do this ?
LONG QUESTION
Ive got a spreadsheet that is regularly provided by a 3rd part with different data in it each time, although the column order of the data is always the same.
To get the data into my spreadsheet, i create another tab in the same workbook as their sheet and reference the data in their spreadsheet in a sheet i call import template sheet, i then from there copy and paste the import sheet into my spreadsheet, as all the columns match.
To reorder their columns in a non destructive way i use the following formula in my import template sheet, for instance if i wanted their column C, in my sheets column F i would place this in F1 and the pull the cell down so it does this for all rows :
=IF(ISBLANK(Q.1!C71),"",Q.1!C71) (the ifblank just helps sanitise the data by not throwing #ref if there is no data in the cell.)
The issue i have is that i have around 30 columns to re order and although the column structure is the same, there is sometimes some 'header' data (basically a text summary) at the top of the document of varying length, so instead of the first row i need being 71 sometimes it might be row 50 or row 90...
To speed things up id like to be able to have a control number / variable cell where i can set a variable for the starting row, and then reference that in my cell, so that i dont have the go and edit the formula in 30 different columns.

If you want to control the starting row of the reference by putting a number in cell B1, that would look something like this:
B1
71
Referencing Cell
=IF(ISBLANK(INDIRECT("Q.1!C"&B1),"",INDIRECT("Q.1!C"&B1))
INDIRECT allows you to build a string representation of a cell reference and then get the value of that cell. So we build a string reference using the value of B1, and then use INDIRECT to get the value of that string's referenced cell.

Related

How to make an INDIRECT formula but with adjusting ranges

I'm hoping someone can help me make better use of the INDIRECT formula.
I have a list of sheet names in a table and an INDIRECT formula that uses that list to return a value in a specified cell - the list of sheet names is just an easier way for me to drag the formula down the table and read the appropriate cells without having to manually link each sheet.
=INDIRECT("'"&A2&"'!"&"K10")
This works fine for single cells as the range ref is simply stated as the text in the formula (K10), the problem arises when I need to start referring to a range such as K10:K15 and summing the values.
The range K10:K15 will inevitably have new rows added or deleted on the relative tab and as INDIRECT is using text as the reference it means the range doesn't automatically adjust - this is as I understand it one of the 'benefits' of INDIRECT but in this case is actually holding me back.
Also worth noting that the range (K10:K15) may move as rows are added/deleted above this, as this range is part of a larger table.
In simplistic terms I want to achieve the same result as a standard reference to a range on another sheet, e.g. =sum(sheet1!K10:K15) (as this will adjust when rows are added/deleted) but I just want to be able to dictate which sheet is referred to via a list I have in a table on a summary sheet.
How do I either write INDIRECT so the range adjusts when new rows are added/deleted or is there a different formula I should be using that achieves this?
Any advice greatly appreciated :)
=INDIRECT("'"&A2&"'!K"& MATCH(TRUE,INDIRECT("'"&A2&"'!K:K")<>"",0)&":K"&MAX((INDIRECT("'"&A2&"'!K:K")<>"")*(ROW(INDIRECT("'"&A2&"'!K:K")))))
This indirectly references the rows from the first non empty cell up to the last non empty cell in given sheet in column K. Not sure if you need to enter with ctrl + shift + enter (not in the app version).
Note: If the range contains empty cells in between the first and last non empty cell it will be included as value 0
Or in office 365 use the following:
=FILTER(INDIRECT("'"&A2&"'!K:K"),INDIRECT("'"&A2&"'!K:K")<>"")

Selecting a Specific Column of a Named Range for the SUMIF Function

I am trying to create a SUMIF function that dynamically adds up values in a specific column of a named range in my Excel sheet.
It is very easy to do this when there is no named range :
The formula picks out all the cells that contain "London" in their name and sums up the expenses related to London.
What I am trying to do is to use a named range called TripsData (A2:B5) and tell the SUMIF function to sum the entries in the column 2 of this range that meet the criterion of having London in their name.
How can I make this work without needing to create a second named range for column 2 and simply by telling Excel to look within the specified column of this named range? Index/Match only return one value so that doesn't work when there are several cells with London in their name.
Thanks for your help!
Use INDEX to refer to a specific column in the named range (it can refer to a whole column), like this
=SUMIF(TripsData,"*London*",INDEX(TripsData,,2))
You can do that without any named ranges at all, if you turn your data into an Excel Table object. Select any cell in the range or the whole range and click Insert > Table or hit Ctrl-T.
There will be a dialog that asks if your table has headers. Yours does. Now you can reference the table and its columns by their inherent names and build your formula like this:
=SUMIF(Table1[Expense],"*London*",Table1[Cost])
You can rename the table, of course, even after the formula is in place. When you click a cell in the table, there will be a new ribbon for commands that relate to tables only. It's a very powerful tool.
Any formulas, formatting etc. that apply to a whole table column will automatically carry over into new table rows. The table column reference will adjust automatically, too, of course, so you don't have to mess with dynamic range names or re-define what a named range applies to.
Note: the formula uses structured referencing instead of cell addresses. This option can be turned off by clicking File > Options > Formulas > tick or untick "Use table names in formulas"
You can use Chris' idea of Index(Table1,,Col#) with the named range "Table1" (without creating an Excel table Object if you don't want to for some reason) and STILL avoid the problem Applez mentions in the comment below Chris' idea. Applez warns that using a constant for a column number reference is dangerous if you later insert another column before that column in the named range. You will find that Excel does NOT auto increment the constant, so your formula breaks.
Applez is right..... so DON'T use a constant, use a column number "reference" instead of a constant. For example....
=SUMIF(TripsData,"*London*",INDEX(TripsData,,Column(B1)))
If you later insert a column between A and B, Excel WILL auto increment the reference Column(B1) to Column(C1). Just don't delete B1 or Row 1 or you will get a REF error. I usually use the the header/tile "cell" (in whatever row that is in) for that table column within the Column reference (as it is highly unlikely I will ever delete the header/title cell of column of a table unless I delete the entire column). In this particular example as it turn out, B1 "IS" the the title/header cell for that column in the data table. So that is what I used for the example.
Awesome formula, just in case anyone needs to use a similar approach to FILTER a range. I used this approach
pmGendHC is the range I wanted to filter (I expect a spilled range with my data) I needed a colum (column number 13) to be different than 0
=FILTER(pmGendHC,INDEX(pmGendHC,,13)<>0)

VLookup Range Change when shifting and deleting columns in the range of the Vlookup function

I have the following search function:
=ALS((VERT.ZOEKEN(B6;'Raw Data'!$H$1:$BB$3000;21;ONWAAR))=100;"Winkel";
ALS((VERT.ZOEKEN(B6;'Raw Data'!$H$1:$BB$3000;21;ONWAAR))=400;"Woning";
ALS((VERT.ZOEKEN(B6;'Raw Data'!$H$1:$BB$3000;21;ONWAAR))=500;"Parkeerplaats";
ALS((VERT.ZOEKEN(B6;'Raw Data'!$H$1:$BB$3000;21;ONWAAR))=200;"Kantoor";
ALS((VERT.ZOEKEN(B6;'Raw Data'!$H$1:$BB$3000;21;ONWAAR))=600;
"Antenne";"Overig")))))
But when I change and delete some of the columns in the range of the Vlookup search with a macro (Vert.Zoeken=Dutch for Vlookup) The Range specified within the formula changes.
Why does it do that, and how do i stop it? I Couldn't find a clear answer anywhere else.
(The macro code just deletes some columns, and doesn't do anything else really)
Cell ID vs Cell Location
One of Excel's primary mechanics is that each cell effectively has its own "ID", which is represented by that the location of that cell at the time that it was referenced. The location of that cell can change, when columns & rows are manipulated.
For example: in A1, make the formula
=B5+D3
Then insert a row above row 3, and a column to the left of B. Your formula will now read:
=C6+E3
You'll notice that because the locations of the unique cells was changed, the formula accounted for that. This feature is incredibly useful, as otherwise, even simply formulas would need to be completely re-written if a new header was inserted above some numbers.
If you want the position of a reference to be "absolute" in the sense that it always points to the same location instead of the same cell ID, then you have a few options:
VBA solution to ignore this feature
By its nature, VBA code does not automatically adjust when cell references change. If you have a formula which references Range("B5"), then it will still say Range("B5") after you insert a new column to the left of B. In this way, you could use VBA to build the formulas within your worksheet. ie: VBA could re-write the formulas to reference the columns you want it to.
Excel solution to ignore this feature
To solve this without VBA, meaning your VBA code would not need to re-write the formulas, you could use the INDIRECT function. INDIRECT allows you to dynamically determine what a cell reference is, based on building a text string of a location. For example:
=VLOOKUP(A1,INDIRECT("B"&5+10&":D100"),2,0)
This will create the text string "B15:D100", and that will be the range referenced by VLOOKUP. Because you have entered the "B" & "D" as text values, they will not change when you insert rows/columns.

Excel offset where reference is to another worksheet

I am trying to create a series of formulas in Excel that use a reference on sheet 2, via a dependent cell on sheet 1 to display data from different rows on the sheet 2. I have searched here for some tips on this problem and discovered that using Offset is probably the solution, but I need some help on the last piece of the puzzle.
Basically, I need to be able to enter into A1 on Sheet1 the formula "=Sheet2!A1" and then, via an =offset formula have cell A2, A3, A4 etc on sheet 1 automatically display the contents of specified cells elsewhere on row 1 in Sheet2.
The reason for this is because sheet 2 is a large database and i'm trying to set-up sheet 1 as a summary sheet where users can display select information.
I can see how I could use the =Offset function in cell A2 on Sheet 1 to make this work if all the data was on one sheet, and how I could set the reference within the Offset formulas to =sheet2!A1, but what I want is for the reference to be A1 and for the offset to follow the target of A1 into sheet 2 and then locate the data there.
Can anyone help? Is this even possible? Am I going about this the right way? I can't use macros because the spreadsheet will be used by people who cannot have macros on their PCs
Thanks
I had this same question I'll share my solution.
Solution:
=OFFSET((INDIRECT(ADDRESS(1,1,,,"Sheet2")),ROW(),0,,)
References:
OFFSET(starting point, num of rows, num of col, [height], [width])
ADDRESS(row_num, col_num, [abs num],[a1],[sheet])
OFFSET can't take a reference from another sheet by itself. Neither will ADDRESS for some reason. In order to reference another sheet within a formula, you need to nest ADDRESS within INDIRECT like so:
INDIRECT(ADDRESS(1,1,,,"Sheet2"))
These two nested functions point to your Sheet1 cell A1. The first argument in OFFSET is your starting point, so you need to take the two nested functions and insert as the first argument to OFFSET like so:
=OFFSET((INDIRECT(ADDRESS(1,1,,,"Sheet2")),1,0,,)
Now you need to get that "1" to increment. The best way to do this is the ROW() function, which can return the row number of the current cell (for you, this is Sheet1 A1, returning a 1). Now we have:
=OFFSET((INDIRECT(ADDRESS(1,1,,,"Sheet2")),ROW(),0,,)
If you had started your formula in Sheet1 A5, instead of ROW(), you would need to insert (ROW()-4). Row 5 minus 4 equals 1. That's the part that I needed.
Hopefully this helps someone.

Find and "copy" a range to a new sheet

I have data that comes in into a sheet in certain time intervals, but every time the order might be different. So now, I want to "copy" the values from the "unsorted" sheet, to a new sheet which is sorted in the order I would like the data to be. From here on, calculations can be done using fixed references when referring to the "sorted" sheet.
Here is what I've done so far:
Determine the location of the label of the needed subset of the data in the unsorted sheet.
Determine where the actual needed data is on the sheet (the label is above the data, and one column to the right, so the data is one row down and one column to the left, and goes on for 32 columns and down 24 rows).
Now I want to do this step:
Use a array formula (CSE function) to reference those cells, as seen in this screenshot:
Trying to copy using CSE reference
Hitting Ctrl+Shift+Enter shows the result: #REF
What can I do differently?
Based on the feedback by #tigeravatar:
I referenced the range to the ASTIR sheet incorrectly:
in cell K13 is an invalid reference of "ASTIR!$A$5:ASTIR!$AF$29". You
don't need the sheet name twice, which is what's causing the #REF!
error.

Resources