Code works, but really slow. How to make it faster?

pupsia

Board Regular
Joined
Dec 2, 2015
Messages
67
Hello all,

I was able to write a code that I need and works, but one part of the code works really slow when there is a lot of data.

I need to work with huge lists sometimes, and when I tested the code with about 500 lines, I had to wait quite a lot of time.

Could someone please help me make the code faster? The "Loop" part is the one that takes up a lot of time. But as I`m still quite new to macro, not sure how to change that...

Code:
Sub Get_Unique_Senders()


    Dim r As Range, i As Long, ar
    Dim c As Integer
    


Application.ScreenUpdating = False




ThisWorkbook.Worksheets("Report").Range("C2", ThisWorkbook.Worksheets("Report").Range("C2").End(xlDown)).Copy Destination:=ThisWorkbook.Worksheets("Temp").Range("A1")


Sheets("Temp").Select




Set wb = ThisWorkbook.Worksheets("Temp")


    With wb
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With




For c = 2 To LastRow
    With Range("A" & c)
        .Value = WorksheetFunction.Trim(.Value)
    End With
    
    With Range("A" & c)
        If Right(.Value, 1) = ";" Then .Value = Left(.Value, Len(.Value) - 1)
    End With


wb.Range("A2:A" & LastRow).Select


Selection.Replace What:="; ", Replacement:=";", LookAt:=xlPart, SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False


Next




    Set r = wb.Range("A" & LastRow)
    Do While r.Row > 1
        ar = Split(r.Value, ";")
        If UBound(ar) >= 0 Then r.Value = ar(0)
        For i = UBound(ar) To 1 Step -1
            r.Copy
            r.Offset(1).Insert
            r.Offset(1).Value = ar(i)
        Next
        Set r = r.Offset(-1)
    Loop




With wb
    .Range("A1", .Range("A1").End(xlDown)).RemoveDuplicates Columns:=1, Header:=xlYes
End With


ThisWorkbook.Worksheets("Temp").Range("A1", ThisWorkbook.Worksheets("Temp").Range("A12").End(xlDown)).Copy Destination:=ThisWorkbook.Worksheets("Sheet7").Range("A1")


ThisWorkbook.Worksheets("Sheet7").Columns(1).AutoFit


Sheets("Sheet7").Select


Application.ScreenUpdating = True


End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I don't trust 'Remove Duplicates'. To see why, do the small exercise suggested in post 4 here.

Wow...That is quite new :eeek: I always thought that the 'Remove Duplicates' works just fine and did not expect something like this. In a way, worried about the data I used to work and had to use the 'Remove Duplicates'. Hoe everything was good there..

Thank you for the additional information!! This is really good to know :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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