VBA Autofilter causing error

JoeRooney

Board Regular
Joined
Nov 27, 2017
Messages
169
Office Version
  1. 365
Hi,

I have the below code and it currently causes an error when nothing is found in the filter, could someone advise me how to ignore this error and move straight to Call test_9 if nothing is found.

I think an "On Error GoTo" will solve this for me but I am not sure where to place this in my code.


VBA Code:
Dim R As Range, rng As Range
Set R = Range("A1").CurrentRegion
Set rng = R.Offset(1, 14).Resize(R.Rows.Count - 1)  'This is all the data w/o the headers


    Sheets("Test1").Select
    With ActiveSheet
    If .AutoFilterMode Then .AutoFilterMode = False
    .Range("A1:AK1").AutoFilter 14, Criteria1:=Array( _
        "Refund"), Operator:=xlFilterValues
    Set rng = rng.SpecialCells(xlCellTypeVisible)
    rng.Select
    rng.Copy
    End With
    
    Sheets("Test2").Select
    Range("A:A").Find("").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

    Call Test_9
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Are you trying to copy the visible cells for columns O:AK only?
 
Upvote 0
Hi Fluff, yes if "refund" is found , if it is not there I would like the code to skip to Call Test_9
 
Upvote 0
Ok, how about
VBA Code:
Sub JoeRooney()
   With Sheets("Test1")
      If .AutoFilterMode Then .AutoFilterMode = False
      .Range("A1:AK1").AutoFilter 14, "Refund"
      .AutoFilter.Range.Offset(1).Columns("O:AK").Copy
   End With
   
   Sheets("Test2").Range("A:A").Find("").PasteSpecial xlPasteValues
   Application.CutCopyMode = False
    Call Test_9
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
HI Fluff, opening this back up , your code works perfect but I would like to add to it , would it be possible to add in a condition that if nothing is found it skips the copy and paste and moves straight to "Call Test_9" ??
 
Upvote 0
If col N does not contain "refund" nothing is pasted, so not sure what you mean.
 
Upvote 0

Forum statistics

Threads
1,214,384
Messages
6,119,201
Members
448,874
Latest member
Lancelots

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