VBA Autofilter causing error

JoeRooney

Board Regular
Joined
Nov 27, 2017
Messages
116
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
 

Some videos you may like

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
48,069
Office Version
  1. 365
Platform
  1. Windows
Are you trying to copy the visible cells for columns O:AK only?
 

JoeRooney

Board Regular
Joined
Nov 27, 2017
Messages
116
Hi Fluff, yes if "refund" is found , if it is not there I would like the code to skip to Call Test_9
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
48,069
Office Version
  1. 365
Platform
  1. Windows
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
 

JoeRooney

Board Regular
Joined
Nov 27, 2017
Messages
116

ADVERTISEMENT

Thank you fluff , works perfect.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
48,069
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback.
 

JoeRooney

Board Regular
Joined
Nov 27, 2017
Messages
116

ADVERTISEMENT

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" ??
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
48,069
Office Version
  1. 365
Platform
  1. Windows
If col N does not contain "refund" nothing is pasted, so not sure what you mean.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
48,069
Office Version
  1. 365
Platform
  1. Windows

Watch MrExcel Video

Forum statistics

Threads
1,113,903
Messages
5,544,962
Members
410,643
Latest member
sng
Top