Efficient code - Cycle throught rows

dispelthemyth

Well-known Member
Joined
Mar 16, 2006
Messages
664
Office Version
  1. 365
Platform
  1. Windows
Hi

I generally analyse data for stuff that relates to my area and delete anything that does not belong to me


The code i use is below

(I use Excel 2007)

<Code>
Sub StartAnalysis()
3
If ActiveCell.Value = "DIVISION" Then GoTo 1
If ActiveCell.Value = "FJSNSD" Then GoTo 2
If ActiveCell.Value = "FJSION" Then GoTo 2

ActiveCell.EntireRow.Delete
ActiveCell.Offset(-1, 0).Select
GoTo 3

2
ActiveCell.Offset(-1, 0).Select
GoTo 3

1

End Sub

<End Code>

I generally speed this up by using the below to disable calculations and screen updating

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

But in some books i have used, it says the GoTo option is a bad/slow method for cycling through rows like this. I was wondering what the alternatives were


Thanks for any and all help


Graeme
 
Last edited:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try like this


Code:
Sub test()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = LR To 1 Step -1
    If IsError(Application.Match(Range("A" & i), Array("DIVISION", "FJSNSD", "FJSION"), 0)) Then Rows(i).Delete
Next i
End Sub
 
Upvote 0
Hi Graeme

There are multiple ways you could achieve this - for example if you wanted to loop you could perhaps use:

Code:
Sub Test()
Dim rng As Range
Set rng = Activecell
Do
   If rng.Value = "DIVISION" Then Exit Sub
   If Not (rng.Value = "FJSNSD" Or rng.Value = "FJSION") Then rng.EntireRow.Delete
   Set rng = rng.Offset(-1)
Loop While rng.Row>2
End Sub

Other options would include Autofilter (likely to be faster if you have lots of data).
 
Upvote 0
Thanks, your help is much appreciated

On the 2nd code, there is an object error on the line

Set rng = rng.Offset(-1)


Im going to look into the Autofiler option, as its for 10,000+ lines so that would end up deing the fastest route

thanks again, Graeme
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,463
Members
452,915
Latest member
hannnahheileen

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