VBA Autofilter Error Handling

Ruca13

Board Regular
Joined
Oct 13, 2016
Messages
85
Hi everyone,

I have a part of my code where I delete some rows based on information found with autofilters.

On the first filter, when it does not meet the conditions, the code just skips it as it should.

On the second filter, the error handling is not working.

VBA Code:
blankcolumn = Cells(1, 1).End(xlToRight).Offset(0, 1).Column
    
    Cells(2, blankcolumn).Select
    ActiveCell.FormulaR1C1 = _
        "=VLOOKUP(RC[-11],'Draft'!C[-11]:C[9],19,0)"
    Selection.AutoFill destination:=Range(Cells(2, blankcolumn), Cells(pendinglastrow, blankcolumn))
    Application.Calculation = xlCalculationAutomatic
    
    Range(Cells(1, 1), Cells(pendinglastrow, blankcolumn)).Select
    Selection.AutoFilter
    ActiveSheet.Range(Cells(1, 1), Cells(pendinglastrow, blankcolumn)).AutoFilter Field:=blankcolumn, Criteria1:="<>" & 0, _
        operator:=xlFilterValues
        
    On Error GoTo test:
    Range(Cells(2, blankcolumn), Cells(pendinglastrow, blankcolumn)).SpecialCells(xlCellTypeVisible).Select
    Selection.EntireRow.Delete

test:
    ActiveSheet.ShowAllData
    Columns(blankcolumn).ClearContents
    Cells(1, 1).Select
        
    pendinglastrow = Cells(1000000, 1).End(xlUp).Row

    Cells(2, blankcolumn).Select
    ActiveCell.FormulaR1C1 = _
        "=IF(VLOOKUP(RC[-11],'Draft'!C[-11]:C[4],14,0)="""",""x"",VLOOKUP(RC[-11],'Draft'!C[-11]:C[4],14,0))"
        Selection.AutoFill destination:=Range(Cells(2, blankcolumn), Cells(pendinglastrow, blankcolumn))
    Application.Calculation = xlCalculationAutomatic
    
    Range(Cells(1, 1), Cells(pendinglastrow, blankcolumn)).Select
    Selection.AutoFilter
    ActiveSheet.Range(Cells(1, 1), Cells(pendinglastrow, blankcolumn)).AutoFilter Field:=blankcolumn, Criteria1:="=0", _
        operator:=xlAnd
        
    On Error GoTo newerror:
    Range(Cells(2, blankcolumn), Cells(pendinglastrow, blankcolumn)).SpecialCells(xlCellTypeVisible).Select 'error here
    Selection.EntireRow.Delete

newerror:
    ActiveSheet.ShowAllData
    Columns(blankcolumn).ClearContents
    Cells(1, 1).Select

Can you please help me understand why the second autofilter does not handle the error?

Thank you.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
How about
VBA Code:
   blankcolumn = Cells(1, 1).End(xlToRight).Offset(0, 1).Column
   Application.Calculation = xlCalculationAutomatic
   
   With ActiveSheet
      .Range(.Cells(2, blankcolumn), .Cells(pendinglastrow, blankcolumn)).FormulaR1C1 = _
         "=VLOOKUP(RC[-11],'Draft'!C[-11]:C[9],19,0)"
      .Range("A1").AutoFilter blankcolumn, "<>0"
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
      .Columns(blankcolumn).ClearContents
      pendinglastrow = .Cells(Rows.Count, 1).End(xlUp).Row
      .Range(.Cells(2, blankcolumn), .Cells(pendinglastrow, blankcolumn)).FormulaR1C1 = _
         "=IF(VLOOKUP(RC[-11],'Draft'!C[-11]:C[4],14,0)="""",""x"",VLOOKUP(RC[-11],'Draft'!C[-11]:C[4],14,0))"
      .Range("A1").AutoFilter blankcolumn, "=0"
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
      .Columns(blankcolumn).ClearContents
   End With
   Cells(1, 1).Select
 
Upvote 0
Thank you Fluff,

Using offset like that avoids the error when no data was visible, and the overall code looks much more tidied up.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

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