Not sure which textbox maps to which variable.
change the button click
Private Sub CommandButton1_Click() 'excecute
Dim w, h
w = Me.Controls("width")
h = Me.Controls("height")
If Not (IsNumeric(w) And IsNumeric(h)) Then
MsgBox "height or width is not valid", vbExclamation + vbOKOnly
Exit Sub
End If
NUM_MOLECULES = w
LIMIT_HIGH = h
Unload Me
Finala
End Sub
and change these in the module.
Dim dblMyCount As Double
Public NUM_MOLECULES As Byte
Public LIMIT_HIGH As Byte
Public LIMIT_LOW As Byte
Dim molecules() As molecule
Sub Finala() 'main sub
LIMIT_LOW = 1
If NUM_MOLECULES = 0 Or LIMIT_HIGH = 0 Then
MsgBox "Limits out of bounds", vbExclamation + vbOKOnly
Exit Sub
End If
running = Not running '?????????????????????????????????????????
If Not running Then Exit Sub
Dim i As Integer
resetall
Do Until AllMatched Or NoMatches
Update
If Not running Then Exit Sub
Application.Wait Now() + TimeValue("00:00:01")
DoEvents
If dblMyCount = ((NUM_MOLECULES / 100) * 80) Then Exit Sub
Loop
Cells(8, 12) = "no more matches"
running = False
End Sub
Sub Update()
Dim i As Integer, j As Integer, m As Byte
For i = LBound(molecules, 1) To UBound(molecules, 1)
For j = LBound(molecules, 2) To UBound(molecules, 2)
If Not molecules(i, j).matched Then
m = 1
Select Case True
Case cycle = 0
'initialise
molecules(i, j).val = Application.WorksheetFunction.RandBetween(LIMIT_LOW, LIMIT_HIGH)
molecules(i, j).mvdir = CBool(Application.WorksheetFunction.RandBetween(0, 1))
Case Not molecules(i, j).mvdir And molecules(i, j).val > LIMIT_LOW
'moving down
molecules(i, j).val = molecules(i, j).val + -m
Case molecules(i, j).mvdir And molecules(i, j).val < LIMIT_HIGH
'moving up
molecules(i, j).val = molecules(i, j).val + m
Case Not molecules(i, j).mvdir And molecules(i, j).val <= LIMIT_LOW
'hit low, start moving up
molecules(i, j).val = LIMIT_LOW + m
molecules(i, j).mvdir = Not molecules(i, j).mvdir
Case molecules(i, j).mvdir And molecules(i, j).val >= LIMIT_HIGH
'hit high, start moving down
molecules(i, j).val = LIMIT_HIGH + -m
molecules(i, j).mvdir = Not molecules(i, j).mvdir
End Select
Cells(i + 1, j).Formula = molecules(i, j).val
End If
Next
DoEvents
Next
SetCell2
If cycle Then Cells(8, 12) = "cycle: " & cycle Else Cells(8, 12) = "initialising..."
Cells(8 + cycle, 3).Value = cycle ' chart cycle
Dim rngMyRange As Range, rngMyCell As Range
Set rngMyRange = Range("A2:j2") 'Range to count cells coloured with vbBlue.
dblMyCount = 0
For Each rngMyCell In rngMyRange
If rngMyCell.Interior.color = vbBlue Then dblMyCount = dblMyCount + 1
Next
Set rngMyRange = Nothing
Cells(8 + cycle, 4).Value = NUM_MOLECULES - dblMyCount
Cells(8 + cycle, 5).Value = (LIMIT_HIGH * LIMIT_HIGH) / (Cells(8 + cycle, 4).Value)
cycle = cycle + 1
End Sub
Sub resetall()
Range("A2:j3").Interior.Pattern = xlNone
dblMyCount = 0
NoMatchCount = 0
ReDim molecules(1 To 2, 1 To NUM_MOLECULES)
Clear
Square_Cell
Table
cycle = 0
Dim i As Integer
For i = 1 To NUM_MOLECULES
Cells(1, i).Value = i
Next
End Sub