Code not deleting every non numeric row

前端 未结 1 562
滥情空心
滥情空心 2020-12-12 01:39

I have this code here which looks through a column of numbers, colors the cells with numbers and deletes the cells and its corresponding rows if it\'s a nonnumeric entry(the

相关标签:
1条回答
  • 2020-12-12 02:24

    There seem to be two problems with your code. The primary issue of skipping some rows that should be deleted can be cleared up by looping from the bottom to the top. Failing to work in ths manner may mean that a row is skipped after a row is deleted, the rows are renumbered and you increment on to the next row.

    There is a secondary issue where you have implemented a With ... End With statement that references the worksheet to be acted upon and then either reiterate the worksheet reference when referencing cells/ranges/rows or discard it altogether.

    With wsO
        For iCounter1 = 2 To LastRow
            If .Cells(iCounter1, 9) > 120 Then
                .Cells(iCounter1, 9).Interior.Color = RGB(255, 101, 101)
            ElseIf .Cells(iCounter1, 9) > 0 And .Cells(iCounter1, 9) < 120 Then
                .Cells(iCounter1, 9).Interior.Color = RGB(169, 208, 142)
            End If
        Next iCounter1
    
        'this is the primary change
        'loop from the bottom to the top when deleting rows
        For iCounter2 = LastRow To 2 Step -1
            If Not IsNumeric(.Cells(iCounter2, 9)) Then
                .Cells(iCounter2, 9).EntireRow.Delete
            End If
        Next iCounter2
    
        .Rows("2:200").RowHeight = 30
    End With
    

    Note that Cells becomes .Cells and Rows become .Rows. The period (aka . or full stop`) prefix associates each cell/range/row with the parent worksheet referenced in the With ... End With block.


    ¹ Thanks to Scott Craner for noting the bottom-to-top method in comments.

    0 讨论(0)
提交回复
热议问题