【问题标题】:MsgBox after not finding a value找不到值后的 MsgBox
【发布时间】:2019-02-02 17:48:30
【问题描述】:

我有一个运行 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

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    下面的代码将检查您指定的范围,如果未找到任何内容或匹配的单元格的行号,则会给您错误。

    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
    

    【讨论】:

    • 完美运行!谢谢!不过,我的想法很接近,很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 2021-06-15
    • 1970-01-01
    • 2012-11-17
    相关资源
    最近更新 更多