Iterate through cells in a range

I am currently using the following code to check column A in a specific cell range for # N / A, and if it is found, I will delete that row.

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

I need to expand this, so I check all columns 1 through 10, not just A. I tried this small modification (a nested loop still), but it does not work. Any suggestions?

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
+3
source share
4 answers

two questions:

  • nested

  • on in any given line after detecting N / A, you need to cancel the loop

to try

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
+2
source

This can lead to errors in languages ​​other than English.

Sub DeleteNA()

    Dim rRange As Range
    Dim rFound As Range

    Const sNA As String = "#N/A"

    Do
        Set rRange = Sheet1.Range("A29:F49")
        Set rFound = rRange.Find(sNA, , xlValues, xlWhole, xlByRows)
        If Not rFound Is Nothing Then
            rFound.EntireRow.Delete
        Else
            Exit Do
        End If
    Loop

End Sub

Modify A29: F49 according to your data.

+2
source

Sheets("Sheet1").Select
Set cols = Range("A1:D80")
For Each Cell In cols
    If Cell.Value = "XXX" Then
        Cell.EntireRow.Delete
    End If
Next
0

I suppose there were problems with the nested "c" clauses that you used.

You can determine the correct range and use the "for everyone" loop, which will make reading more understandable and understandable. I named the range as "MyRange" for testing purposes.

Sub test()

    Dim cell As Excel.Range

    For Each cell In [myRange]

        If CVErr(cell.Value) = CVErr(xlErrNA) Then cell.EntireRow.Delete

    Next cell

End Sub
0
source

Source: https://habr.com/ru/post/1795278/


All Articles