Question regarding for loops vs arrays

Huby00

New Member
Joined
Nov 14, 2011
Messages
36
Hello,

Thanks to the members of this forum i have gone from completely clueless to beginning novice in vba. To improve, I am trying to get in the habit of creating code that is more efficient and faster.

I usually create for loops to run through the data in excel to delete unwanted values (such as an error in recording software that gives a 11111 value, for example).

Code:
Lastrow = Range("A65536").End(xlUp).Row
    For i = Lastrow To 1 Step -1
        If Cells(i, 5).Value = 11111 Then
        Rows(i).Select
        Selection.Delete Shift:=xlUp
        End If
    Next i

When i have a long data sheet of 20,000+ entries per column it takes a while.

My question is, would it be faster to create an array to store the row numbers of when 11111 and then delete them?


something like:
Code:
Dim counter As Integer
Dim deleterows() As Integer
counter = 0

Lastrow = Range("A65536").End(xlUp).Row
For i = 1 To Lastrow
If Cells(i, 5) = 11111 Then
    deleterows(counter) = i
    counter = counter + 1
End If
For i = counter To 0 Step -1
    Range(deleterows(counter) & ":" & deleterows(counter)).Delete Shift:=xlUp
    Next i
End Sub


Does anyone know how to delete ALL the rows at once without using a for loop? i tried on macro recorder but it gives me something like range("x:x, y:y, z:z.... etc
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top