# how to set a range in vba to be the the nonzero values in a column (skipping the zero cells)

#### breezer123

##### New Member
Hi all! Thank you for taking the time to read my question and potentially offer some help!

I am trying to use solver for an optimization problem. One of my constraints is that the cells in this range must be greater than or equal to an input cell value. However, I want the range to not include the cells that are zero, as this makes the constraint non feasible in solver. My range AN45:AN76 is changing as the solver is running. However, some of the values in the range are zero (or I can easily make them "" if this helps with the solution). The values that are zero are not always the same ones in the column, so I need to find a way to call the range to be all the cells except for the zero ones. My attempt is in this line: minrange = Range(isnot(IsEmpty("\$AN\$45:\$AN\$76"))). Thank you in advance for any advice!

VBA Code:
Sub solvermacro()

Application.ScreenUpdating = False
Application.ThisWorkbook.Sheets("Calculations").Activate

Dim minrange As Range
minrange = Range(isnot(IsEmpty("\$AN\$45:\$AN\$76")))

SolverOk SetCell:="\$AN\$79", MaxMinVal:=1, ValueOf:=0, ByChange:="\$J\$9:\$J\$39", _
Engine:=1, EngineDesc:="GRG Nonlinear"

SolverOptions Iterations:=10, Precision:=0.1, Convergence:= _
0.001, StepThru:=False, Scaling:=False, AssumeNonNeg:=True, Derivatives:=1
SolverOptions PopulationSize:=0, RandomSeed:=0, MutationRate:=0.075, Multistart:= _
False, RequireBounds:=False, MaxSubproblems:=0, MaxIntegerSols:=0, IntTolerance _
:=1, SolveWithout:=False, MaxTimeNoImp:=30

solversolve

End Sub

#### JLGWhiz

##### Well-known Member
I have never used Solver, so am not familiar with the alerts or error types thay are generated nor their remedies. I would have to do a lot of research to figure out what is causing the error and am not willing to put that much effort into it. Maybe someone more knowledgeable about Sover will jump in and assist.
Regards, JLG

### Excel Facts

Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

#### breezer123

##### New Member
I have never used Solver, so am not familiar with the alerts or error types thay are generated nor their remedies. I would have to do a lot of research to figure out what is causing the error and am not willing to put that much effort into it. Maybe someone more knowledgeable about Sover will jump in and assist.
Regards, JLG
No worries, thanks for your help!

#### JLGWhiz

##### Well-known Member
I did notice that you have minrange in quotation marks in your SolverAdd function. variables should be used without quotation marks, otherwise they become text and not the values expected.

#### JoeMo

##### MrExcel MVP
Thanks so much! Will this work with "" cells as well?
Depends,are there formulas in AN45:AN76? If so, can you post the formula?

Replies
1
Views
152
Replies
3
Views
600
Replies
1
Views
296
Replies
3
Views
2K
Replies
1
Views
270

1,127,567
Messages
5,625,550
Members
416,116
Latest member
Joemamasuka

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back