Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Solver iteration in VBA macro

The following VBA code runs Solver for one column (E) in a spreadsheet. I need to run it successivley for a series of columns. The row references do not change but the column references increment through F,G,H etc.

Can you show me how to program this? Thanks in advance.

Sub DAL()
'
' DAL Macro
' Macro recorded 4/18/2011 by local.admin
'


SolverOk SetCell:="$E$25", MaxMinVal:=3, ValueOf:="0", ByChange:= _
"$E$10,$E$14,$E$28,$E$29,$E$41"
SolverAdd CellRef:="$E$37", Relation:=2, FormulaText:="0"
SolverOk SetCell:="$E$25", MaxMinVal:=3, ValueOf:="0", ByChange:= _
"$E$10,$E$14,$E$28,$E$29,$E$41"
SolverAdd CellRef:="$E$38", Relation:=2, FormulaText:="0"
SolverOk SetCell:="$E$25", MaxMinVal:=3, ValueOf:="0", ByChange:= _
"$E$10,$E$14,$E$28,$E$29,$E$41"
SolverAdd CellRef:="$E$47", Relation:=2, FormulaText:="0"
SolverOk SetCell:="$E$48", MaxMinVal:=3, ValueOf:="0", ByChange:= _
"$E$10,$E$14,$E$28,$E$29,$E$41"
SolverAdd CellRef:="$E$48", Relation:=2, FormulaText:="0"
SolverOk SetCell:="$E$48", MaxMinVal:=3, ValueOf:="0", ByChange:= _
"$E$10,$E$14,$E$28,$E$29,$E$41"
SolverSolve

End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Solver iteration in VBA macro

One way...

Sub DAL2()
Dim sByChange As String, sCurCol As String
Dim sCRef1 As String, sCRef2 As String
Dim sCRef3 As String, sCRef4 As String
Dim sCSet1 As String, sCSet2 As String, vSz As Variant
Const sCol_List As String = "F,G,H,I,J,K,L,M,N" '//edit to suit

sByChange = "$E$10,$E$14,$E$28,$E$29,$E$41"
sCRef1 = "$E$37": sCRef2 = "$E$38"
sCRef3 = "$E$47": sCRef4 = "$E$48"
sCSet1 = "$E$25": sCSet2 = "$E$48"
sCurCol = "E"

For Each vSz In Split(sCol_List, ",")
SolverOk SetCell:=sCSet1, MaxMinVal:=3, ValueOf:="0", _
ByChange:=sByChange
SolverAdd CellRef:="$E$37", Relation:=2, FormulaText:="0"
SolverOk SetCell:=sCSet1, MaxMinVal:=3, ValueOf:="0", _
ByChange:=sByChange
SolverAdd CellRef:="$E$38", Relation:=2, FormulaText:="0"
SolverOk SetCell:=sCSet1, MaxMinVal:=3, ValueOf:="0", _
ByChange:=sByChange
SolverAdd CellRef:="$E$47", Relation:=2, FormulaText:="0"
SolverOk SetCell:=sCSet2, MaxMinVal:=3, ValueOf:="0", _
ByChange:=sByChange
SolverAdd CellRef:="$E$48", Relation:=2, FormulaText:="0"
SolverOk SetCell:=sCSet2, MaxMinVal:=3, ValueOf:="0", _
ByChange:=sByChange
SolverSolve

'Reset to next column
sByChange = Replace(sByChange, sCurCol, vSz)
sCSet1 = Replace(sCSet1, sCurCol, vSz)
sCSet2 = Replace(sCSet2, sCurCol, vSz)
sCRef1 = Replace(sCRef1, sCurCol, vSz)
sCRef2 = Replace(sCRef2, sCurCol, vSz)
sCRef3 = Replace(sCRef3, sCurCol, vSz)
sCRef4 = Replace(sCRef4, sCurCol, vSz)
sCurCol = vSz
Next
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Solver iteration in VBA macro

Oops! -Forgot to insert CellRef vars...

Sub DAL2()
Dim sByChange As String, sCurCol As String
Dim sCRef1 As String, sCRef2 As String, sCRef3 As String, sCRef4 As
String
Dim sCSet1 As String, sCSet2 As String, vSz As Variant
Const sCol_List As String = "F,G,H,I,J,K,L,M,N" '//edit to suit

sByChange = "$E$10,$E$14,$E$28,$E$29,$E$41"
sCRef1 = "$E$37": sCRef2 = "$E$38": sCRef3 = "$E$47": sCRef4 =
"$E$48"
sCSet1 = "$E$25": sCSet2 = "$E$48"
sCurCol = "E"

For Each vSz In Split(sCol_List, ",")
SolverOk SetCell:=sCSet1, MaxMinVal:=3, ValueOf:="0", _
ByChange:=sByChange
SolverAdd CellRef:=sCRef1, Relation:=2, FormulaText:="0"
SolverOk SetCell:=sCSet1, MaxMinVal:=3, ValueOf:="0", _
ByChange:=sByChange
SolverAdd CellRef:=sCRef2, Relation:=2, FormulaText:="0"
SolverOk SetCell:=sCSet1, MaxMinVal:=3, ValueOf:="0", _
ByChange:=sByChange
SolverAdd CellRef:=sCRef3, Relation:=2, FormulaText:="0"
SolverOk SetCell:=sCSet2, MaxMinVal:=3, ValueOf:="0", _
ByChange:=sByChange
SolverAdd CellRef:=sCRef4, Relation:=2, FormulaText:="0"
SolverOk SetCell:=sCSet2, MaxMinVal:=3, ValueOf:="0", _
ByChange:=sByChange
SolverSolve

'Reset to next column
sByChange = Replace(sByChange, sCurCol, vSz)
sCSet1 = Replace(sCSet1, sCurCol, vSz)
sCSet2 = Replace(sCSet2, sCurCol, vSz)
sCRef1 = Replace(sCRef1, sCurCol, vSz)
sCRef2 = Replace(sCRef2, sCurCol, vSz)
sCRef3 = Replace(sCRef3, sCurCol, vSz)
sCRef4 = Replace(sCRef4, sCurCol, vSz)
sCurCol = vSz
Next
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having Solver Call a macro for each iteration Andrew Harris Excel Discussion (Misc queries) 2 March 8th 10 04:27 AM
I want to use the solver in a macro. Can this be done? drjmb924 Excel Programming 1 November 5th 09 04:47 AM
How do I use Excel Solver (for an Iteration in a row) for several no.of times? Deepak[_2_] Excel Discussion (Misc queries) 0 December 9th 08 06:35 AM
Running a Macro at every Solver Iteration sk[_4_] Excel Programming 4 January 26th 07 12:46 AM
solver in macro sisco98 Excel Programming 5 June 2nd 05 01:27 PM


All times are GMT +1. The time now is 12:26 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"