目前,我正在使用以下代码检查特定单元格范围中的 A 列是否有 #N/A 值,如果找到,我将删除该行。
With Sheets(Sheet)
For LRow = 45 To 29 Step -1
With .Cells(LRow, "A")
If (CVErr(.Value) = CVErr(xlErrNA)) Then .EntireRow.Delete
End With
Next LRow
End With
我需要扩展它,所以我检查所有列 1 到 10,而不仅仅是 A。我尝试了这个轻微的修改(嵌套另一个循环),但它不起作用。有什么建议吗?
With Sheets(Sheet)
For LRow = 45 To 29 Step -1
For LCol = 10 To 1 Step -1
With .Cells(LRow, LCol)
If (CVErr(.Value) = CVErr(xlErrNA)) Then .EntireRow.Delete
End With
Next LCol
Next LRow
End With
最佳答案
这里有两个问题:
嵌套
一旦发现 N/A,就在任何给定行上打开,您需要中止循环
尝试
Set sh = Sheets(Sheet)
For LRow = 45 To 29 Step -1
For LCol = 10 To 1 Step -1
If (CVErr(sh.Cells(LRow, LCol).Value) = CVErr(xlErrNA)) Then
sh.Cells(LRow, 1).EntireRow.Delete
Exit For ' Exit the LCol loop
End If
Next LCol
Next LRow
关于excel - 迭代某个范围内的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5145316/