VBA excel代码删除行如果N / A被发现

我正在寻找一种方法来查找整个列中的N/A单元格,然后删除整个行如果N/Afind。 我发现这个VBA代码,但它只是不适合我,因为它只select一列。 请帮助(我在Excel Mac 2011中处理大约300000行)

 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 

试试这个(在Windows XP / Excel 2007上testing,但它应该可以在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 

请注意,您可以(也可能必须)在代码开始时更改要检查f #N/A的列

另一种方法是使用Find来快速testing哪些单元格是NA() (假设您正在testing=NA()作为公式单元格 – 如果它们是可更新的,或者也可以是文本)

此代码使用SpecialCells仅search错误值。

 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