hkm*_*kml 2 excel vba for-loop
我有一本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
Run Code Online (Sandbox Code Playgroud)
我想要可以遍历我所有工作表的代码,并删除具有#N / A的行。谢谢你的帮助!
您只是忘了在循环中使用“ I”:
如果您仅添加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
Run Code Online (Sandbox Code Playgroud)
当表中没有#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
Run Code Online (Sandbox Code Playgroud)