VBA Formula Loop Question

tejadajm

New Member
Joined
Aug 21, 2002
Messages
10
I have the following code that works well. However, because of the size of my file, this loop takes a long time to run. Is there anyway to streamline what I am doing and perhaps calculate the formula in the VBA script and only return the value to the cell in Excel? Any help is greatly appreciated.



Sub Update_YPL()
For rwNumber = 6 To 55
For clNumber = 3 To 233
Cells(rwNumber, clNumber).Select

ActiveCell.FormulaR1C1 = _
"=IF(R4C="""","""",SUMIF(INDIRECT(""'""&R1C&""'!$C$1:$C$2000""),RC1,INDIRECT(""'""&R1C&""'!""&R4C&"":""&R4C)))"

Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Next clNumber
Next rwNumber
End Sub
 

Some videos you may like

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Sub Update_YPL()
With Range(Cells(6, 3), Cells(55, 233))
    .FormulaR1C1 = "=IF(R4C="""","""",SUMIF(INDIRECT(""'""&R1C&""'!$C$1:$C$2000""),RC1,INDIRECT(""'""&R1C&""'!""&R4C&"":""&R4C)))"
    .Value = .Value
End With
End Sub
 

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
You can do it without a loop

Code:
Dim MyRange as Range
Set MyRange = Range(Cells(6,3),Cells(55,233))
MyRange.FormulaR1C1 = "=IF(R4C="""","""",SUMIF(INDIRECT(""'""&R1C&""'!$C$1:$C$2000""),RC1,INDIRECT(""'""&R1C&""'!""&R4C&"":""&R4C)))"
MyRange.Copy
MyRange.PasteSpecial xlPasteValues

Hope that helps.
 

tejadajm

New Member
Joined
Aug 21, 2002
Messages
10
Both of these solutions worked great. Thanks so much for the help and the quick response.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,271
Messages
5,600,650
Members
414,399
Latest member
Ninjee

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
Top