Improve performance of "For.. Each...If ..... next" code

Alroj

Board Regular
Joined
Jan 12, 2016
Messages
53
Office Version
  1. 365
Platform
  1. Windows
Hi Team,

I have this code that I use to calculate the difference between "1Act" minus "2Act" and place the result in "3Act". All these 3 names are located in column A and they are organised by the name of the event located column 4. So, what I am doing is if the name of the event (located in column 4) matches for "1Act", "2Act", "3Act" (located in column A) then the macro calculates the difference and copy the formula across columns for different days and then copy the formulas as values.

This macro works BUT it is slow. I am hoping you may be able to help me improve the performance of the below code please

Much appreciated

VBA Code:
 Range("A1:az8").Select
 Selection.Find(What:="Combination", LookIn:=xlValues, LookAt:=xlWhole).Offset(1, 0).Select

 
 For Each cell In Range(ActiveCell, ActiveCell.End(xlDown))
 If cell.Value = "3Act" Then
  cell.Offset(0, 4).FormulaR1C1 = "=SUMIFS(R9C:R400C,R9C1:R400C1,""1Act"",R9C4:R400C4,RC4)-SUMIFS(R9C:R400C,R9C1:R400C1,""2Act"",R9C4:R400C4,RC4)"
  cell.Offset(0, 4).Copy
  Range(cell.Offset(0, 4), cell.Offset(0, 4).End(xlToRight)).Select
   Selection.PasteSpecial Paste:=xlFormulas
   Selection.Copy
   Selection.PasteSpecial Paste:=xlPasteValues
   Application.CutCopyMode = False
 End If
            
Next
 
Try.
VBA Code:
Dim Frng As Range
Set Frng = Range("A1:az8").Find(What:="Combination", LookIn:=xlValues, LookAt:=xlWhole).Offset(1, 0)
If Not Frng Is Nothing Then
 For Each cell In Range(Frng, Frng.End(xlDown))
 If cell.Value = "3Act" Then
  Range(cell.Offset(0, 4), cell.Offset(0, 4).End(xlToRight)).FormulaR1C1 = "=SUMIFS(R9C:R400C,R9C1:R400C1,""1Act"",R9C4:R400C4,RC4)-SUMIFS(R9C:R400C,R9C1:R400C1,""2Act"",R9C4:R400C4,RC4)"
 End If

Next
End If
 
Upvote 0

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