Hey everybody

I am trying to do problem solving in VBA by the use of a loop. In the following example it is working quite well (optimization of C by the change in "a" an "b"). The problem occurs because I need a third variable, let's call it d. I try doing the same thing but adding "next d" after "next a" and "next b" (and of course also adding the variable earlier in the code). But apparently the loop function doesn't work when there are three variables - does anybody have an idea of how to fix this?

Sub Solver1()
'
' NRmethod Makro

Dim a As Integer
Dim b As Integer
Dim aBest As Double
Dim bBest As Double
Dim aMax As Integer
Dim bMax As Integer
Dim CBest As Double

'Define the upper limits of the two variables
aMin = 5
bMin = 5
bMax = 25

CBest = 0

Application.ScreenUpdating = False

'Define lower limits of the two variables
For b = bMin To bMax
    
For a = aMin To b - 1

'Relate the count variables with the worksheet
Range("c2").Value = a
Range("c3").Value = b
 
'If the solverfound solution is valid and larger than previously found solutions, save the variable and outcome values.
 If IsNumeric(Cells(21, 4)) Then 'And Range("D21").Value > DBest Then
    If Cells(21, 4).Value > CBest Then
    CBest = Range("d21").Value
    aBest = Range("c2").Value
    bBest = Range("c3").Value
    'MsgBox DBest & vbNewLine & iBest & vbNewLine & jBest
    End If
 End If
  
 
 Next a
 
 Next b
 
ans = MsgBox("Optimal løsning fundet: IR = " & CBest & " for x = " & aBest & " og y = " & bBest & vbNewLine _
& vbNewLine & "Skal de fundne værdier indsættes i arket?", vbYesNo)

If ans = vbNo Then
Exit Sub

ElseIf ans = vbYes Then
Range("c2").Value = aBest
Range("c3").Value = bBest
End If

Application.ScreenUpdating = True

End Sub