Excel VBA to check autofilter for data

cav719 picture cav719 · Oct 1, 2012 · Viewed 39k times · Source

I need help checking for autofiltered rows not including the header. I want it to give a message box "No records found." then exit sub or continue with copy paste if there are rows beyond the header row. I know I need an If/Else entry after the filter to check for data but I'm having trouble figuring how to check. This code is being done from a UserForm button I created.

Here is my script:

Private Sub Searchbycompanyfield_Click()

If CompanyComboBox1.Value = "" Then
    MsgBox "Please enter a Company to begin search."
    Exit Sub
End If
ActiveSheet.Range("$A:$H").AutoFilter Field:=1, Criteria1:=EQDataEntry.CompanyComboBox1.Value, Operator:=xlOr
Cells.Select
Selection.Copy
Sheets("Sheet2").Select
Range("A5").Select
ActiveSheet.Paste
Call MessageBoxYesOrNoMsgBox
End Sub

Any help would be greatly appreciated.

Answer

nutsch picture nutsch · Oct 2, 2012

count the lines, or check if the last row is the header

if application.worksheetfunction.subtotal(3,activesheet.columns(1))>1 then 
    msgbox "Records"
else
    msgbox "No Records"
end if

check the last row

if activesheet.cells(rows.count,1).end(xlup).row>1 then 
    msgbox "Records"
else
    msgbox "No Records"
end if