The truth of the matter is that there is limited need for this. Provided a
userform is in memory all its controls are available to module code and they
tend to hold the information that you're interested in.
This is all artificial but, for example's sake, let's suppose you have a
series of buttons and you want to hide certain of them after they have been
clicked a certain number of times (in other words you want to impose a
maximum number of clicks) and then check (outside the userform) which ones
the user has actually clicked and how many times.
Your userform will have five buttons in total - three accumulator buttons
called, say, Button1, Button2 and Button3, and also buttons called ButtonOK
and a ButtonCancel. When it is shown click a number of times on each of the
three accumulator buttons and click OK or Cancel to finish.
You could code the Userform like this:
Public Button1HideCount As Integer
Public Button2HideCount As Integer
Public Button3HideCount As Integer
Public Button1ClickCount As Integer
Public Button2ClickCount As Integer
Public Button3ClickCount As Integer
Private Sub Button1_Click()
Button1ClickCount = Button1ClickCount + 1
If Button1ClickCount >= Button1HideCount Then _
Button1.Visible = False
End Sub
Private Sub Button2_Click()
Button2ClickCount = Button2ClickCount + 1
If Button2ClickCount >= Button2HideCount Then _
Button2.Visible = False
End Sub
Private Sub Button3_Click()
Button3ClickCount = Button3ClickCount + 1
If Button3ClickCount >= Button3HideCount Then _
Button3.Visible = False
End Sub
Private Sub ButtonOK_Click()
Hide
End Sub
Private Sub ButtonCancel_Click()
Button1ClickCount = 0
Button2ClickCount = 0
Button3ClickCount = 0
Hide
End Sub
And code a module like this:
Sub Example()
With UserForm1
' Set maxima - choose your own
.Button1HideCount = 1
.Button2HideCount = 3
.Button3HideCount = 22
' Initialize counters (to be safe)
.Button1ClickCount = 0
.Button2ClickCount = 0
.Button3ClickCount = 0
' Make sure all buttons show (again to be safe)
.Button1.Visible = True
.Button2.Visible = True
.Button3.Visible = True
' Show the form
.Show
' Grab the information from the form
Debug.Print "Button1 was pressed " & .Button1ClickCount & " times."
Debug.Print "Button2 was pressed " & .Button2ClickCount & " times."
Debug.Print "Button3 was pressed " & .Button3ClickCount & " times."
End With
' Destroy the form when finished
Unload UserForm1
End Sub
The controls and public variables are available inside the userform without
qualification, and outside it by qualification with the userform object
reference (in this example the default instance created on first reference).
All of the variables in the Userform relate to the form and are not relevant
in other processing (a given in this example) so are kept with and destroyed
with (each instance of) the form instead of cluttering up a namespace with
wider scope.