how to make code skip if is blank row?

jaik22

Board Regular
Joined
Sep 23, 2016
Messages
102
Code:
Sub Practicereport()
Dim markdata As String: markdata = Sheets("Main").Range("C1").Value
Dim lastrow As String: lastrow = Sheets("Data3").Range("A1000000").End(xlUp).Row
Sheets("Data3").Select
    Range("A1:B1").Select
    Selection.AutoFilter
ActiveSheet.Range("$A$1:$B$" & lastrow).AutoFilter Field:=2, Criteria1:= _
        "<" & markdata, Operator:=xlAnd
Range("B2:B" & lastrow).SpecialCells(xlCellTypeVisible).Select
On Error Resume Next
Selection = "NG"
ActiveSheet.ShowAllData

End Sub

The code is looking at the main page's value(markdata) and then filter with markdata on A and B columns in data3 sheet, then put NG in B column cells if markdata exist in A column.
This code works fine when I have data after filtering, but if I do not have data after filtering, it gives me an error.
Is there any way to skip code, if filter is blank?

Thank you!
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
So, if there is no data, shouldn't lastrow return 1?
If so, then just add this line after the lastrow calculation:
Code:
If lastrow=1 Then Exit Sub
 
Upvote 0
Thanks for the tip Joe4,

Is there any way to go next code, rather than exiting the sub?
 
Upvote 0
Sure. Just use an and IF... THEN block, and put all your filter code between it, i.e.
Code:
If lastrow > 1 Then
'   Filter code here
End If
Then if lastrow is equal to 1, it will just skip what is in that block and go to the next section of code.
 
Upvote 0
For a quick fix, relocate the error redirect like this:
Code:
Sub Practicereport()
Dim markdata As String: markdata = Sheets("Main").Range("C1").Value
Dim lastrow As String: lastrow = Sheets("Data3").Range("A1000000").End(xlUp).Row
Sheets("Data3").Select
    Range("A1:B1").Select
    Selection.AutoFilter
ActiveSheet.Range("$A$1:$B$" & lastrow).AutoFilter Field:=2, Criteria1:= _
        "<" & markdata, Operator:=xlAnd
On Error Resume Next
Range("B2:B" & lastrow).SpecialCells(xlCellTypeVisible).Select
If Err.Number = 0 Then
       Selection = "NG"
End If
On Error GoTo 0
ActiveSheet.ShowAllData
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,115
Messages
6,128,919
Members
449,478
Latest member
Davenil

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