1

我正在寻找一种方法来查找N/A整个列中的单元格,然后如果N/A找到则删除整行。我找到了这个 VBA 代码,但它对我不起作用,因为它只选择一列。请帮忙(我在 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 
4

2 回答 2

2

另一种方法是用于Find快速测试单元格是什么NA()(我假设您正在测试=NA()作为公式单元格 - 如果它们是,我可以更新,或者也可以是文本)

此代码用于SpecialCells仅搜索错误值。

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
于 2012-05-16T07:19:26.327 回答
1

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

#N/A请注意,您可以(并且可能必须)在代码开头更改要检查 fot 的列

于 2012-05-16T06:43:23.977 回答