Option Explicit
Option Compare Text
Public Sub ShowUserFormByName(FormName As String)
On Error Goto err
Dim i%
' Load the Userform
UserForms.Add FormName
'Go through all loaded Userforms
For i = 0 To UserForms.Count - 1
'Find the one with the matching name
If UserForms(i).Name = FormName Then
'Show that
UserForms(i).Show
Exit For
End If
Next i
Exit Sub
err:
Select Case err.Number
Case 424:
MsgBox "The Userform with the name " & FormName & " was not found.", vbExclamation, "Unknown form"
Case Else:
MsgBox err.Description, vbCritical, "A strange error: " & err.Number
End Select
End Sub
|