简体   繁体   中英

Excel VBA Message Box when there are no blank fields in an autofilter and stop macro

I have a simple macro to set one AutoFilter column(field 4) to a particular value, and a second AutoFilter column(field 5) to show only blank entries. Sometimes there are no blank entries and in this situation is there a way to stop the filter function and provide a message box to notify no blank entries were found?

When my code ends currently, the column(field 5) where there are no blanks to filter actually looks like an autofilter has been applied looking at the change of dropdown arrow state. The full non-blank cells are visible still.

Sub PoPODRecvdStatus()
'
' PoPODRecvdStatus Macro
' Filters Courier/Status by POD Received and Consignment to Blank
'
Application.ScreenUpdating = False
'
ActiveSheet.Range("$A$1:$K$12543").AutoFilter Field:=4, Criteria1:= _
    "POD RECEIVED"
ActiveSheet.Range("$A$1:$K$12543").AutoFilter Field:=5, Criteria1:="<>"
Application.ScreenUpdating = True
End Sub

Based on the comment I understand you might want to have something like that. The code will display a message box if there is no blank cell when filtered on POD RECEIVED and display these lines otherwise it will display the "blanks" with POD RECEIVED

Sub PoPODRecvdStatus()
'
' PoPODRecvdStatus Macro
' Filters Courier/Status by POD Received and Consignment to Blank
'
    Application.ScreenUpdating = False
    '
    ActiveSheet.Range("$A$1:$K$12543").AutoFilter Field:=4, Criteria1:= _
                                                  "POD RECEIVED"
    ActiveSheet.Range("$A$1:$K$12543").AutoFilter Field:=5, Criteria1:="="

    Dim rng As Range
    Set rng = ActiveSheet.AutoFilter.Range
    If rng.Columns(5).SpecialCells(xlCellTypeVisible).Count - 1 = 0 Then
        MsgBox "No blank cells in column 5 for POD RECEIVED", vbOKOnly, "POD_RECEIVED"
        ActiveSheet.Range("$A$1:$K$12543").AutoFilter Field:=5, Criteria1:="<>"
    End If
    Application.ScreenUpdating = True

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM