Improve VBA function run-time - replace For/Next Loop

grexcelman

Board Regular
Joined
Mar 1, 2015
Messages
50
I have the following code which operates as it should, taking a text string in the adjacent cell and converting it to a formula and applying it to a few other referenced cells. I currently only have about 20 rows of data but I anticipate having nearly 1,000. The run-time is already too slow...I can only imagine how bad it would be trying to evaluate 1,000 cells. Any advice on how to convert this code into a faster function or UDF?

Code:
Sub randcalc()
Application.ScreenUpdating = False

Dim i As Long
    
    For i = Range("D" & Rows.Count).End(xlUp).Row To 31 Step -1
Range("N" & i).Select
foutput = "=" & ActiveCell.Offset(0, -1).Range("A1").Value & "(RC[2]:RC[5])"

ActiveCell.FormulaR1C1 = foutput
    Next i
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
From personal experience, avoid ".Select" as it really can bog down a script. Instead of having foutput reference ActiveCell, try something like:
Code:
Range("N" & i) = Range("M" & i).Range("A1").Value & "(RC[2]:RC[5])"

I am not able to test, but give it a shot and let us know what happens.
 
Upvote 0
From personal experience, avoid ".Select" as it really can bog down a script. Instead of having foutput reference ActiveCell, try something like:
Code:
Range("N" & i) = Range("M" & i).Range("A1").Value & "(RC[2]:RC[5])"

I am not able to test, but give it a shot and let us know what happens.

Thanks for your reply. The runtime seems about the same but also after removing the fouput reference it no long works as a formula, it simply prints the string as text into the cell. my original function would take the text in the adjacent cell, M, and apply in N as a formula.

I've tried the following but still prints the string as text.
Code:
Range("N" & i).FormulaR1C1 = Range("M" & i).Range("A1").Value & "(RC[2]:RC[5])"
 
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,458
Members
449,085
Latest member
ExcelError

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