Index referencing with relative values - excel

I'd like to reference a cell in a named range depending on a value outside of this named range
What I would like to see is ideally:
INDEX(named_range;;COLUMN()-RC1)
However, INDEX does not allow for the RC1 whilst it recognizes Column().
One way would be to make RC1 part of the named range (by copying the data into first column of range etc), but as the named range is somewhere completely different to the cell containing the formula, "I don't want to...".
Any other options to achieve it?
Thanks
*Addition:
Maybe the purpose helps. The aim is to find the number of installed units (named_range) that need to be replaced in the future depending on the lifetime which is in RC1.*

As per comments, you need to take into account the start column of the named range to get the correct column, possibly use
=INDEX(named_range;;COLUMN()-RC1-MIN(COLUMN(named_range))+1)

INDEX has two version, you're thinking solely of the INDEX/MATCH (2D) version, but it also has an INDEX/MATCH/MATCH version, too.
INDEX(My3DRange, MATCH("Y", MyYValues, 1), MATCH("X", MyXValues, 1))

Related

Get Current Region with Office-JS

How do I get the current region surrounding the ActiveCell using the Excel JS API?
In VBA this is
Set rng=ActiveCell.CurrentRegion
The current region property in the JavaScript API has now been implemented. The property is called getSurroundingRegion()
There is no direct equivalent, but we do have a range.getUsedRange() that will take an existing range and give you a smaller range that represents the non-empty portions. Note that this method will throw a not-found error if there is nothing in the entire range (since effectively it's an empty range, which Excel can't express).
If you really need the CurrentRegion scenario (and I'd be curious to learn more), you could first get the used range (to ensure you're not loading too much data), then load the values property, and then do range.getExpandedRange(indexOfLastRow, indexOfLastColumn).
BTW, unlike VBA's usedRange, the JS "getUsedRange()" always creates an accurate snapshot of the current used range (the VBA one could get stale), and we're exposing it not just on the worksheet but also on a given range.
Update
What I mean is that there are a couple of scenario, one simpler, the other harder.
The simpler one: you know roughly what range you need, but you just need to trim it. For example, you know you have a table-like entity in columns A:C, but you don't know the row count. That's where
worksheet.getRange("A:C").getUsedRange()
would get you what you need.
The harder one: you use getUsedRange() to trim down what you can, but you then load range.values and manually do a search for rows and columns where each cell is empty (""). Once you have that (suppose you found that the relative row index you care about is 5, and column index 2), you could do
originalRange.getCell(0, 0).getExpandedRange(rowIndex, columnIndex)
Concrete example for the above: You have data in A2:C7, though the getUsedRange() of the worksheet is much larger (and hence my suggestion could try to trim it down further by doing a range.getUsedRange()). But for this case, let's imagine that getUsedRange on a worksheet returned a range corresponding to A1:Z100. worksheet.getRange(0, 0) would get you the first cell, which you can then expand by 5 rows and 2 columns (which you find through simple albeit tedious array iteration) to get the range you care about. Makes sense?

How to select cell range dynamically in sumproduct in excel

I am trying to use Sumproduct dynamically on a sheet that will be updated regularly.
My motive is to not manually select the columns ( i.e. not to use the shift key and down key to select the range A2:A6 as seen in the attached snapshot)
I want to have the formula "=SUMPRODUCT(--(A2:A6="TX"))" pick up the A2:A6 dynamically since my data in worksheet say A is everchanging. So that when the data has grown to A8, it can be somehow picked up dynamically than manually using the keys.
Can this be done? Please let me know if my question is not clear.
Screenshot of my xls
Regards
SM
Yes, you could define the range dynamically. However, given your current formula, I see no reason why you should not switch to COUNTIF here, i.e.:
=COUNTIF(A:A,"TX")
a function for which the use of entire column referencing (A:A here) has no detriment to performance (which is not at all the case with SUMPRODUCT).
Regards
Shitty recommendations guy, here.
Can you insert a new line above all in your sheet? You could put it really wherever you want and you can even hide it, the first line just works for me.
If you can, just make it an "count.values" function on the entire column with an appropriated subtraction to adjust the count to the right length of your data. E.g. Removing this auxiliary cell value plus a header would be "=count.values(A:A)-2".
Later, on your VBA, you could retrieve this value to a variable and concatenate it to your main function. Lets say you made the auxiliary on cell A1 (coordinates 1, 1)
Dim auxLine As Integer
auxLine = cells(1, 1).value
whatever.formular1c1 = "=SUMPRODUCT(--(A2:A" & auxLine & ="TX"))"

Selecting a variable range, but with constant last value

I'm seeing quite some of methods to select a range, but not for my problem. I want to select a variable range, and i think the best way is to tell it to go upto a certain value. (then copy paste etc)
Within the range (one column) the number of rows will vary regularly. Also, there will be several empty rows within that range, and below it will be more full rows.
So, i'm thinking i put in an Anchor value, and always select the range upto that value, so adding and removing rows will not interfere. But how do i do that...
edit: So it's important i only copy upto that anchor value, what is below it must not be copied...
Anyone?
Solved As;
Set EndCell = ActiveSheet.Range("L7:L45").Find("AnchorSec", LookIn:=xlValues)
ActiveSheet.Range("L7", EndCell).Copy
AAR.Worksheets("Portfolio_2016").Range("K7").PasteSpecial
I still have to be careful with the range in which it looks for AnchorSec, but that could in theory be upto L1000 or where ever.
As an example if you were looking to have the range C1:C100 to be variable, you can just type C:C there. There is no need to specify the row numbers. (Am not sure if I have gotten your problem correctly. Sorry can't type this in the comments, don't have the reputation)

