如何添加已过滤参数不存在的消息

时间:2018-04-30 19:06:45

标签: excel excel-vba vba

我编写了以下函数来过滤工作表。工作正常,但我没有办法通知用户输入的参数是否不存在。在这个问题上,我正在寻求一些帮助。这是我的功能:

    Public Function filterSheets(Sheets As Worksheet, searchRange As String, operator As String, filterString As String)

'This function is used to filter sheets by deleting any rows
'that do not contain the value stated in variable filterString


Dim lngLastRow As Long

    Application.ScreenUpdating = False

    With Sheets

        lngLastRow = GETLASTROW(.Cells)

        If lngLastRow > 1 Then
            'we don't want to delete our header row
            With .Range(searchRange & lngLastRow)

                    .AutoFilter Field:=1, Criteria1:=operator & filterString


                    .EntireRow.delete
            End With
        End If
    End With

    Application.ScreenUpdating = True

End Function

以下是使用我的函数的过程:

Sub filterSheets

For Each ws In Sheets(Array("Return to Work Report", "New Leaves Report"))

filterRange = ("H2:H")
compOperator = "<>"
filterString = locationCode

    Call filterSheets(ws, filterRange, compOperator, filterString)

       Next ws

End Sub

谢谢

1 个答案:

答案 0 :(得分:2)

您可以检查现有行,因为您只是删除项目。

Dim lr as long, lra as long
lr = cells(rows.count,1).end(xlup).row

    'do all of your other code

lra = cells(rows.count,1).end(xlup).row
If lr=lra then msgbox("Your criteria can't be filtered.")