Non-contiguous ranges in Excel solver - excel-formula

Can the excel solver handle non-contiguous ranges in a constraint? For example, I want to have decision variables in A1:A5,A7:A10. The solver interface accepts this as a range for decision variables, but if I then want to add a constraint that these variables should be explicitly made nonzero (or any other restriction), the "Add constraint" validator claims that "Cell Reference box is empty or contents are not valid." I get the same error if I use a named range reference instead of the direct string "A1:A5,A7:A10". Do I really have to split the constraint into two separate constraints?

Hello I also faced this issue,
And as a work around, you can make the the combination in a different cell then use it in solver as your cell reference for your constraint.
you can refer to this link.
https://www.solver.com/solver-tutorial-defining-constraints
"General Constraints
Suppose that cells A1:A5 contain the percentage of funds to be invested in each of 5 stocks. We would want the sum of these cells to equal 1 (or 100%). To accomplish this, in cell B1 you might calculate the sum of the percentages as =SUM(A1:A5) and then use solver to define a constraint to require that cell B1 = 1."

Related

Why does excel SUMIF function give wrong figure?

Taking sum of HP. But my formula is providing me wrong amount despite of criteria not being fulfilled!
I think it's worth posting an explanation as to why your original formula does not work, as it highlights an interesting feature of the function SUMIF.
As stated in one of my commments, you are passing a range parameter to that function of CN_2021[[#All],[HP]:[Sum of ORDER_AMT]], which is a reference to the entire table (including headers). Hence, you are effectively searching for the entry from P14 in all three columns, not just the first.
However, you are also using a sum_range parameter of CN_2021[[#All],[Sum of ORDER_QTY]], which is only one column. One might think that the construction should flat out error, since your range and sum_range parameters are not of an equal dimension (the former comprises 3 columns, the latter only 1).
The reason it does not error is due to the fact that, in such cases, Excel redimensions the sum_range so as to be of an equal dimension to the range.
So, if we replace the Table references with their equivalent worksheet references (assuming that table begins in A3) for the sake of simplification, we have that
=SUMIF(CN_2021[[#All],[HP]:[Sum of ORDER_AMT]],P14,CN_2021[[#All],[Sum of ORDER_QTY]])
which is equivalent to
=SUMIF(A3:C19,P14,B3:B19)
should error, though the single-column range
B3:B19
is redimensioned to a three-column range in line with the range, i.e.
B3:D19
which means that you are effectively performing
=SUMIF(A3:C19,P14,B3:D19)
This could have potential consequences were column D also populated: for example, place a 3 in cell C14 and 1000 in cell D14. The formula will now pick up this extra 1000 in the sum.
Strangely, unlike SUMIF, SUMIFS does not appear to be so lenient with respect to this redimensioning:
=SUMIFS(B3:B19,A3:C19,P14)
errors.

Excel Vlookup with cell reference

I have a cell range that I named "cell_range" in Excel. I want to extract the fourth row and fifth column from this table. The formula,
=vlookup(4,cell_range,5)
gives me the value I am looking for.
However, I also have the text "cell_range" in cell A1. Instead of typing out "cell_range" in my formula, I want to reference "cell_range" indirectly by referencing cell A1. The formula
vlookup(4,A1,5)
is giving me a "#N/A" error.
How can I indirectly reference the table "cell_range" in my formula?
Use INDIRECT:
=VLOOKUP(4,INDIRECT(A1),5)
While you can use INDIRECT to perform this kind of stuff, I steer clear of it for reasons I've outlined at https://chandoo.org/wp/2014/03/03/handle-volatile-functions-like-they-are-dynamite/
In this particular case I'd use a lookup table and either CHOOSE or INDEX, as demonstrated in the screenshot below:
Note that you need to pre-specify your areas with my approach.
Here's the syntax for CHOOSE
=CHOOSE(index_num,value1,value2,...)
Here’s the translation from Microsoft-speak into Jeff-speak:
=CHOOSE(Which area do you want?, First area, Second area, ...)
If using a dropdown or a cell reference to provide the index_num argument, you simply use a lookup table that converts the output of the dropdown or cell input into an index number that tells CHOOSE which range from the list should be used.
In this example, you’re using this approach to choose which area on the spreadsheet to sum. But CHOOSE and INDEX can be used to do a lot more than that. For instance, you could use them to allow a user to dynamically pick which lookup table to use when doing a VLOOKUP. Or you could let the user dynamically pick which range in which sheet to user for some calculations do. Very powerful stuff indeed!

how to reference to a range being a combination of two or more separate ranges in pure Excel

I want to reference a combination of two ranges that would be treated as one reference in pure Excel function. Specifically, in DGET function I would like to provide Criteria range that would be a combination of two or more ranges: one range with names of columns and the other separate range(s) with criteria. I tried to write DGET(TableRange, "Column_Name", CHOOSE({1,2},A1:B1,A10:B10)) and received an error "A value used in this formula is of a wrong value" probably because I provided an array rather than a reference.

How to Use Cell Text From Cell Being Checked by COUNTIF in Excel

What I'm wanting to do is have a formula in one cell that counts the values in a range that conform to a lookup of that range cell's value compared to another cell.
OMG, now that I look at it, that is totally confusing. Let me try to clarify a lot here.
Say we have Cell1, which will hold the counting formula. I have a list of values in a two-column table, Table1. The range, Range1 that Cell1 will be counting from is a range of cells that have List Validation in them. Table1 holds references to all values that can result from those Lists, in column 1. I have another cell, Cell2, which holds a number value. Column 2 of Table1 holds values that reference Cell2. I need to count the number of values from Range1 whose row matches in Table12 match the value in Cell2. Is there a way I can do this with COUNTIF without referencing each cell individually? Is there some shorthand (like Range.currentValue) that I can use to get the value of the cell currently being checked? The range is 11 rows long, and I need to do a second range that has 12 rows counted.
Man, I really don't know how to clarify that any more... I'll post this for now, in case anyone can understand what I'm saying and knows the answer, while I work on a sample spreadsheet I can upload.
I did my best to visually represent what I'm trying to accomplish:
http://gyazo.com/b83295baf3b156683a5c39b40c806504
Extended explanation: http://gyazo.com/4048802050e3dcfca7aee238acc2f7dd
Use a helper column, say, between the brown and the first blue or at the right of the setup. Use a vlookup like
=vlookup(brownvalue,BluetableRange,2,false)
Then do a countif on the helper column
=countif(HelperColumn,"<="&GreenCellAddress)
You can hide the column with the helper if it upsets your spreadsheet design.
You can (and probably should) use a helper column as Teylyn suggests. But, for when that may be inconvenient, you can also use an array formula:
=SUM(COUNTIFS(listlookupcolumn,rangeoflists,numbervaluecolumn,"<="&numbertomatch))
To enter it as an array formula, type "ctrl-shift-enter" after editing the formula, rather than just "enter"
Rough explanation: since rangeoflists is in a place where a single value is expected, the countifs is calculated once for each value, and the array of results is passed to sum. Use the "evaluate formula" feature to see the intermediate result array.
Afterthought: It occurs to me now that this does rely on listlookupcolumn containing unique values. (Almost certainly true in this example.) You can modify the formula a bit to get around this:
=SUM(SIGN(COUNTIFS(listlookupcolumn,rangeoflists,numbervaluecolumn,"<="&numbertomatch)))
The SIGN function will keep you from double counting.
Again, you must use "ctrl-shift-enter" for this to work. (Yes, as I'm sure others are ready to point out, you can also use the sumproduct hack in this instance.)

Create a custom function in Excel

This seems like such an obvious thing that excel must have this feature, I just can't find it.
How can I create a custom function without using VBA? (VBA is too big of a hammer and causes security warnings, etc).
For example, I have a spreadsheet with several very complex formulas. Each of these formulas are replicated in several columns. Each column has hundreds of entries, so each one is replicated hundreds of times. If I tweak something then I have to manually fill-down or copy my change from one column to another.
A simple one looks like this:
=(Payment1 - F$12)*12 + ($D21-H21)
But what I'd like to do is:
=MyFunction(f$12,$D21,H21)
And have the actual formula for "MyFunction" written just once someplace.
I've found a few things that come close to giving me what I want. For example, in tables Excel will automatically replicate changes in a formula down the rest of the column saving you the step of manually selecting the range and doing a "Fill Down".
It will also allow relative references off of named cells, which seems equivalent of a user-defined parameter-less functions.
if you can use text to create the formula, then you can define a name to evaluate the function.
In cell A2, create a name EvalAbove, and in Refers To, enter =evaluate(A1)
This way, you can construct a formula
e.g. B1 contains SUM, B2 contains =("="&B1&"(A2:A5)")
and in B3, you can then put =EvalAbove
This means that if you change the formula name in B1, then B2 will change to show the changed formula, and B3 will change to show the result.
Note that this still counts as a macro enabled workbook, but there's no VBA code, just named ranges
You can do this for the example you show if I interpret it correctly.
If not you may be able to rearrange things slightly to conform
your function has three parameters:
The first comes from row 12 of the current column
The second from column D of the current row
The third comes from the column two to the right of the current row
I assume Payment1 is a named variable already?
Set the cursor in say F21 and then define this name
MyFunction =(Payment1 - F$12)*12 + ($D21-H21)
This will set the parameters to come from the places shown
To understand this better switch to RC mode and type the formula as:
=(Payment1 - R12C)*12 + (RC4-RC[+2])
You can now propagate down the formula through the F coloumn
=MyFunction
and it will always use the values in the corresponding F12 column Dxx and column Hxx
If you drag the formula to the next column it will use G12, Dxx and Ixx
If you want to change the formula edit it in the define name space
This is a general exception to the rule that you cannot have non-vba UDFs in Excel. Often in Excel the things you want as 'arguments' to the function are actually in fixed places (rows or columns) that can be addressed relatively.
For example you often want to perform a udf on the cell to the left
So a udf giving the cuberoot of the cell to the left would be a named formula like this:
Cuberoot =(RC[-1])^(1/3)
Or in a1 form set the cursor in B1 and type =(A1)^(1/3)
And Excel will convert it internally to the RC form
For three args - use three columns
It works and does not suffer the volatility issue mentioned about evaluate()
Yes I know this is an old posting but it may help someone with the same issue.
Bob J.

Resources