VBA_How 使用按钮在用户窗体之间传递不同的值
VBA_How to use buttons to deliver different values between UserForms
我在使用按钮在用户表单(UF2 和 UF3)之间传输值时遇到了问题。
我的目的是使用不同的按钮来改变userform3标签中的内容。例如,如果我单击 'aaa to 3',则会显示 userform3,标签中的文本将为 'aaa'.
这是用户表单 2:
代码在这里:
Public UF3 As UserForm3
Public Zone As String
Private Sub CommandButton2_Click()
Set UF3 = UserForm3
Zone = "aaa"
UF3.Show
Zone = "aaa"
End Sub
Private Sub CommandButton3_Click()
Set UF3 = UserForm3
Zone = "bbb"
UF3.Show
End Sub
还有用户窗体 3:
Public UF2 As UserForm2
Private Sub CommandButton1_Click()
Me.Hide
End Sub
Private Sub UserForm_Initialize()
Set UF2 = UserForm2
Label1 = UF2.Zone
End Sub
但是我运行它的时候,UF3里面的标签一直是空的
在您的代码中,Userform3
的 Initialize
事件将在您设置 Zone
之前被调用,就在您设置 UF3 时(如果 userform3 的默认实例尚未加载到其他地方)。
由于您没有使用关键字 new
来实例化新的用户窗体对象,它使用默认实例。
在您的特定情况下,您需要在设置 UF3 之前设置 Zone,以便在 Userform3 中调用 Initialize 时 Zone 将具有值,或者您可以在调用之前直接将 UF3.Label1 设置为正确的值 UF3.Show.
Private UF3 As UserForm5
Public Zone As String
Private Sub CommandButton1_Click()
'Either this possibility
Zone = "test"
Set UF3 = UserForm3
'or this one
UF3.Label1 = "aaa"
UF3.Show
End Sub
请注意,如果您使用用户窗体的默认实例,您甚至不需要设置它们,可以直接使用以下代码:
Private Sub CommandButton1_Click()
Userform3.Label1 = "aaa" 'If the Userform3 default instance doesn't already exists,
'the fist call to a method or a property will create and initialize it.
'So here the Initialize event of Userform3 will be called
'and then the value of Label1 will be changed.
Userform3.Show
End Sub
附加信息:请记住,Initialize 只会为用户窗体的每个实例调用一次,并且调用 Me.Hide
不会卸载用户窗体,只会 "unshow" 它。因此,在您的原始代码中,Userform3 的 label1 仅在您第一次调用 Set UF3 = Userform3
时设置了一次。如果您想更好地控制加载和卸载用户窗体实例的时间,您可能必须使用用户窗体的特定实例而不是默认实例。
我在使用按钮在用户表单(UF2 和 UF3)之间传输值时遇到了问题。
我的目的是使用不同的按钮来改变userform3标签中的内容。例如,如果我单击 'aaa to 3',则会显示 userform3,标签中的文本将为 'aaa'.
这是用户表单 2:
代码在这里:
Public UF3 As UserForm3
Public Zone As String
Private Sub CommandButton2_Click()
Set UF3 = UserForm3
Zone = "aaa"
UF3.Show
Zone = "aaa"
End Sub
Private Sub CommandButton3_Click()
Set UF3 = UserForm3
Zone = "bbb"
UF3.Show
End Sub
还有用户窗体 3:
Public UF2 As UserForm2
Private Sub CommandButton1_Click()
Me.Hide
End Sub
Private Sub UserForm_Initialize()
Set UF2 = UserForm2
Label1 = UF2.Zone
End Sub
但是我运行它的时候,UF3里面的标签一直是空的
在您的代码中,Userform3
的 Initialize
事件将在您设置 Zone
之前被调用,就在您设置 UF3 时(如果 userform3 的默认实例尚未加载到其他地方)。
由于您没有使用关键字 new
来实例化新的用户窗体对象,它使用默认实例。
在您的特定情况下,您需要在设置 UF3 之前设置 Zone,以便在 Userform3 中调用 Initialize 时 Zone 将具有值,或者您可以在调用之前直接将 UF3.Label1 设置为正确的值 UF3.Show.
Private UF3 As UserForm5
Public Zone As String
Private Sub CommandButton1_Click()
'Either this possibility
Zone = "test"
Set UF3 = UserForm3
'or this one
UF3.Label1 = "aaa"
UF3.Show
End Sub
请注意,如果您使用用户窗体的默认实例,您甚至不需要设置它们,可以直接使用以下代码:
Private Sub CommandButton1_Click()
Userform3.Label1 = "aaa" 'If the Userform3 default instance doesn't already exists,
'the fist call to a method or a property will create and initialize it.
'So here the Initialize event of Userform3 will be called
'and then the value of Label1 will be changed.
Userform3.Show
End Sub
附加信息:请记住,Initialize 只会为用户窗体的每个实例调用一次,并且调用 Me.Hide
不会卸载用户窗体,只会 "unshow" 它。因此,在您的原始代码中,Userform3 的 label1 仅在您第一次调用 Set UF3 = Userform3
时设置了一次。如果您想更好地控制加载和卸载用户窗体实例的时间,您可能必须使用用户窗体的特定实例而不是默认实例。