【问题标题】:How to bypass other subs on unload in a userform?如何绕过用户表单中卸载的其他潜艇?
【发布时间】:2019-07-31 03:30:12
【问题描述】:

这段代码检查用户表单文本框中的重复值并强制用户填写信息。效果很好!我唯一的问题是现在我无法在不触发事件的情况下卸载用户表单,如果我试图完全取消,那就是一个问题......(我有一些要启动......)

您对如何绕过或抑制这一点有任何想法吗?

    Duplicate check code
    Private Sub ItemName_exit(ByVal Cancel As MSForms.ReturnBoolean)  'checks for duplicate

    If Application.WorksheetFunction.CountIf(Worksheets(2).Range("B6:B505"), ItemName.Text) > 0 Then
    MsgBox ("Duplicate value, please change the name."), vbOKOnly, Title:="Duplicate"
       Cancel = True
        Exit Sub: End If

    End Sub

我尝试将事件抑制为布尔值,关闭显示警报无效...

有什么想法吗?

【问题讨论】:

    标签: excel vba events exit userform


    【解决方案1】:

    Daniel,使用 TextBox1_Change 事件会更好。此事件会在您键入时进行检查,并且 IF 语句中的“Exiting Sub”也不会关闭用户表单 - 当然,除非您想要它。您可以在设计模式下为您的 TextBox 添加一个 ControlTipText,然后确保将 ShowModal 属性更改为 False。下面的示例与您的示例不同,但实现了目标。

    代码示例:

    Option Explicit
    
    Private Sub TextBox1_Change()
    
    Dim ws As Worksheet
    Dim rng As Range
    Dim intDupes As Integer
    
    'set variables
    Set ws = ThisWorkbook.Worksheets("sheetname")
    Set rng = ws.Range("B6:B505")
    intDupes = Application.WorksheetFunction.CountIf(rng, TextBox1.Value)
    
    'changes color of textbox
    'also, you can add a ControlTipText text to the textbox
    'that informs the user what your message box did
    If intDupes > 0 Then
        TextBox1.BackColor = vbRed
    ElseIf intDupes = 0 Then
        TextBox1.BackColor = vbWhite
    End If
    
    'clean up
    Set cell = Nothing: Set ws = Nothing
    
    End Sub
    

    【讨论】:

    • 不完全是我的想法,但是在最后的传输步骤中将您的代码与一点点结合起来就可以了!谢谢!!!
    • 很高兴为您提供帮助!对我来说也很有趣。
    猜你喜欢
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 2012-12-01
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    相关资源
    最近更新 更多