×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

#### Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
To Your Posts
• Keyword Search
• One-Click Access To Your
Favorite Forums
• Automated Signatures
On Your Posts
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

# VBA Code for solver constraint not effective

 Forum Search FAQs Links MVPs

## VBA Code for solver constraint not effective

(OP)
Hi all, I'm trying to write a macro that will solve a couple hundred excel solver problems.  I've made the file smaller for your convenience.  The main problem is that when the macro is run, the constraint for a binary solution does not hold.  Has anyone seen a problem like this?
Replies continue below

### RE: VBA Code for solver constraint not effective

CardinalKid,

#### CODE

Sub test()

counter = 1:

For i = 1 To 1

For j = 2 To 53
Sheets("DATA").Cells(14, j).Value = Sheets("DATA").Cells(counter, j).Value
Next j

counter = counter + 1

Worksheets("data").Activate
SolverReset
SolverOptions , 0.001

'adding constraints

SolverAdd "$B$12:$AY$12", 1, "1"
SolverAdd "$B$12:$AY$12", 3, "0"

' executing solver
Application.Run "SolverOK", "$BC$14", 2, 0, "$B$12:$AY$12"
SolverAdd "$B$12:$AY$12", 5, "binary"
SolverAdd "$B$12:$AY$12", 4, "integer"
SolverSolve True

Next i

End Sub

two observations
1.  You do not have to use application.run just activate your worksheet prior to code. This may or not may speed up your code and does not really matter.

2.<-- Most Important  For some reason you have to list binary and interger below your solverok line.  Do not ask me why. But this seemed to work for me.(xl 2007)

Hope this helps

ck1999

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

### Reply To This Thread

#### Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!

Already a member? Close this window and log in.