VBA Autofilter causing error

JoeRooney

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

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,548
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
118
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
55,548
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
118

ADVERTISEMENT

Thank you fluff , works perfect.
 

Fluff

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

JoeRooney

Board Regular
Joined
Nov 27, 2017
Messages
118

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
55,548
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
55,548
Office Version
  1. 365
Platform
  1. Windows

Watch MrExcel Video

Forum statistics

Threads
1,127,573
Messages
5,625,593
Members
416,121
Latest member
MrBuzz

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
Top