I have a script that repeats the same three lines of code at two different
places. The script works fine that way. However, if I put the aforementioned
three lines of code into a "sub", and replace the aforementioned two
instances of that code with calls to the sub, I get an error either at
compile time or at runtime. Can anybody give me a clue as to what might be
going on?
If it helps, this is the script:
Dim cell(3, 5) As Object
Dim storage As Object
Dim stackTop(3) As Integer
Private Sub CommandButton1_Click()
Call react(1)
End Sub
Private Sub CommandButton2_Click()
Call react(2)
End Sub
Private Sub CommandButton3_Click()
Call react(3)
End Sub
Private Sub UserForm_Initialize()
'''''''''''''''''''''''''''''''''
Set cell(1, 1) = TextBox1
Set cell(1, 2) = TextBox2
Set cell(1, 3) = TextBox3
Set cell(1, 4) = TextBox4
Set cell(1, 5) = TextBox5
Set cell(2, 1) = TextBox6
Set cell(2, 2) = TextBox7
Set cell(2, 3) = TextBox8
Set cell(2, 4) = TextBox9
Set cell(2, 5) = TextBox10
Set cell(3, 1) = TextBox11
Set cell(3, 2) = TextBox12
Set cell(3, 3) = TextBox13
Set cell(3, 4) = TextBox14
Set cell(3, 5) = TextBox15
Set storage = TextBox16
For i = 1 To 5
cell(1, i).Value = Val(i)
Next i
For j = 2 To 3
For i = 1 To 5
cell(j, i).Value = ""
Next i
Next j
storage.Value = ""
stackTop(1) = 1
stackTop(2) = 6
stackTop(3) = 6
''''''''''''''''''''
End Sub
Public Sub react(j As Integer)
''''''''''''''''''''''''''''''
i = stackTop(j)
stored = storage.Value
If stored = "" Then
If i < 6 Then
storage.Value = cell(j, i).Value
cell(j, i).Value = ""
stackTop(j) = stackTop(j) + 1
End If
ElseIf i = 6 Then
cell(j, i - 1).Value = stored
storage.Value = ""
stackTop(j) = stackTop(j) - 1
ElseIf Val(stored) < Val(cell(j, i).Value) Then
cell(j, i - 1).Value = stored
storage.Value = ""
stackTop(j) = stackTop(j) - 1
End If
''''''''''''''''''''''''''''''
End Sub
Public Sub codeblock(ByVal j As Integer, ByVal i As Integer)
cell(j, i - 1).Value = stored
storage.Value = ""
stackTop(j) = stackTop(j) - 1
End Sub
'NOTE: Replacing the repeated code in "react"
'with a call to "codeblock" results in an error
'at compile time.