Msgbox无法在Excel VBA中运行

时间:2017-11-23 08:42:41

标签: excel vba excel-vba excel-2010

我想在excel表中根据输入框过滤数据。然后将数据复制到另一个工作表。我使用下面的代码。代码几乎正常工作。但是当没有可用数据时,我想展示一条消息。在我的代码message1和message2中都不起作用。

Sub Findrpnumber()
    Call Clearoldrp

    Dim str1 As Variant
    Dim Tbl As ListObject
    Dim FiltRng As Range
    Dim RngArea As Range

    Set Tbl = Sheet2.ListObjects("DataTable")
    str1 = Application.InputBox("Select the RP Number")

    If str1 = False Then
        MsgBox "Please Enter one RP number", , "Find RP Data"
        Exit Sub
    Else
        Tbl.Range.AutoFilter Field:=1, Criteria1:=str1
        For Each RngArea In Tbl.Range.SpecialCells(xlCellTypeVisible).Rows
            If RngArea.Row > 1 Then
                If Not FiltRng Is Nothing Then
                    Set FiltRng = Application.Union(FiltRng, RngArea)
                Else
                    Set FiltRng = RngArea
                End If
            End If
        Next RngArea

        If Not FiltRng Is Nothing Then 'Filter range is not empty
            FiltRng.Copy Sheets("Chart").Range("A1")
            MsgBox "Data available on Chart Sheet" 'Message1
        Else
            MsgBox "RP Number not found" 'Message2
        End If
    End If
    Sheet2.ListObjects("DataTable").Range.AutoFilter Field:=1
End Sub

0 个答案:

没有答案