Slow Code (short)

royca

New Member
Joined
Jan 27, 2011
Messages
44
Hi folks, I have a shared workbook that breaks if you sort and filter in certain ways because of blank cells. I would like it serialized when I use it, this code works but seems slower than I would expect. Is there a technique someone would teach me that might decrease the time this takes this to complete?

'col 3 has the unique identifier I would like to tie to an ordered serial number
Dim serial As Long
Dim row As Long

serial = 0
row = 3
While Sheets("data").Cells(row, 3) <> ""
If Sheets("data").Cells(row, 3) <> Sheets("data").Cells(row - 1, 3) Then
serial = serial + 1
End If
Sheets("data").Cells(row, 4) = serial
row = row + 1
Wend
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
This might be a little faster.

Code:
Sub t()
Dim c As Range, x As Long
With Sheets("data")
    For Each c In .Range("C3", .Cells(Rows.Count, 3).End(xlUp).Offset(-1))
        If c.Value <> c.Offset(1).Value Then
            c.Offset(1, 1) = x + 1
            x = x + 1
        End If
    Next
End With
End Sub
 
Last edited:
Upvote 0
try this
Code:
Sub AddSerial()

    Range("C2").Value = 1
    With Range("D3:D" & Range("C" & Rows.Count).End(xlUp).Row)
        .Formula = "=if(C3<>C2,D2+1,D2)"
        .Value = .Value
    End With
End Sub
Using Evaluate might be quicker still, but I can't get it to work:(
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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