Speed Up Macro

katekoz

Board Regular
Joined
Jan 20, 2020
Messages
91
Office Version
  1. 2016
Platform
  1. Windows
I have a long macro that does many things and takes a really long time to run. I'd like to speed it up, but I'm not entirely sure which part is bogging it down. I have a guess that it's one of two places, as they're the two parts of the macro that "step through" data. I've copied each below. Does anyone have any tips on how I could speed these sections up, or could you let me know if they're just as good as they're going to get?

In this first section, the "Column 4" in "Overall User Data" that's being referenced currently has just over 10k rows of data. The "Column 4" in "New Data Add" is currently an empty set, so is checking 0 rows, and the most it will ever have is about 2k. "UserValue" is the value received from an inputbox earlier in the macro.

VBA Code:
With ThisWorkbook.Sheets("Overall User Data").Columns(4)
    Set Fnd = .Find(UserValue, , , xlWhole, , , False, , False)
End With
If Fnd Is Nothing Then
    With ThisWorkbook.Sheets("New Data Add").Columns(4)
        Set Fnd = .Find(UserValue, , , xlWhole, , , False, , False)
    End With
End If

This second code is dealing with a little table, about 21 rows long, so I doubt this would be bogging it down too much, which such a small data set. Maybe it is, though??

VBA Code:
Set ws = Sheets("All Users Month")

    lastRow = ws.Range("Y" & Rows.Count).End(xlUp).Row - 1

    With ws
        For i = 1 To lastRow
            If Len(Trim(.Range("Y" & i).Value)) <> 0 Then _
            .Range("Z" & (i + 1)).Formula = "=RAND()"
        Next i
    End With
    
    Worksheets("All Users Month").Range("Z:Z").Copy
    Worksheets("All Users Month").Range("Z:Z").PasteSpecial Paste:=xlPasteValues

If both of these look ok, then it must be some other part that's not running well. Thanks for any tips you can offer with this!
 
You can speed up the 2nd part of your code by using variant arrays and calculating the random number in VBA: like this: (untested)
VBA Code:
Sub test()
Set ws = Sheets("All Users Month")

    lastrow = ws.Range("Y" & Rows.Count).End(xlUp).Row - 1
    With ws
    'load column Y into a variant array
    yarr = Range(.Cells(1, 25), .Cells(lastrow, 25))
    'load column Z into a variant array
    Zarr = Range(.Cells(1, 26), .Cells(lastrow, 26))
   
        For i = 1 To lastrow
            If Len(Trim(yarr(i, 1))) <> 0 Then _
            Zarr(i + 1, 1) = Rnd()
        Next i
    Range(.Cells(1, 26), .Cells(lastrow, 26)) = Zarr
    End With
   
End Sub
 
Upvote 0

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.

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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