end sub when next visible cell is empty. Compile error: Block If Without End If

zorcy3

New Member
Joined
May 14, 2013
Messages
2
<code>I am using a filter to remove unwanted clutter. I then need to fill in column G and H, but only if there is a usable row. I am having troubles with row just below the filters. If the first visible row below the filters is blank, it needs to exit the sub.
HTML:
Sub selectDirty()
Rows("1:1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$L$669").AutoFilter Field:=9, Criteria1:="=*Drty*" _
        , Operator:=xlAnd
    Dim myHRange As Range
   
    If Range("A2").CurrentRegion.SpecialCells(xlCellTypeVisible) = "" Then
    Exit Sub
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Name = "base"
    ActiveSheet.Range("base").Offset(0, 7).Select
    Selection.Value = "Failure"
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Name = "base"
    
    ActiveSheet.Range("base").Offset(0, 6).Select
    Selection.Value = "Equipment"
End Sub
</code><code> </code>
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I think you just need to add the line "End If" after "Exit Sub" like the following.

Code:
Sub selectDirty()
Rows("1:1").Select 
   Selection.AutoFilter 
   ActiveSheet.Range("$A$1:$L$669").AutoFilter Field:=9, Criteria1:="=*Drty*", Operator:=xlAnd 
   Dim myHRange As Range 
   If Range("A2").CurrentRegion.SpecialCells(xlCellTypeVisible) = "" Then 
   Exit Sub 
   End If 
   Range("A1").Select    
   Range(Selection, Selection.End(xlDown)).Name = "base" 
   ActiveSheet.Range("base").Offset(0, 7).Select    
   Selection.Value = "Failure" 
   Range("A1").Select    
   Range(Selection, Selection.End(xlDown)).Name = "base" 
   ActiveSheet.Range("base").Offset(0, 6).Select    
   Selection.Value = "Equipment"
End Sub
 
Last edited:
Upvote 0
Thanks bodhi808. That did resolve the error. I had tried to add it in, but put it before the End Sub. That obviously failed. Do you see any reason this would not exit the sub if the first visible cell after A2 is blank?
 
Upvote 0
I've not used the currentregion method really, but my understanding is that it represents an associated range to any data in your declared range.
Therefore, it is likely finding your entire list of data surrounding A2.
As this is a range of cells, you're unlikely to ever return a blank value in every cell, hence the IF statement never being true.

As I read your code, the range for the auto filter is hard coded as A1 to L669. Therefore, the next visible line after the autofilter is applied is always going to be row 670 if I understand correctly.
If this is the case, then perhaps replace the IF with:

Code:
If Application.CountA(Range("a670").EntireRow) = 0 Then
Exit Sub
End If
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,085
Messages
6,123,030
Members
449,092
Latest member
ikke

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