Dynamically Pick a Dynamic Range - excel

I've spent several hours trying to come up with a clean solution to this, and I just don't seem to be able to.
Basically, I have several dynamic ranges that I have defined, and I want to select one based upon a condition. It seems so straightforward! The reason that I want to select this dynamic range is so that I can graph the range.
Indirect does not work.
DIndirect (A common VBA algorithm) does not work.
(Or I can't get them to)
Choose works! However, the maximum length of a dynamic range is very quickly reached, which means I'm forced into inane solutions like the following.
I define dynamic_dynamic
=IF(A1<3,CHOOSE(A1,'WorksheetName'!dynamic_range_1, 'WorksheetName'!dynamic_range_2),'WorksheetName'!dynamic_dynamic_2)
Then I define dynamic_dynamic_2
=IF(A1<5,CHOOSE(A1-2,'WorksheetName'!dynamic_range_3, 'WorksheetName'!dynamic_range_4),'WorksheetName'!dynamic_dynamic_3)
Then I define dynamic_dynamic_3
=IF(A1<7,CHOOSE(A1-4,'WorksheetName'!dynamic_range_5, 'WorksheetName'!dynamic_range_6),'WorksheetName'!dynamic_dynamic_4)
.... And so on.
Really? I'm sure I'm being an idiot, but nothing else seems to work!

Related

VBA loop preference

I am currently trying to optimize a VBA code with extreme execution time (56 hours and more on high end PC). The main problem is nested loops (up to 8) due to complicated iterative calculations and referencing ranges in formulas.
I have two questions:
1)
What loops are most preferred in VBA (based on their hardware strain for lack of better word)
2)
Is there a way to reference variable range of cells in a formula? For example in formula:
=AGGREGATE(15,6,SQRT((R2C10:R500000C10-RC[10])^2+(R2C11:R500000C11-RC[11])^2+(R2C12:R500000C12-RC[12])^2),1)
I am referencing relative cell: "R500000C10". There are two main issues: For one, the half milion cells might not capture all data - though in most cases there is less active cells than that, in same cases that might be an issue. Second, it takes forever to scan through so many cells (same reference is used in multiple of VLOOKUPs).
The first issue could be solved by referencing the whole column, but that makes the second problem worst.
I would like to reference it in a similar way that xlDown works:
Range().End(xlDown).Select
The problem there is I would need to reference it within the Active.Cell.FormulaR1C1. Is there a way to do that?
Thanks!
Well you wrote that you have a code with 8 loops and try to optimize it maybe you can edit and put part of that code, because your description is very general, however, with information that you put i speculate that about your point
1)it depend what do you want to do? because each "loop" have their own use for specifyc scenaries
2)there are tricks to do that but all depend what are you trying to do and why you want to use .formulaR1C1
again im speculating but you can try tu separate your loops in indenpendent function, and maybe you are putting a formula inside cells, and question is why? you can create functoins and Sub in your VBA code and do your "extrem execution" without use xtra "resours"

Can I use MINIFS or INDEX/MATCH on two non-contiguous ranges...?

Problem is straightforward, but solution is escaping. Hopefully some master here can provide insight.
I have a big data grid with prices. Those prices are ordered by location (rows) and business name (cols). I need to match the location/row by looking at two criteria (location name and a second column). Once the matching row is found (there will always be a match), I need to get the minimum/lowest price from two ranges within the grid.
The last point is the real challenge. Unlike a normal INDEX or MINIFS scenario, the columns I need to MIN aren't contiguous... for example, I need to know what the MIN value is between I4:J1331 and Q4:U1331. It's not an intersection, it's a contiguous set of values across two different arrays.
You're probably saying "hey, why don't you just reorder your table to make them contiguous"... not an option. I have a lot of data, and this spreadsheet is used for a bunch of other stuff. So, I have to work with the format I have, and that means figuring out how to do a lookup/min across multiple non-contiguous ranges. My latest attempt:
=MINIFS(AND($I$4:$J$1331,$K$4:$P$1331),$B$4:$B$1331,$A2,$E$4:$E$1331,$B2)
Didn't work, but it should make it more clear what I'm trying to do. There has GOT to be an easy way to just tell excel "use these two ranges instead of one".
Thanks,
Rick
Figured it out. For anyone else who's interested, there doesn't seem to be any easy way to just "AND" arrays together for a search (Hello MS, backlog please). So, what I did instead was to just create multiple INDEX/MATCH arrays inside of a MIN function and take the result. Like this:
MIN((INDEX/MATCH ARRAY 1),(INDEX/MATCH ARRAY 2))
They both have identical criteria, the only difference is the set of arrays being indexed in each function. That basically gives me this:
MIN((match array),(match array))
And Min can then pull the lowest value from either.
Not as elegant as I'd like... lots of redundant code, but at least it works.
-rt

