I am looking for a way to find the N/A
cells in the entire columns and then delete the entire row if N/A
is found. I found this VBA code, but it just doesn’t work for me because it selects only one column. Please help (I am working on around 300000 rows in Excel Mac 2011)
Sub DeleteBlankARows()
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
Dim r As Long
For r = Cells(Rows.Count, 11).End(xlUp).Row To 1 Step -1
If Cells(r, 11) = "" Then Rows(r).Delete
Next r
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
Try this (tested on Windows XP / Excel 2007 but it should work on Mac / Office 2011):
Option Explicit
Sub DeleteNARows()
Dim r As Long
Dim iCol As Long
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
.DisplayAlerts = False
For iCol = 1 To Cells(1, Columns.Count).End(xlToLeft).Column
For r = Cells(Rows.Count, iCol).End(xlUp).Row To 1 Step -1
If Application.WorksheetFunction.IsNA(Cells(r, iCol)) Then Rows(r).Delete
Next r
Next iCol
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
Note that you can (and probably have to) change the column where you want to check fot #N/A
at the begining of the code
Answer:
Another way would be to use Find
to quickly test what cells were NA()
(I have assumed your are testing for =NA()
as formula cells – I can update if they are, or also can be text)
This code uses SpecialCells
to search only error values.
Sub GetNA()
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim strFA As String
On Error Resume Next
Set rng1 = ActiveSheet.UsedRange.SpecialCells(xlFormulas, xlErrors)
On Error GoTo 0
If rng1 Is Nothing Then
MsgBox "No NA() cells"
Exit Sub
End If
With rng1
Set rng2 = .Find("=NA()", LookIn:=xlFormulas)
If Not rng2 Is Nothing Then
strFA = rng2.Address
Set rng3 = rng2.EntireRow
Do
Set rng2 = .FindNext(rng2)
Set rng3 = Union(rng3, rng2.EntireRow)
Loop While Not rng2 Is Nothing And rng2.Address <> strFA
End If
If Not rng3 Is Nothing Then rng3.Delete
End With
End Sub
Tags: excelexcel, vba