【问题标题】:Excel VBA: Stop userform from reopeningExcel VBA:阻止用户窗体重新打开
【发布时间】:2019-01-06 15:56:32
【问题描述】:

我在工作表中有两个用户窗体,当分别从单元格 G5 的下拉列表中选择是或否时打开。 每次用户继续在工作表的其他位置(在 G5 以外的单元格中)输入数据时,用户窗体都会重新打开/重新出现。 有没有办法确保只在 G5 中的值发生变化时才打开用户窗体?

(Application.EnableEvents = True 需要打开,因为表单下方还有更多用户表单。)

提前感谢您的帮助!

这是我的代码:

Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = True
Set Target = Range("G5")
Application.EnableEvents = False

If Target = "No" Then

    Form1.Show

ElseIf Target = "Yes" Then

    From2.Show

End If

Application.EnableEvents = True

End Sub

【问题讨论】:

  • 如果 Target.Address = "$G$5" 那么 ...
  • 我不认为 Set Target = Range("G5") 做你认为它做的事情......你不是在测试范围是否是 G5,而是在更改 Target 引用的单元格。
  • 看来这确实是问题所在!谢谢! :)

标签: excel vba userform


【解决方案1】:

类似

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    Application.EnableEvents = False

    If Target.Address = "$G$5" Then

        If Target = "No" Then

            Form1.Show

        ElseIf Target = "Yes" Then

            From2.Show

        End If

    End If

    Application.EnableEvents = True

End Sub

【讨论】:

    猜你喜欢
    • 2013-09-06
    • 2019-01-11
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    相关资源
    最近更新 更多