1

I populate an array of numbers with some criteria and then what I am trying to get to is deleted all of the rows that are in this area.

Basically I go through a column and if in that specific row, the cell in this column matches a criteria, I add that row number into an array. After it is done going through all rows I want to delete all of the row numbers.

I'm having trouble figuring out how to delete all rows at once because obviously if I do it one at a time the row numbers change as the one prior or below gets deleted. Because of this I want to select all of the rows together and then just call the Delete command on all rows at once. ANy ideas?

2
  • 1
    You could just store a value in a hidden column as you go through the rows, indicating that row needed to be deleted. Commented Oct 3, 2012 at 20:29
  • Use autofilter to filter the rows based on your criteria, then delete the hidden rows in one hit using specialcells. Commented Oct 3, 2012 at 21:47

3 Answers 3

9
Sub Tester()
Dim arr
    arr = Array(3, 5, 7, 9)
    ActiveSheet.Range("A" & Join(arr, ",A")).EntireRow.Delete
End Sub
Sign up to request clarification or add additional context in comments.

Comments

2

Iterate backwards through your rows.

Something like:

Sub tester()

    'setting ScreenUpdating false makes this go faster...
    Application.ScreenUpdating = False
    Dim i As Integer
    'go through all rows starting at last row
    For i = Range("A1:E5").Rows.Count To 1 Step -1
        'check if you need to delete them (you will want to update this)
        If Cells(i, 1).Value = "Delete this row!" Then
            Rows(i).Delete
        End If

   Next i


 Application.ScreenUpdating = True
End Sub

1 Comment

This does not delete all rows in the array at once (as asked in the question) but relies on a loop and is thus a lot slower than Tim William's solution
1

Here's a simple one:

If Range("B1") <> "" Then ' Range that bears the array of cell.addresses....

    ar = Array(Range(Range("B1").Cells))

    For Each a In ar
        a.EntireRow.ClearContents
    Next

    Range("B1").ClearContents
End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.