What does the Userform.Tag do?

  • Thread starter Thread starter Brad E.
  • Start date Start date
Hi,

The help files says,

Stores additional information about an object.

Syntax

object.Tag [= String]

The Tag property syntax has these parts:

Part Description
object Required. A valid object.
String Optional. A string expression identifying the object. The default
is a zero-length string ("").


Remarks

Use the Tag property to assign an identification string to an object
without affecting other property settings or attributes.

For example, you can use Tag to check the identity of a form or control
that is passed as a variable to a procedure.

Cheers
Andy
 
The Tag property is String that can be used for anything you wish to help
identify any control. Most controls have them. To me it's kinda used as a
built in custom univerisal property.

In your case for example, lets say you have 3 userforms. Userfrom1 and
Userform2 have the Tag property set to "Run Code". Userform3 has nothing as
a Tag. Now lets say you want to run a procedure for all userforms with Tag =
"Run Code". You would put this in the Initialize Event of each userform.

Private Sub UserForm_Initialize()
If Me.Tag = "Run Code" Then Call MySub
End Sub

Hope this helps! If so, let me know, click "YES" below.
 
I've seen some code where it's used as (essentially) a variable to pass
information back to the routine that loaded and showed the userform.

For instance, I created a userform with two commandbuttons and a single
checkbox.

This was the code behind the userform:

Option Explicit
Private Sub CommandButton1_Click()
Me.Tag = "cancel"
Me.Hide
End Sub
Private Sub CommandButton2_Click()
Me.Tag = "ok"
Me.Hide
End Sub
Private Sub UserForm_Initialize()
Me.CommandButton1.Caption = "Cancel"
Me.CommandButton2.Caption = "ok"
End Sub

Then I loaded the userform (and read the results) in this code in a General
module:

Option Explicit
Sub testme()

Dim UF1 As UserForm1

'load the userform
Set UF1 = New UserForm1

UF1.Show

If UF1.Tag = "ok" Then
MsgBox UF1.CheckBox1.Value
Else
MsgBox "User hit cancel, don't trust the checkbox!"
End If

Unload UF1

End Sub

But you could use it to pass any other info to the userform, too--maybe a string
that initializes a textbox????
 
Back
Top