Excel: Returning values from a range that haven't already been specified

I have a dataset in long format with a unique ID representing people at different points in time. I already have a list of ID's with the first code, but would like to add a column for the second and third code, if available.
An example of the data, with what the output should look like at the bottom
I already have the first two columns in this example. I was thinking I would specify that I want to look at the values that are not the first code, and output a different code in the range if available, otherwise it returns blank. I also want to be able to use the command with few modifications in a third cell for a third code, again as long as it exists.
Ideally this can all be done without the use of vba, however if that would be easier, then by all means go ahead. Any solution not necessarily following my logic map are appreciated as well. Thanks in advance for the help!

Named Range "name" vs name

I have a ridiculous problem in Excel 2003 where I want to reference a Range I have defined myself, with names such as Div1, Div2, Div3 etc.
I have a macro that determines whether I need to use Div1, Div2, Div3 etc. and I then need to use VLOOKUP and MATCH with these different ranges.
However:
MATCH("ValueSearched", Div1, 0) works fine, but
MATCH("ValueSearched", "Div1", 0) fails
Since Div1 is determined programmatically, it is only stored as a string and I cannot use it.
I understand that in normal programming, you never really reference values like this and would use a hash table or similar, but I thought Excel would have a better work around as everything is done runtime.
Any suggestions on how I can dynamically reference these ranges?
pnuts solved it.
Have you tried =MATCH("ValueSearched",INDIRECT(Div1),0)

Chart always complains about invalid references - Excel 2007

I made a XY plot that shows points from one data set in two different colors, depending on a set of conditions. I achieved this by making the source table three columns instead of two. First column is the X. Second column is Y is one set of conditions apply, third column is Y is the other set of conditions apply. So the second and third columns have formulas like this in them, respectively:
=IF(ConditionApplies,YValue,"")
=IF(ConditionApplies,"",YValue)
(So the graph actually has two series, each of which is not a contiguous block of numbers - each is interspersed with "nothing")
When I make a change that affects the ConditionApplies, the table reacts properly. Then I switch to the chart (on a different sheet) and it always says: "A formula in this worksheet contains one or more invalid references...". Click OK.
The chart itself always looks the way I would expect, with two different sets of points according to the Conditions I devised. If I inspect the data source fields, all the references are intact and proper.
Basically everything works, I would just like to avoid this annoying pop-up.
Had the same problem. Deleted a data column and the chart that referenced it kept complaining.
Solution was to move the chart to its own page. then copy the chart and put it back into worksheet.
Hope it helps.
I 100% understand everything you've said here and, on the surface, it sounds like it's not any kind of bug. It seems like you are actually referencing something you shouldn't. If that's, in fact, the case that's obviously something you want to fix.
My first guess would be to look at your "ConditionApplies" formulas. Under certain cases, would they create invalid references (referencing data of the wrong type, dividing by zero, circular references, etc.). The most common cause of problems like that would be dragging formulas but not having the "$" signs in the appropriate places. So your cell references change when you expected they'd stay the same.
For example:
=SUM(A1:G25)
should be something like the following to prevent the column and row from incrementing when dragged:
=SUM($A$1:$G$25)
Recommendation
Look at the "ConditionApplies" formulas (or better yet, post them here) and aggressively place $ where ever they don't break things. Then "re-drag" your new formulas, updating the previous ones.
There is a microsoft KB 931389! about this problem with status "Confirmed, not fixed".
In my situation with chart and two series collection problem solved by adding a code to delete all seriesCollection before adding new data:
While Sheets(3).ChartObjects(1).Chart.SeriesCollection.Count > 0
Sheets(3).ChartObjects(1).Chart.SeriesCollection(Sheets(3).ChartObjects(1).Chart.SeriesCollection.Coun t).Delete
Wend

Resources