Forms 用户输入数据后立即进行VBA数据验证
您好,我得到了这个代码,有一个功能可以检查用户是否要输入一个已经存在的发票号码。实际上,这个函数只在整个表单填写完毕并将存储在表中时才会出现,但我希望在用户输入数据后立即进行验证 这是我的实际代码:Forms 用户输入数据后立即进行VBA数据验证,forms,vba,validation,duplicates,Forms,Vba,Validation,Duplicates,您好,我得到了这个代码,有一个功能可以检查用户是否要输入一个已经存在的发票号码。实际上,这个函数只在整个表单填写完毕并将存储在表中时才会出现,但我希望在用户输入数据后立即进行验证 这是我的实际代码: Private Sub CommandButton1_Click() Dim L As Long Dim factureWs As Worksheet Dim rng As Range Dim thColor As XlThemeColor If MsgBox("Confirm?", vbYesN
Private Sub CommandButton1_Click()
Dim L As Long
Dim factureWs As Worksheet
Dim rng As Range
Dim thColor As XlThemeColor
If MsgBox("Confirm?", vbYesNo, "Confirming new invoice") = vbNo Then Exit Sub
Set factureWs = Worksheets("FACTURE") '<--| set the worksheet you want to work with
L = GetLastNonEmptyRow(factureWs, "D", 12) + 1 '<--| get passed worksheet first empty row after last non empty one in column "D" from row 12 (included)
If L > 0 Then If Not CheckDuplicate(Me.TextBox2, factureWs.Range("D12:D" & L - 1)) Then Exit Sub '<--| exit if duplicated non accepted by the user
FillRanges factureWs, L '<--| fill worksheet ranges with userfom controls values
With Me
If .OptionButton1 Then
FormatCell Range("B" & L), xlThemeColorAccent3
ElseIf .OptionButton2 Then
FormatCell Range("B" & L), xlThemeColorAccent1
ElseIf .OptionButton3 Then
FormatCell Range("B" & L), xlThemeColorAccent4
Else
FormatCell Range("B" & L), xlThemeColorAccent2
End If
End With
End Sub
谢谢你的帮助 您可以在userform模块中添加以下事件处理程序
Private Sub TextBox2_Change()
Dim L As Long
Dim factureWs As Worksheet
Set factureWs = Worksheets("FACTURE")
L = GetLastNonEmptyRow(factureWs, "D", 12) + 1 '<--| get passed worksheet first empty row after last non empty one in column "D" from row 12 (included)
If L <= 12 Then Exit Sub '<--| exit if no data in worksheet "FACTURE"
With Me.TextBox2
If Not CheckDuplicate(.Text, factureWs.Range("D12:D" & L - 1)) Then .Text = Left(.Text, Len(.Text) - 1) '<--| erase the last character that triggered the duplication issue
End With
End Sub
…您可以在userform模块中添加以下事件处理程序:
Private Sub TextBox2_Change()
Dim L As Long
Dim factureWs As Worksheet
Set factureWs = Worksheets("FACTURE")
L = GetLastNonEmptyRow(factureWs, "D", 12) + 1 '<--| get passed worksheet first empty row after last non empty one in column "D" from row 12 (included)
If L <= 12 Then Exit Sub '<--| exit if no data in worksheet "FACTURE"
With Me.TextBox2
If Not CheckDuplicate(.Text, factureWs.Range("D12:D" & L - 1)) Then .Text = Left(.Text, Len(.Text) - 1) '<--| erase the last character that triggered the duplication issue
End With
End Sub
…您应该在更新之前使用
TextBox2\u
或TextBox2\u退出
Change
事件在每次按键后更新。例如,如果您有一张#发票123,并且您试图输入一张新的#发票1234,则重复消息将错误显示
文本框2\u更新事件之前
TextBox2_退出事件
您应该在更新之前使用TextBox2\u
或TextBox2\u退出
Change
事件在每次按键后更新。例如,如果您有一张#发票123,并且您试图输入一张新的#发票1234,则重复消息将错误显示
文本框2\u更新事件之前
TextBox2_退出事件
当用户在factureWs.Range(“D12:D”&L-1))
中输入数据时,是否要查找重复项?在这种情况下,如果您的“制作”工作表发生工作表更改(ByVal目标为范围)
,您需要调用函数检查重复项。用户在制作范围(“D12:D”&L-1”)中输入数据时,是否要查找重复项
?在这种情况下,如果您的“制作”工作表发生工作表\u选择更改(以VAL目标为范围)
,您需要调用功能检查复制。
Option Explicit
Dim factureWs As Worksheet '<--| declare 'factureWs' at the userform level
Private Sub UserForm_Initialize()
Set factureWs = Worksheets("FACTURE") '<--| set 'factureWs' a the userform initializing
End Sub
...
Private Sub TextBox2_Change()
Dim L As Long
L = GetLastNonEmptyRow(factureWs, "D", 12) + 1 '<--| get passed worksheet first empty row after last non empty one in column "D" from row 12 (included)
If L <= 12 Then Exit Sub '<--| exit if no data in worksheet "FACTURE"
With Me.TextBox2
If Not CheckDuplicate(.Text, factureWs.Range("D12:D" & L - 1)) Then .Text = Left(.Text, Len(.Text) - 1) '<--| erase the last character that triggered the duplication issue
End With
End Sub
Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Const msg = "This invoice number already exist!" & vbCrLf & vbCrLf & "Continue?"
With Worksheets("FACTURE")
If Not .Range("D12", .Range("D" & .Rows.Count).End(xlUp)).Find(what:=Me.TextBox2, LookIn:=xlValues, lookat:=xlWhole) Is Nothing Then
If Not MsgBox(msg, vbExclamation + vbYesNo, "Duplicate alert") = vbYes Then
Me.TextBox2 = ""
End If
End If
End With
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Const msg = "This invoice number already exist!" & vbCrLf & vbCrLf & "Continue?"
With Worksheets("FACTURE")
If Not .Range("D12", .Range("D" & .Rows.Count).End(xlUp)).Find(what:=Me.TextBox2, LookIn:=xlValues, lookat:=xlWhole) Is Nothing Then
If Not MsgBox(msg, vbExclamation + vbYesNo, "Duplicate alert") = vbYes Then
Cancel = True
End If
End If
End With
End Sub