Excel 用户窗体 IF AND 语句
Excel Userform IF AND statement
我正在尝试在用户表单中创建一个 IF/AND 语句。
我想将相同的姓氏和不同的名字添加到动态列表中。
我的代码将其捕获为重复值。我可以添加完全唯一的名字和姓氏。
在我的代码中,我从 TextBox2/Num2 中提取名字,从 TextBox3/Num3 中提取姓氏。
例如:
将 John Doe 添加到已有 Jane Doe 的列表中将被视为姓氏的重复项。
我的工作表的当前状态
Dim Num1 As Long
Dim Num2 As Long
Dim Num3 As Long
On Error Resume Next
Num1 = Application.WorksheetFunction.Match(Me.TextBox1.Value, ws.Range("A2:A500000"), 0)
Num2 = Application.WorksheetFunction.Match(Me.TextBox2.Value, ws.Range("B2:B500000"), 0)
Num3 = Application.WorksheetFunction.Match(Me.TextBox3.Value, ws.Range("C2:C500000"), 0)
On Error GoTo 0
If Num1 > 0 Then
MsgBox "Error! Duplicate EID detected", , "Duplicate Detected"
Exit Sub
End If
If Num2 > 0 And Num3 > 0 Then
MsgBox "Error! Duplicate NAME detected", , "Duplicate Detected"
Exit Sub
End If
使用 WorksheetFunction.CountIfs
而不是 Match
并检查结果是否大于 0。这将搜索名字和姓氏的 组合 在同一行。
Dim firstName As String
firstName = Me.TextBox2.Value
Dim lastName As String
lastName = Me.TextBox3.Value
If WorksheetFunction.CountIfs(ws.Range("B:B"), firstName, ws.Range("C:C"), lastName) > 0 Then
MsgBox "Error! Duplicate NAME detected", , "Duplicate Detected"
Exit Sub
End If
我正在尝试在用户表单中创建一个 IF/AND 语句。
我想将相同的姓氏和不同的名字添加到动态列表中。
我的代码将其捕获为重复值。我可以添加完全唯一的名字和姓氏。
在我的代码中,我从 TextBox2/Num2 中提取名字,从 TextBox3/Num3 中提取姓氏。
例如:
将 John Doe 添加到已有 Jane Doe 的列表中将被视为姓氏的重复项。
我的工作表的当前状态
Dim Num1 As Long
Dim Num2 As Long
Dim Num3 As Long
On Error Resume Next
Num1 = Application.WorksheetFunction.Match(Me.TextBox1.Value, ws.Range("A2:A500000"), 0)
Num2 = Application.WorksheetFunction.Match(Me.TextBox2.Value, ws.Range("B2:B500000"), 0)
Num3 = Application.WorksheetFunction.Match(Me.TextBox3.Value, ws.Range("C2:C500000"), 0)
On Error GoTo 0
If Num1 > 0 Then
MsgBox "Error! Duplicate EID detected", , "Duplicate Detected"
Exit Sub
End If
If Num2 > 0 And Num3 > 0 Then
MsgBox "Error! Duplicate NAME detected", , "Duplicate Detected"
Exit Sub
End If
使用 WorksheetFunction.CountIfs
而不是 Match
并检查结果是否大于 0。这将搜索名字和姓氏的 组合 在同一行。
Dim firstName As String
firstName = Me.TextBox2.Value
Dim lastName As String
lastName = Me.TextBox3.Value
If WorksheetFunction.CountIfs(ws.Range("B:B"), firstName, ws.Range("C:C"), lastName) > 0 Then
MsgBox "Error! Duplicate NAME detected", , "Duplicate Detected"
Exit Sub
End If