Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default VBA question!?

Hi everyone,

I have this piece of vba code:

Option Explicit
Option Base 1

Dim Budget(1) As Single

Sub Sensitivity()
Dim cell As Range, Mult As Variant, ModelCounter As Integer, _
IncludeConstraint As Boolean
Application.ScreenUpdating = False
SaveOriginalValues
ModelCounter = 0
For Each cell In Range("Multiples")

ModelCounter = ModelCounter + 1
Mult = cell.Value
If IsNumeric(Mult) = True Then IncludeConstraint = True
ChangeModel Mult
RunSolver IncludeConstraint
StoreResults ModelCounter
Next
RestoreOriginalValues
End Sub

Sub SaveOriginalValues()
Dim i As Integer
For i = 1 To 1
Budget(i) = Range("Budget").Cells(i)
Next
End Sub

Sub ChangeModel(Mult As Variant)
Dim i As Integer
If IsNumeric(Mult) = True Then
For i = 1 To 1
Range("Budget").Cells(i) = Mult * Budget(i)
' MsgBox Mult * Budget(i)
Next
End If
End Sub

Sub RunSolver(IncludeConstraint As Boolean)
SolverReset
SolverOk SetCell:=Range("TotProj"), MaxMinVal:=1,
ByChange:=Range("Picked")
SolverAdd CellRef:=Range("Spending"), Relation:=1,
FormulaText:="Budget"

If IncludeConstraint = True Then _
SolverAdd CellRef:=Range("Picked"), Relation:=5
', FormulaText:="Budget"
SolverOptions AssumeLinear:=True, AssumeNonNeg:=True
SolverSolve UserFinish:=True
End Sub

Sub StoreResults(ModelCounter As Integer)
Dim i As Integer
With Range("G2")
For i = 1 To 30
.Offset(i, ModelCounter) = Range("Picked").Cells(i)

Next
' .Offset(i, ModelCounter) = Range("TotProj")
End With
End Sub

Sub RestoreOriginalValues()
Dim i As Integer
For i = 1 To 1
Range("Budget").Cells(i) = Budget(i)
Next
RunSolver True
End Sub


NOW, the results (either 1 or 0) should come out like this:

1 1 0 1
0 1 1 0
1 0 0 1

and so on. Instead I get things like thise:

1
0 1 0 1
1 1 1 0

and so on.

As you see in the first row, I get after the 1st value nothing but
blank. That means somehow my counter is NOT positioned correctly!?

Any help is appreciated....

Thanks,
Mike

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default VBA question!?

you don't give the start values nor do you describe what is is the code is
supposed to do. I don't understand why you'd create an array for one value,
nor a loop for i = 1 to 1 ? Doesn't make sense. I don't see any 2 dim
arrays, so Its pretty hard to see how you'd get any output at all.



"Mike" wrote:

Hi everyone,

I have this piece of vba code:

Option Explicit
Option Base 1

Dim Budget(1) As Single

Sub Sensitivity()
Dim cell As Range, Mult As Variant, ModelCounter As Integer, _
IncludeConstraint As Boolean
Application.ScreenUpdating = False
SaveOriginalValues
ModelCounter = 0
For Each cell In Range("Multiples")

ModelCounter = ModelCounter + 1
Mult = cell.Value
If IsNumeric(Mult) = True Then IncludeConstraint = True
ChangeModel Mult
RunSolver IncludeConstraint
StoreResults ModelCounter
Next
RestoreOriginalValues
End Sub

Sub SaveOriginalValues()
Dim i As Integer
For i = 1 To 1
Budget(i) = Range("Budget").Cells(i)
Next
End Sub

Sub ChangeModel(Mult As Variant)
Dim i As Integer
If IsNumeric(Mult) = True Then
For i = 1 To 1
Range("Budget").Cells(i) = Mult * Budget(i)
' MsgBox Mult * Budget(i)
Next
End If
End Sub

Sub RunSolver(IncludeConstraint As Boolean)
SolverReset
SolverOk SetCell:=Range("TotProj"), MaxMinVal:=1,
ByChange:=Range("Picked")
SolverAdd CellRef:=Range("Spending"), Relation:=1,
FormulaText:="Budget"

If IncludeConstraint = True Then _
SolverAdd CellRef:=Range("Picked"), Relation:=5
', FormulaText:="Budget"
SolverOptions AssumeLinear:=True, AssumeNonNeg:=True
SolverSolve UserFinish:=True
End Sub

Sub StoreResults(ModelCounter As Integer)
Dim i As Integer
With Range("G2")
For i = 1 To 30
.Offset(i, ModelCounter) = Range("Picked").Cells(i)

Next
' .Offset(i, ModelCounter) = Range("TotProj")
End With
End Sub

Sub RestoreOriginalValues()
Dim i As Integer
For i = 1 To 1
Range("Budget").Cells(i) = Budget(i)
Next
RunSolver True
End Sub


NOW, the results (either 1 or 0) should come out like this:

1 1 0 1
0 1 1 0
1 0 0 1

and so on. Instead I get things like thise:

1
0 1 0 1
1 1 1 0

and so on.

As you see in the first row, I get after the 1st value nothing but
blank. That means somehow my counter is NOT positioned correctly!?

Any help is appreciated....

Thanks,
Mike


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default VBA question!?

Pat,

This is a vba code for using excel optimizer. I agree it is confusing.
However, I removed the un-ncessary pieces. The 1st sub is the driver.
The "StoreResults" sub is the one that is writing results. The whole
code runs an excel optimizer known as "SOLVER" which tries to find x
values and f objective function value. It does several runs of course
and writes down x values of each run in one column. If you an email
address, I can email you the whole code to see?


Dim Budget(1) As Single

Sub Sensitivity()
Dim cell As Range, Mult As Variant, ModelCounter As Integer, _
IncludeConstraint As Boolean
Application.ScreenUpdating = False
SaveOriginalValues
ModelCounter = 0
For Each cell In Range("Multiples")

ModelCounter = ModelCounter + 1
Mult = cell.Value
If IsNumeric(Mult) = True Then IncludeConstraint = True
ChangeModel Mult
RunSolver IncludeConstraint
StoreResults ModelCounter
Next
RestoreOriginalValues
End Sub

Sub StoreResults(ModelCounter As Integer)
Dim i As Integer
With Range("G2")
For i = 1 To 30
.Offset(i, ModelCounter) = Range("Picked").Cells(i)


Next

End With
End Sub


Sub RestoreOriginalValues()
Dim i As Integer
For i = 1 To 1
Range("Budget").Cells(i) = Budget(i)
Next
RunSolver True
End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default VBA question!?

Pat,

Forgot to say that you can email me at

Then, I can email you the whole code with the application?

Thanks,
Mike

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
Newbie Question - Subtraction Formula Question [email protected] Excel Discussion (Misc queries) 3 May 5th 06 05:50 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good davegb Excel Programming 1 May 6th 05 06:35 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 0 April 27th 05 07:46 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 23 April 23rd 05 09:26 PM
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you [email protected] Excel Programming 0 April 22nd 05 03:30 PM


All times are GMT +1. The time now is 02:21 AM.

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

About Us

"It's about Microsoft Excel"