【问题标题】:Loop through entire workbook and delete rows that have #N/A循环遍历整个工作簿并删除具有 #N/A 的行
【发布时间】:2020-01-31 11:00:04
【问题描述】:

我有一个 11 张工作簿。我需要删除所有工作表中出现#N/A 的行。我该怎么做呢?

我已经写了一些代码,见下文,它允许我只删除当我的 excel 文件打开到该特定选项卡时出现的 #N/A 行。它不会循环遍历整个工作簿。

Sub RemoveErrorsLoop()

     Dim WS_Count As Integer
     Dim I As Integer

     ' Set WS_Count equal to the number of worksheets in the active
     ' workbook.
     WS_Count = ActiveWorkbook.Worksheets.Count

     ' Begin the loop.
     For I = 2 To WS_Count
         Cells.SpecialCells(xlCellTypeFormulas, xlErrors).EntireRow.Delete
     Next I

End Sub

我想要循环遍历我的所有工作表并删除具有#N/A 的行的代码。感谢您的帮助!

【问题讨论】:

    标签: excel vba for-loop


    【解决方案1】:

    您只是忘记在循环中使用“我”:
    如果您只是添加Sheets(I).,您的行将适用于工作簿的每一页。

    Sub RemoveErrorsLoop()
             Dim WS_Count As Integer
             Dim I As Integer
    
             ' Set WS_Count equal to the number of worksheets in the active
             ' workbook.
             WS_Count = ActiveWorkbook.Worksheets.Count
    
             ' Begin the loop.
             For I = 1 To WS_Count
                 Sheets(I).Cells.SpecialCells(xlCellTypeFormulas, xlErrors).EntireRow.Delete
             Next I
    
    End Sub
    

    当您在工作表中没有#N/A 时,我什至会添加一个错误处理程序以让代码正常工作。

             For I = 1 To WS_Count
                On Error Resume Next
                   Sheets(I).Cells.SpecialCells(xlCellTypeFormulas,xlErrors).EntireRow.Delete
                On Error GoTo 0
             Next I
    

    【讨论】:

    • 没用。给我一个运行时错误“1004”并且不会贯穿整个工作簿。
    • @hkml 如果没有类别为xlErrors的单元格,则应为。
    • @hkml 查看最后的错误处理部分
    • @pierre44 你可以使用If Not Sheets(I).Cells.SpecialCells(xlCellTypeFormulas, xlErrors) is nothing then Sheets(I).Cells.SpecialCells(xlCellTypeFormulas, xlErrors).EntireRow.Delete 代替On Error 部分。
    猜你喜欢
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 2019-11-07
    相关资源
    最近更新 更多