在范围内找不到号码时显示“重复号码”

If MoneyTextBox.Value = range("H:H").Find(MoneyTextBox.Value) Then MsgBox "Duplicate number" EndIf 

在这里输入图像说明

或者干脆这个…

 If Application.CountIf(Range("H:H"), MoneyTextBox.Value) > 0 Then MsgBox "Duplicate number" End If 

用这个代替:

 If Not IsError(Application.Match(CLng(MoneyTextBox.Value), Range("H:H"), 0)) Then MsgBox "Duplicate number" EndIf 

Find方法不会返回find的文本,而是find文本的第一个单元格,如果找不到则返回Nothing 。 此外,你的范围有数字(通过你的问题的标题),而你的表单可能有一个文本input框(问题不明确)。 你的情况每次都会因为这个而变成错误的。

相反,将文本input转换为数字,并使用Find方法没有find文本时返回Nothing的事实:

改变这个:

 If MoneyTextBox.Value = range("H:H").Find(MoneyTextBox.Value) Then 

至:

 If Not Range("H:H").Find(CLng(MoneyTextBox.Value)) Is Nothing Then