how can this loop be speeded up?

jalea148

Board Regular
Joined
Mar 23, 2012
Messages
58
iRowL is generally over 2000. Waiting for this loop to execute is like watching paint drying. Any suggestions will be appreciated.

Set Rng = Selection
For J = 2 To iRowL
X = Rng(J, 7) / Rng(J, 5)
For M = 2 To 4
Rng(J, M) = Rng(J, M) * X
Next M
Rng(J, 5) = Rng(J, 7)
Next J
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Drying agent?

Code:
Sub J148()
    Dim nRow        As Long
    Dim av          As Variant
    Dim i           As Long
    Dim j           As Long
    Dim d           As Double
    
    nRow = 22

    With Selection.Resize(nRow - 1, 7).Offset(1)
        av = .Value
        
        For i = 1 To nRow - 1
            d = av(i, 7) / av(i, 5)
            
            For j = 2 To 4
                av(i, j) = av(i, j) * d
            Next j
            
            av(i, 5) = av(i, 7)
        Next i

        For j = 2 To 5
            .Columns(j).Value = Application.Index(av, 0, j)
        Next j
    End With
End Sub
 
Upvote 0
Drying agent?

Code:
Sub J148()
    Dim nRow        As Long
    Dim av          As Variant
    Dim i           As Long
    Dim j           As Long
    Dim d           As Double
    
    nRow = 22

    With Selection.Resize(nRow - 1, 7).Offset(1)
        av = .Value
        
        For i = 1 To nRow - 1
            d = av(i, 7) / av(i, 5)
            
            For j = 2 To 4
                av(i, j) = av(i, j) * d
            Next j
            
            av(i, 5) = av(i, 7)
        Next i

        For j = 2 To 5
            .Columns(j).Value = Application.Index(av, 0, j)
        Next j
    End With
End Sub

WOW! That runs like greased lightning. Grazie mille [1000 thanx]
 
Upvote 0
You're welcome, glad it helped.
 
Upvote 0
Shg,
I'd like to understand why your solution is so much faster; it reduced the time from several minutes to under a second. If I understood, I'm sure I could profitably apply the method to many of my routines.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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