Return error if "Filter" doesn't return the required entry

skittlz

New Member
Joined
Oct 26, 2012
Messages
35
Office Version
  1. 365
Platform
  1. Windows
I currently have the sub-Macro below which filters for Columns A and B (for Strings Asset and SN), and then copies the value. What I'm trying to figure out is how do I set it so that if no results are found for either, the macro would generate an error instead of crashing/copying just the title headers.

VBA Code:
Sub filter()
    
    ws1.Activate

    ' Turn off AutoFilter if it's on
    If ws1.AutoFilterMode Then
        ws1.AutoFilterMode = False
    End If
    
    ' Turn on AutoFilter
    ws1.Range("A1").AutoFilter ' Assumes your data starts from A1; adjust as necessary

    ' Apply filters based on Asset and SN
    ws1.Range("A:A").AutoFilter Field:=1, Criteria1:=Asset
    ws1.Range("B:B").AutoFilter Field:=2, Criteria1:=SN

    ' Find the last non-empty row in column A
    ws1.Range("A" & Cells.Rows.Count).End(xlUp).Activate
    
    CalFreq = ActiveCell.Offset(0, 9)
    PM1Freq = ActiveCell.Offset(0, 12)
    PM2Freq = ActiveCell.Offset(0, 15)

End Sub

What I need to Macro to do is if Asset and/or SN or not found in the filter, than generate error "Asset# not found" or "SN# not found". How can I do that?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try:

VBA Code:
Sub filter()
    ws1.Activate

    ' Turn off AutoFilter if it's on
    If ws1.AutoFilterMode Then
      ws1.AutoFilterMode = False
    End If
    
    ' Turn on AutoFilter
    ws1.Range("A1").AutoFilter ' Assumes your data starts from A1; adjust as necessary

    ' Apply filters based on Asset and SN
    ws1.Range("A:A").AutoFilter Field:=1, Criteria1:=Asset
    If ws1.Range("A" & Rows.Count).End(3).Row = 1 Then
      MsgBox "Asset " & Asset & " not found"
      Exit Sub
    End If
    
    ws1.Range("B:B").AutoFilter Field:=2, Criteria1:=SN
    If ws1.Range("B" & Rows.Count).End(3).Row = 1 Then
      MsgBox "SN " & SN & " not found"
      Exit Sub
    End If

    ' Find the last non-empty row in column A
    ws1.Range("A" & Cells.Rows.Count).End(xlUp).Activate
    
    CalFreq = ActiveCell.Offset(0, 9)
    PM1Freq = ActiveCell.Offset(0, 12)
    PM2Freq = ActiveCell.Offset(0, 15)

End Sub

🤗
 
Upvote 0
Solution

Forum statistics

Threads
1,224,595
Messages
6,179,799
Members
452,943
Latest member
Newbie4296

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