1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| Option Explicit
Const FormsList As String = "UserForm1;UserForm2;UserForm3;UserForm4;UserForm5;UserForm6"
Enum SensRecherche
SEARCH_FORWARD = 0
SEARCH_BACKWARD = 1
End Enum
Public Sub ShowNextForm(callerName As String)
Call SetVisible(callerName, False)
Call SetVisible(GetNextUserForm(callerName, SEARCH_FORWARD), True)
End Sub
Public Sub ShowPreviousForm(callerName As String)
Call SetVisible(callerName, False)
Call SetVisible(GetNextUserForm(callerName, SEARCH_BACKWARD), True)
End Sub
Public Sub ShowFirstForm(callerName As String)
Call SetVisible(callerName, False)
Call SetVisible(GetFirstUserForm(), True)
End Sub
Private Function GetFirstUserForm() As String
Dim uFormNames() As String
uFormNames = Split(FormsList, ";")
GetFirstUserForm = uFormNames(0)
End Function
Private Function GetNextUserForm(userFormName As String, sens As SensRecherche) As String
Dim uFormNames() As String
uFormNames = Split(FormsList, ";")
Dim i As Integer
For i = 0 To UBound(uFormNames)
If uFormNames(i) = userFormName Then
Exit For
End If
Next i
i = IIf(sens = SEARCH_FORWARD, i + 1, i - 1)
If i < 0 Then i = 0
If i > UBound(uFormNames) Then i = UBound(uFormNames)
GetNextUserForm = uFormNames(i)
End Function
Private Sub SetVisible(userFormName As String, visible As Boolean, Optional Modal As FormShowConstants = vbModeless)
Dim u As Object
Dim target As Object
For Each u In VBA.UserForms
If u.Name = userFormName Then
Set target = u
Exit For
End If
Next u
If target Is Nothing Then
VBA.UserForms.Add (userFormName)
Call SetVisible(userFormName, visible, Modal)
Else
If visible Then
target.Show (Modal)
Else
target.Hide
End If
End If
End Sub |
Partager