私有字符串变量未显示在文本标签中 vba

Private String variable not showing in Text Label vba

我想将参数传递给用户表单。我尝试的解决方案是为我的用户表单(名称为 InformationMissing)设置一个自制的 属性,设置一个私有变量。 这是我的用户表单的代码

Private msMsg As String

Private Sub CommandButton1_Click()
    MsgBox msMsg
End Sub

Private Sub UserForm_Initialize()
    txtLabel1.Caption = "hello" & msMsg
End Sub

Property Let MyProp(sText As String)
    msMsg = sText
End Property

我正在使用另一个将 MyProp 属性 设置为字符串(在本例中为 "aa")的子表单:

Sub ShowFormProp()

    Dim myForm As InformationMissing
    Set myForm = New InformationMissing

    myForm.MyProp = "aa"
    myForm.Show

End Sub

我想在这里做的是在我的用户表单中打印 "helloaa",但我只得到 "hello"。但是,当我单击 CommandButton1 时,我得到 "aa"。 这意味着 属性 已正确设置但未传递给 txtLabel1。

你有什么想法吗?

提前致谢

如果您希望标题反映对私有字符串变量的更改,

UserForm.Initialize 不是放置 txtLabel1.Caption = "hello" & msMsg 的正确位置。 Initialize 将始终在您可以访问表单属性的 any 之前执行。

在您的情况下,一个好的解决方案是直接在 属性 过程中执行您想要的更改。

Property Let MyProp(sText As String)
    msMsg = sText
    ' You can also put the caption change in its own sub, if you want more things to happen when you change it
    txtLabel1.Caption = "hello" & msMsg
End Property

编辑:并反映@LatifaShi 在对您的问题的评论中所说的话:您那里有错字。 msMgsmsMsg 不是同一个变量。始终在每个模块的开头使用 Option Explicit 以防止出现此问题以及更多问题。