It is difficult to identify what might be wrong with your existing code from the snippet you provide.
I have created two approaches that give the effect you seek although I suspect a mixture will give the most satisfactory solution.
I created a new workbook and within it I created six user forms: frmTest01
to frmTest06
. On each form I creates two command buttons named cmdBack
and cmdForward
. I saved the workbook and created two copies.
For version A, the switching between forms is handled by a macro outside the forms. The code for each user form is:
Option Explicit
Private Sub cmdBack_Click()
Direction = -1
Unload Me
End Sub
Private Sub cmdForward_Click()
Direction = 1
Unload Me
End Sub
That is, the two exits routines unload their form after setting a global variable Direction
to indicate whether the user wants to move back to the previous form or forward to the next. All the complexity is in the calling macro which is:
Option Explicit
Public Direction As Long
Sub CtrlForms()
Dim FormCrnt As Long
FormCrnt = 0
Do While True
Select Case FormCrnt
Case 0
FormCrnt = FormCrnt + 1
Load frmTest01
frmTest01.Show
Case 1
If Direction = 1 Then
FormCrnt = FormCrnt + 1
Load frmTest02
frmTest02.Show
Else
Exit Sub
End If
Case 2
If Direction = 1 Then
FormCrnt = FormCrnt + 1
Load frmTest03
frmTest03.Show
Else
FormCrnt = FormCrnt - 1
Load frmTest01
frmTest01.Show
End If
Case 3
If Direction = 1 Then
FormCrnt = FormCrnt + 1
Load frmTest04
frmTest04.Show
Else
FormCrnt = FormCrnt - 1
Load frmTest02
frmTest02.Show
End If
Case 4
If Direction = 1 Then
FormCrnt = FormCrnt + 1
Load frmTest05
frmTest05.Show
Else
FormCrnt = FormCrnt - 1
Load frmTest03
frmTest03.Show
End If
Case 5
If Direction = 1 Then
FormCrnt = FormCrnt + 1
Load frmTest06
frmTest06.Show
Else
FormCrnt = FormCrnt - 1
Load frmTest04
frmTest04.Show
End If
Case 6
If Direction = 1 Then
Exit Sub
Else
FormCrnt = FormCrnt - 1
Load frmTest05
frmTest05.Show
End If
End Select
Loop
End Sub
Direction -1 from frmTest01
and direction +1 from frmTest06
, results in an exit the macro. In all other situation, the appropriate user form is loaded and shown.
For approach B, the complexity is in the user forms. Here the calling macro is:
Option Explicit
Sub CtrlForms()
Load frmTest01
Load frmTest02
Load frmTest03
Load frmTest04
Load frmTest05
Load frmTest06
frmTest01.Show
Unload frmTest01
Unload frmTest02
Unload frmTest03
Unload frmTest04
Unload frmTest05
Unload frmTest06
End Sub
That is, this macro is only responsible for loading and unloading the forms and showing the first.
The code for each of the forms:
frmUser01
Option Explicit
Private Sub cmdBack_Click()
Me.Hide
End Sub
Private Sub cmdForward_Click()
Me.Hide
frmTest02.Show
End Sub
frmUser02
Option Explicit
Private Sub cmdBack_Click()
Me.Hide
frmTest01.Show
End Sub
Private Sub cmdForward_Click()
Me.Hide
frmTest03.Show
End Sub
frmUser03
Option Explicit
Private Sub cmdBack_Click()
Me.Hide
frmTest02.Show
End Sub
Private Sub cmdForward_Click()
Me.Hide
frmTest04.Show
End Sub
frmUser04
Option Explicit
Private Sub cmdBack_Click()
Me.Hide
frmTest03.Show
End Sub
Private Sub cmdForward_Click()
Me.Hide
frmTest05.Show
End Sub
frmUser05
Option Explicit
Private Sub cmdBack_Click()
Me.Hide
frmTest04.Show
End Sub
Private Sub cmdForward_Click()
Me.Hide
frmTest06.Show
End Sub
frmUser06
Option Explicit
Private Sub cmdBack_Click()
Me.Hide
frmTest05.Show
End Sub
Private Sub cmdForward_Click()
Me.Hide
End Sub
Back from frmTest01
and forward from frmTest06
hide their form and exit back to the controlling macro. All others hide their form and show the next form.
Both these approaches give the control you seek.
I suspect the best approach is to load all the forms at the beginning and unload all at the end with hide the old form and show the new rather than unload and load during processing. I prefer to localise flow control so I would use the Select
statement from approach A but without the loads.