@@ -12,7 +12,7 @@ Attribute VB_Description = "A class to set/manage Solver constraints."
1212'@Exposed
1313'@folder("SolverWrapper.Source")
1414' ==========================================================================
15- ' SolverWrapper v0.6
15+ ' SolverWrapper v0.7
1616'
1717' A wrapper for automating MS Excel's Solver Add-in
1818'
@@ -65,7 +65,7 @@ Attribute Add.VB_Description = "Adds a constraint to the current problem."
6565 End If
6666
6767 Dim ws As Worksheet
68- Dim solver_num As Integer
68+ Dim solver_num As Long
6969
7070 Set ws = oSolverSheet
7171
@@ -88,7 +88,6 @@ Attribute Add.VB_Description = "Adds a constraint to the current problem."
8888 ws.Names.Add Name:="solver_rhs" & CStr(solver_num), RefersTo:="=""AllDifferent""" , Visible:=nameVisible
8989 End Select
9090 Else 'Relation <=3
91- If Left$(formulaText, 1 ) <> "=" Then formulaText = "=" & formulaText
9291 ws.Names.Add Name:="solver_lhs" & CStr(solver_num), RefersTo:="=" & ws.Range(cellRef).Address, Visible:=nameVisible
9392 ws.Names.Add Name:="solver_rel" & CStr(solver_num), RefersTo:=relation, Visible:=nameVisible
9493 ws.Names.Add Name:="solver_rhs" & CStr(solver_num), RefersTo:=formulaText, Visible:=nameVisible
@@ -114,10 +113,10 @@ Attribute Count.VB_Description = "Gets the total number of constraints set for t
114113End Property
115114
116115'%Description("Deletes a constraint from the current problem.")
117- Public Sub Relax (ByVal cellRef As Variant , ByVal relation As SlvRelation , Optional formulaText As Variant )
116+ Public Sub Relax (ByVal cellRef As Variant , ByVal relation As SlvRelation , Optional ByVal formulaText As Variant )
118117Attribute Relax.VB_Description = "Deletes a constraint from the current problem."
119118 Dim ws As Worksheet
120- Dim solver_num As Integer
119+ Dim solver_num As Long
121120 Dim saveScreenUpdating As Long
122121 Dim savConstraints As New Collection
123122 Dim i As Long
@@ -222,7 +221,7 @@ End Sub
222221Public Sub Change (ByVal cellRef As Variant , ByVal relation As SlvRelation , ByVal newFormulaText As Variant )
223222Attribute Change.VB_Description = "Changes a constraint of the current problem. If cellRef AND relation do not match an existing constraint, then use Relax and Add instead."
224223 Dim ws As Worksheet
225- Dim solver_num As Integer
224+ Dim solver_num As Long
226225 Dim matchCount As Long
227226 Dim matchNum As Long
228227 Dim i As Long
@@ -275,7 +274,7 @@ End Function
275274' Private members
276275' ==========================================================================
277276
278- Private Sub ProcessConstraintParams (ByRef cellRef As Variant , ByVal relation As Integer , Optional ByRef formulaText As Variant )
277+ Private Sub ProcessConstraintParams (ByRef cellRef As Variant , ByVal relation As Long , Optional ByRef formulaText As Variant )
279278 Dim ws As Worksheet
280279
281280 Set ws = oSolverSheet
@@ -292,26 +291,25 @@ Private Sub ProcessConstraintParams(ByRef cellRef As Variant, ByVal relation As
292291 Err.Raise vbObjectError + 4001 , , "Please specify a valid cell reference"
293292 End If
294293
295- 'could do this check in SolveIt right before calling dll - that way use can do things in any order
294+ 'could do this check in SolveIt right before calling dll - that way user can do things in any order
296295 'check that lhs refer(s) to the decision variables
297296 If relation > 3 Then
298297 If Not NameExists("solver_adj" , ws) Then
299298 Err.Raise vbObjectError + 4001 , , "Cannot set constraint relations slvInt, slvBin, or slvAllDif, until after the decision variables are defined."
300299 End If
301300 If Not IsRangeInRange(ws.Range(cellRef), ws.Range(ws.Names("solver_adj" ).Name)) Then
302- 'they are not the same
301+ 'the int, bin, and alldifferent constraints can only be applied to decision vars
303302 Err.Raise vbObjectError + 4001 , , "The selected Relation only applies to contraints on decision variables."
304303 End If
305304 End If
306305
307- 'the int, bin, and alldifferent constraints can only be applied to decision vars
308-
309306 If relation <= 3 Then
310307 'process formula text (RHS)
311308 If IsMissing(formulaText) Or IsError(formulaText) Then Err.Raise vbObjectError + 4001 , , "Please specify a valid formula text"
309+
312310 formulaText = ProcessFormulaText(formulaText)
313311
314- If IsCellReference (formulaText) Then
312+ If CellRefHasOneArea (formulaText, ws ) Then
315313 'cell reference consists of a single contiguous cell block
316314 If ws.Range(formulaText).Count > 1 Then
317315 'multiple cells, so must match count of cellRef
@@ -321,27 +319,27 @@ Private Sub ProcessConstraintParams(ByRef cellRef As Variant, ByVal relation As
321319 End If
322320 Else
323321 'then to be valid, must be either numeric or a formula text that evaluates to numeric
322+ 'Evaluate will take a formula like "=$o2" and convert it to the actual value of cell $o2
324323 If Not IsNumeric(Application.Evaluate(formulaText)) Then
325324 Err.Raise vbObjectError + 4001 , , "The formula text does not evaluate to a valid formula."
326325 End If
327326 End If
328327 End If
329328End Sub
330329
331- Private Function ProcessFormulaText (formulaText As Variant ) As String
330+ Private Function ProcessFormulaText (ByVal formulaText As Variant ) As String
332331 Dim tmp As String
333332 Select Case TypeName(formulaText)
334333 Case "String"
335334 tmp = Application.ConvertFormula(formulaText, Application.ReferenceStyle, Application.ReferenceStyle, True )
336335 If Application.ReferenceStyle = xlR1C1 Then tmp = Application.ConvertFormula(tmp, xlR1C1, xlA1)
337336 If Application.International(xlDecimalSeparator) <> "." Then tmp = Replace(tmp, Application.International(xlDecimalSeparator), "." )
338- 'convert to absolute formula
339337 Case "Range"
340338 tmp = formulaText.Address
341339 Case Else
342340 tmp = formulaText
343341 End Select
344- ' If Left$(tmp, 1) <> "=" Then tmp = "=" & tmp
342+ If Left$(tmp, 1 ) <> "=" Then tmp = "=" & tmp
345343 ProcessFormulaText = tmp
346344End Function
347345
0 commit comments