2017-07-08 39 views

回答

1

或者干脆这...

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

用这个代替未发现:

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

为什么'不是IsError'而不是直接'IsNumeric'? –

+0

@DirkReichel我在Excel公式中使用了紧凑形式'ISNUMBER',但是在VBA中尤其如此,我更喜欢更明确的形式。但两者都很好。 –

+0

但输出只能是数字或错误。这样'IsNumeric'并不比'Not IsError'更不明确:P –

2

Find method不返回找到的文本,但第一个小区的情况下的文字被发现,并且当它没有被发现时,它返回Nothing。此外,你的范围有数字(通过你的问题的标题),而你的表单可能有一个文本输入框(问题不明确)。由于这个原因,你的情况每次都是错误的。

相反,文本输入转换为数字,并用事实Find方法返回Nothing文本时未找到:

更改此:

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

到:

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