excel - 迭代某个范围内的单元格

标签 excel vba

目前,我正在使用以下代码检查特定单元格范围中的 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/

相关文章:

excel - 如何将具有可扩展行的 react 表导出到 csv 或 excel?

php - Laravel Excel 将设置值导出到单元格?

python - 在 python 中运行多个 OLS 回归

excel - 在 Excel 中使用 MS Query 查询自身(不是外部源)

python - 像函数一样使用 Excel 工作簿

excel - Application.run 有超过 30 个参数

vba - 如何将 Excel 工作表中第一个列表对象的范围设置为变量?

excel - Perl Spreadsheet::ParseExcel 无法解析并返回 undef

VBA 插件 ActiveWorkBook 何时变为事件状态?

Excel VBA Worksheetfunction.Match 不适用于日期