VBA - Message Box is Criteria not available

KatKitKat

New Member
Joined
Apr 27, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hi.

I need some help on if something can be done. I have a spreadsheet with many rows of data. I added a macro that users can select 2 criteria to filter the list by selecting choices from drop down fields and "clicking" a button. This works fine. My problem is if there is no data in the listing based on the selected option combination. When this happens, the report does not filer. I would assume if there is no data in the list, the report would show nothing but instead it just doesn't filter and the user does not know that there is entries that match the requested filter.

Can I add a command that if the selected combination is not available, a message box would pop up saying "There are no entries that match this criteria. Try Again"? Below is the beginning and end of my existing code (several lines long so I will spare all the coding).

Thanks for any help offered.
Kat

VBA Code:
Sub Button24_Click() 'filter Use Log by SOF and Status
    ActiveSheet.Unprotect Password:="1234"
    Dim list1 As String, list2 As String
    
    list1 = Range("D2")
    list2 = Range("E2")
        
    If list1 = "Construction Contingency" And list2 = "USED" Then
        With Worksheets("UseLog").Range("A11:J1010")
        .AutoFilter Field:=5, Criteria1:="Construction Contingency"
        .AutoFilter Field:=6, Criteria1:="Closed"
        Range("A7") = Sheets("ExposureLog").Range("K2:K2") & (" - ") & Range("D2") 'set for all transactions
        Range("I6") = Sheets("Table").Range("E68") 'set for construction contingency
        Range("I7") = Sheets("Table").Range("E69") 'set for construction contingency
        Range("I8") = Sheets("Table").Range("E70") 'set for construction contingency
        Range("A9") = Range("E2") & (" ") & ("CONTINGENCY TRANSACTIONS") 'set for all transactions - may need IF statement to add note that pending means open
        Range("G1993") = Range("E2") & (" ") & ("Total:")
        End With
        ElseIf list1 = "Construction Contingency" And list2 = "PENDING" Then

Code contines on with all possible combinations and ends as such:

VBA Code:
  End With 'END OF PROJECT CONTINGENCY
        End If
    ActiveSheet.Protect Password:="1234"
End Sub
 
That did it! Whew! I am so grateful for your help. Have the best day!
Thank you for the feedback
Don't miss @Joe4 message on using Option Explicit; in our case it is not sufficient to declare the variable, it needs to be declared "as Range" for the algorithm to work properly
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Forum statistics

Threads
1,224,814
Messages
6,181,121
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top