Alternative code to speed a macro

olympiac

Board Regular
Joined
Sep 26, 2010
Messages
158
It takes more than 5 minutes to run this code in a spreadsheet with more than 60.000 rows
The code below looks at the value in column "C" and if the value = Paris then the the relative cell in column "A" is updated with the value "code001"

Dim LastRow As Long
' Dim i As Long
' LastRow = Range("C" & Rows.Count).End(xlUp).Row
' For i = 2 To LastRow
' If Range("C" & i).Value = "Paris" Then
' Range("A" & i).Value = "code001"
' End If
' Next i
Is there any alternative to speed it up?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Is this better?
Code:
Dim LastRow As Long
With ActiveSheet
    If .AutoFilterMode Then .AutoFilterMode = False
    LastRow = .Range("C" & Rows.Count).End(xlUp).row
    With .Range("A1:C" & LastRow)
        .AutoFilter
        .AutoFilter Field:=3, Criteria1:="Paris"
    End With
    .Range("A2:A" & LastRow).SpecialCells(xlCellTypeVisible) = "code001"
    .AutoFilterMode = False
End With
 
Upvote 0
Check out this Site:

http://www.ozgrid.com/VBA/SpeedingUpVBACode.htm

I'm thinking that encapsulating your code as below might help:

Code:
Sub yourSub()

[COLOR=Red][B]Application.ScreenUpdating=False[/B][/COLOR]

Dim LastRow As Long
    Dim i As Long
    LastRow = Range("C" & Rows.Count).End(xlUp).Row
    For i = 2 To LastRow
        If Range("C" & i).Value = "Paris" Then
            Range("A" & i).Value = "code001"
        End If
    Next i
[COLOR=Red][B]
Application.ScreenUpdating=True[/B][/COLOR]

End Sub
 
Upvote 0
Here's another possibility

Code:
Sub paris()
    Application.ScreenUpdating = False
    lr = Range("C" & Rows.Count).End(xlUp).Row
    With Range("A2:A" & lr)
        .FormulaR1C1 = "=IF(RC[2]=""Paris"",""code001"","""")"
        .Value = .Value
    End With
End Sub

Of course, this won't work if there are values in the rows that don't equal "Paris" that you don't want to disturb.
 
Upvote 0
And here's another which avoids the issue with the previous solution.
Code:
Sub paris2()
    Application.ScreenUpdating = False
    With Range("C2", Range("C" & Rows.Count).End(xlUp))
        Set f = .Find(what:="Paris", lookat:=xlWhole)
        If Not f Is Nothing Then
            fs = f.Address
            Do
                f.Offset(, -2).Value = "Code001"
                Set f = .FindNext(f)
            Loop Until f.Address = fs
        End If
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,181
Members
452,893
Latest member
denay

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