如何绕过用户窗体中卸载的其他潜艇?
How to bypass other subs on unload in a userform?
这段代码检查用户窗体文本框中的重复值并强制用户填写信息。效果很好!我唯一的问题是现在我无法在不触发事件的情况下卸载用户窗体,如果我试图完全取消那么这就是一个问题......(我有一些要启动......)
您对如何绕过或抑制它有什么想法吗?
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
我试过将事件抑制为布尔值,关闭显示警报无效...
有什么想法吗?
Daniel,使用 TextBox1_Change 活动会更好。此事件会在您键入时进行检查,并且 IF 语句中的 'Exiting Sub' 也不会关闭用户窗体 - 当然,除非您希望这样做。您可以在设计模式下为您的文本框添加一个 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
这段代码检查用户窗体文本框中的重复值并强制用户填写信息。效果很好!我唯一的问题是现在我无法在不触发事件的情况下卸载用户窗体,如果我试图完全取消那么这就是一个问题......(我有一些要启动......)
您对如何绕过或抑制它有什么想法吗?
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
我试过将事件抑制为布尔值,关闭显示警报无效...
有什么想法吗?
Daniel,使用 TextBox1_Change 活动会更好。此事件会在您键入时进行检查,并且 IF 语句中的 'Exiting Sub' 也不会关闭用户窗体 - 当然,除非您希望这样做。您可以在设计模式下为您的文本框添加一个 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