【问题标题】:Can't change Visible property in Label of a VBA UserForm无法更改 VBA 用户窗体标签中的可见属性
【发布时间】:2015-06-23 15:16:29
【问题描述】:

我有一个名为 UserForm1 的 Excel 表单,其标签具有以下属性:

此标签具有Visible=False 属性。我想在用户单击 CommandButton1 时使标签可见,为此我编写了下一个代码:

Private Sub CommandButton1_Click()
    Me.Label6.Visible = True
    Dim oficina_garsa, file_source, file_solds As String
    Dim invoice_year, invoice_month As Integer
    oficina_garsa = TextBox3.Value
    file_source = TextBox1.Value
    file_solds = TextBox2.Value
    invoice_year = CInt(ComboBox1.Value)
    invoice_month = ComboBox2.ListIndex
    Debug.Print oficina_garsa, file_source, file_solds, invoice_year, invoice_month
    Call MainProcess(oficina_garsa, file_source, file_solds, invoice_year, invoice_month)
End Sub

但它不起作用。所以我写了其他链接到 CommandButton2 的事件代码来测试这样的目的:

Private Sub CommandButton2_Click()
    If Me.Label6.Visible = False Then
        Me.Label6.Visible = True
    Else
        Me.Label6.Visible = False
    End If
    Application.Wait Now + TimeValue("00:00:03")
End Sub

最后一个代码可以正常工作,而第一个代码不行!但在第一个代码中,Me.Label6.Visible = True 之后的下一个代码运行完整而没有错误消息。

我尝试用“UserForm1”替换“Me”,但结果是一样的。为什么将 Visible 属性分配给 True 在 CommandButton2 事件中有效,但在 CommandButton1 事件中无效?

【问题讨论】:

  • 您在运行 CommandButton1 代码时是否真的收到错误消息?如果你不是,那么这个例程中会发生很多事情。您是否绝对确定标签可见属性没有在您调用的代码中的其他地方被重置为 false,或者用户表单正在被重置? IE。在您的 MainProcess 例程中。
  • Visible = true 之后的代码中是否有任何内容可能触发标签再次不可见?
  • @andshrew 和 DiscipleMichael 是的,在 Sub MainProcess 结束时有一个 Visible=False 需要 3 秒才能结束(调试检查),因此是测试中的延迟时间。

标签: forms vba excel visible


【解决方案1】:

我知道这个帖子很老了,但是当我遇到同样的问题时,我想我会发布修复。

设置可见参数后,添加一行

Me.Repaint

这将重新渲染表单并更新元素的可见性。我认为重绘命令特定于用户表单,因此不确定这是否适用于元素无法准确呈现的其他情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 2020-05-16
    • 2010-09-05
    • 2018-02-20
    相关资源
    最近更新 更多