Looking for suggestion on how to rewrite a loop to make faster?

mayoung

Active Member
Joined
Mar 26, 2014
Messages
257
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
I have created the following loop but it takes to long to run. Any suggestions on how to change it? I would be very grateful. New at this and self taught.

Code:
    Range("F2").Select

    Do While Not IsEmpty(ActiveCell.Offset(0, -5))


        If Selection = "FIELD SERVICE" Then


            With Selection
                .EntireRow.Delete
            End With


                    ElseIf ActiveCell.Interior.Color = 10066431 Then
            
                        With Selection
                            .EntireRow.Delete
                        End With


        ElseIf Selection = "INTERNAL" Then


            With Selection
                .EntireRow.Delete
            End With


        ElseIf ActiveCell.Select <> "FIELD SERVICE" Then


            ActiveCell.Offset(1, 0).Select


        End If


    Loop
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Tell me in words what your wanting to do.

Sounds like your looking for values in Column A for:
FIELD SERVICE

INTERNAL

Or

Interior.Color = 10066431

And if found delete entire row.

Is this what your trying to do?
 
Upvote 0
I am wanting to check cells in column F and loop down as long as column A is not empty
and delete entire row if the criteria you have listed is found. The main goal is delete the entire row if any cell in column F contains the criteria you have listed.
 
Upvote 0
I have created the following loop but it takes to long to run. Any suggestions on how to change it?
It will be much faster if you do not use a loop at all...
Code:
[table="width: 500"]
[tr]
	[td]Sub DeleteRows()
  With Range("A1", Cells(Rows.Count, "A").End(xlUp)).Offset(, 5)
    .Replace "FIELD SERVICE", "#N/A", xlWhole, , False, , False, False
    .Replace "INTERNAL", "#N/A", xlWhole, , False, , False, False
    Application.FindFormat.Clear
    Application.FindFormat.Interior.Color = 10066431
    .Replace "", "#N/A", , , , , True, False
    Application.FindFormat.Clear
    Columns("F").SpecialCells(xlConstants, xlErrors).EntireRow.Delete
  End With
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
I like the use of the Replace / xlerrors Rick.
Very slick...(y)
 
Upvote 0
Just for interest, could also use a filter :
Code:
Sub DeleteRows()
Dim rw&
Application.ScreenUpdating = False
Rows(1).Insert
[F1] = "Temp"
rw = Cells(Rows.Count, "A").End(xlUp)(2).Row
Cells(rw, "F").Interior.Color = 10066431
[F:F].AutoFilter
With Range("F1:F" & rw)
    .AutoFilter Field:=1, Criteria1:=10066431, Operator:=xlFilterCellColor
    .Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    .AutoFilter Field:=1, Criteria1:="=FIELD SERVICE", Operator:=xlOr, Criteria2:="=INTERNAL"
    .SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
End Sub
 
Upvote 0
Rick Rothstein I am a self taught person and still learning everyday. Can you please explain two pieces of your code so I can better understand how it works? The .Repace Field Service and the part about the color? Trying to understand the #NA and xlwhole and the commas with true false etc? Thank you in advance for taking the to explain if you do.
 
Upvote 0
Footoo thank you for your response as well. Both codes work great.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,399
Members
448,958
Latest member
Hat4Life

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