Using Named Range as single cell references in formulas that accept arrays

So background first, question second.
I recently discovered an interesting property of named ranges that I'm experimenting with and not finding much help. The property is this: If I name a range (a column in this example), I can use the named range as a reference in formulas and it will usually resolve as though it were a reference to the same relative position as the current cell within the named range. So if I call A:A "Alphabet" and it contains letters a through z, each in their own row, I can simply type =Alphabet in cell b26 and it will evaluate to "z" (i.e. A26 instead of a:A). Seems simple, but it is shaping up to be quite powerful, because there is essentially an index function built-in. Very useful for making tidy formulas.
Onto the issue: when I use this same technique with a range that accepts an array argument (i.e. MAX, EOMONTH, etc.), the reference is resolving in the standard way (Max(A:A)). If I wrap the reference in VALUE(), then it resolves to just the single reference within the larger range (a26). The question is simply can anyone think of any way to avoid needing to do this, or at least to make the wrapper as unobstrusive as possible?
Real world example: I have a list of employees and I want to determine which date from three named ranges is larger. So I have something like
='DateSameRow1' > Max('DateSameRow2','DateSameRow3','DateSameColumn', and it is resolving as =a10 > Max(b:b,c:c,2:2). Note the issue: The named range outside of MAX resolved one way, and inside MAX resolved another. Sure, I could just write = a10 > Max(b10,c10,d2) or whatever, but this is so much more readable in what will end up being a huge formula. Right now I'm having to write MAX(VALUE('DateSameRow2')) or whatever to get the result I want and it is defeating the purpose.
Thanks
You can use --NAMED_RANGE in front of the "offending" named range and it will force a negative VALUE and then undo the negative.
Per my comment, I would recommend you not build a spreadsheet on this functionality. The -- is even further removed from VALUE in indicating that a named range is being used in this way.

Excel, Specifying cell contents as a result of other cells

I don't use excel often, and I haven't really found a good solution to my problem. (which is probably really simple).
I would like to have a cell with a function in my spreadsheet that shows another cell value value that depends on yet another cell value.
Such as:
The Best Deal heading simply uses the formula
=MAX(D3,D1000)
But under Best Deal I would like to display the Name Test1 rather than the numeric value.
Another thing that would be nice to know, is if there is a way to know the maximum row with data in it. So rather than =MAX(D3,D1000) something like =MAX(D3,Max(RowCount_InD))
Obviously that function wouldn't work as I wrote it, but hopefully this pseudo code gives you an idea of what I mean. The purpose is that if more entries are added, it would be able to handle them.
I know this is possible, but I'm having some trouble. Hopefully I can get some help here.
Thanks!
The easiest way to do this is to use a combination of Index and Match. Match will find the position of the maximum value, and then Index will look in column A and return the data in that same position. So, your formula would be:
=INDEX(A$3:A$1000,MATCH(MAX(D$3:D$1000),D$3:D$1000,0))
Put that formula in F3. No hidden columns required.
Sorry, I missed the part about the expanding range. You can do that by using Count or CountA along with Offset. The new formula would be:
=INDEX(A3:OFFSET(A3,COUNTA(A:A)-1,0),MATCH(MAX(D3:OFFSET(D3,COUNT(D:D)-1,0)),D3:OFFSET(D3,COUNT(D:D)-1,0),0))
More complex, but it is basically the same except that it will expand as you add new values at the end.
There is only one 'simple' way I can think of this, but it requires hidden columns (sorry).
set E1 = A1 and fill down all the way (Basically you are making a copy of column A in column E but you are using a formula so it will always be the same)
Then under 'Best deal' - put this formula:
=VLOOKUP(MAX(D3,D1000),$D:$E,2,FALSE)
Then hide column E so it doesn't look like a mess. This way you do not need any fancy macro's and it will work everywhere because it is a normal formula.
"Another thing that would be nice to know, is if there is a way to know the maximum row with data in it. So rather than =MAX(D3,D1000) something like =MAX(D3,Max(RowCount_InD))"
This is a called a dynamic named range. Create a name for the ratio data, and set up the formula for the name range to be this:
=OFFSET($D$3, 0, 0, COUNTA($D$3:$D$1048576), 1)
More info here: http://www.ozgrid.com/Excel/DynamicRanges.htm
Then, assuming you name this named range ratio_data, your function could be referring to =MAX(ratio_data) in combination with index-match as suggested by #Tim Mayes. The range will expand automatically as you add more data.
=INDEX(A$3:A$1000,MATCH(MAX(ratio_data),ratio_data,0))
Ideally, you can replace the A3:A1000 by a dynamic named range as well.

Resources