Excel 未找到值后的MsgBox
我有一个宏,它运行一个Excel 未找到值后的MsgBox,excel,vba,Excel,Vba,我有一个宏,它运行一个InputBox,用户在其中键入数字,然后excel在某个范围内找到一个包含该值的单元格。我想制作一个MsgBox,如果找不到给定的数字,它会发出警报,并再次运行InputBox,直到值在范围内 我有一点想法我该怎么做,但不能使发现本身的工作。我录制了一个宏,但不知道如果它找不到任何东西,如何正确设置它 到目前为止,我的代码是: Sub leftbutton() Start: n = InputBox("Podaj numer punktu z wykresu") 'gi
InputBox
,用户在其中键入数字,然后excel在某个范围内找到一个包含该值的单元格。我想制作一个MsgBox
,如果找不到给定的数字,它会发出警报,并再次运行InputBox
,直到值在范围内
我有一点想法我该怎么做,但不能使发现本身的工作。我录制了一个宏,但不知道如果它找不到任何东西,如何正确设置它
到目前为止,我的代码是:
Sub leftbutton()
Start: n = InputBox("Podaj numer punktu z wykresu") 'give me a point number from a chart
If n = "" Then Exit Sub
If n = "0" Then
MsgBox ("Liczba musi byc wieksza od zera") 'number must be > 0
GoTo Start
End If
If Not IsNumeric(n) Then
MsgBox ("Podaj liczbe!") 'give me a number!
GoTo Start
End If
Range("AS3:AS50000").Select
if
Selection.Find(What:=n, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase _
:=False, SearchFormat:=False).Activate 'now how to know if it finds value or not?
Then msgbox("Nie ma takiego punktu na wykresie") 'there's no such point on a chart
Goto Start
Else '~> select cell with the found value
End If
End Sub
下面的代码将在您指定的范围内进行检查,如果未找到任何内容,则返回错误,或者返回匹配的单元格行号
Dim FindResult As Object
n=... 'set whatever value you want to find
Range("AS3:AS50000").Select
Set FindResult = Selection.Find(n, LookIn:=xlFormulas, LookAt:=xlWhole)
If FindResult Is Nothing Then
MsgBox ("missing")
Else
MsgBox (FindResult.Row)
End If
很好用!非常感谢。不过,我和我的想法很接近,很好。