Filter data set to #N/A, skip step entirely if none

rachel06

Board Regular
Joined
Feb 3, 2016
Messages
103
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I know several threads exist on this topic but I'm apparently really bad at wording my searches because I'm not finding my answer.

Below is the code I'm working with for my macro for a specific data set. It filters to #N/A lines, moves them to a new sheet, then deletes them from the original sheet.

I need to make two changes to it.

1. Not have exact cell references (pertaining to the # of rows - the A-D will always be the same.
2. Skip this step entirely if there are no #n/a values present. Right now, the macro bugs out if there are no #n/a lines

Thanks so much!!

Code:
  ActiveSheet.Range("$A$1:$D$7116").AutoFilter Field:=4, Criteria1:="#N/A"
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("NA Lines").Select
    Range("A1").Select
    ActiveSheet.Paste
    Cells.Select
    Cells.EntireColumn.AutoFit
    Range("A1").Select
    Sheets("MACRO").Select
       lr = Cells(Rows.Count, 1).End(xlUp).Row
    If lr > 1 Then
        Range("A2:A" & lr).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End If
    ActiveSheet.ShowAllData
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try this:

VBA Code:
With ActiveSheet.Range("A1").CurrentRegion
   .AutoFilter Field:=4, Criteria1:="#N/A"
   If .Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
      .Copy Sheets("NA Lines").Range("A1")
      Sheets("NA Lines").UsedRange.EntireColumn.AutoFit
      .Resize(.Rows.Count - 1).Offset(1).Columns(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End If
   End With
    ActiveSheet.ShowAllData
 
Upvote 1
Solution
Try this:

VBA Code:
With ActiveSheet.Range("A1").CurrentRegion
   .AutoFilter Field:=4, Criteria1:="#N/A"
   If .Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
      .Copy Sheets("NA Lines").Range("A1")
      Sheets("NA Lines").UsedRange.EntireColumn.AutoFit
      .Resize(.Rows.Count - 1).Offset(1).Columns(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    End If
   End With
    ActiveSheet.ShowAllData
Perfection. It even runs faster. Thanks so much!!!!
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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