How can I simplify this macro so it loops through certain ranges, I am trying to skip subtotal functions

jtt2

New Member
Joined
Sep 19, 2023
Messages
7
Office Version
  1. 365
Sub MyFormulaAdder()

Range("C12").Formula = "=" & Range("C12").Value & "+" & Range("D12").Value
Range("C13").Formula = "=" & Range("C13").Value & "+" & Range("D13").Value
Range("C14").Formula = "=" & Range("C14").Value & "+" & Range("D14").Value

Range("C18").Formula = "=" & Range("C18").Value & "+" & Range("D18").Value
Range("C19").Formula = "=" & Range("C19").Value & "+" & Range("D19").Value
Range("C20").Formula = "=" & Range("C20").Value & "+" & Range("D20").Value
Range("C21").Formula = "=" & Range("C21").Value & "+" & Range("D21").Value
Range("C22").Formula = "=" & Range("C22").Value & "+" & Range("D22").Value
Range("C23").Formula = "=" & Range("C23").Value & "+" & Range("D23").Value
Range("C24").Formula = "=" & Range("C24").Value & "+" & Range("D24").Value
Range("C25").Formula = "=" & Range("C25").Value & "+" & Range("D25").Value

End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Sub MyFormulaAdder()

Range("C12").Formula = "=" & Range("C12").Value & "+" & Range("D12").Value
Range("C13").Formula = "=" & Range("C13").Value & "+" & Range("D13").Value
Range("C14").Formula = "=" & Range("C14").Value & "+" & Range("D14").Value

Range("C18").Formula = "=" & Range("C18").Value & "+" & Range("D18").Value
Range("C19").Formula = "=" & Range("C19").Value & "+" & Range("D19").Value
Range("C20").Formula = "=" & Range("C20").Value & "+" & Range("D20").Value
Range("C21").Formula = "=" & Range("C21").Value & "+" & Range("D21").Value
Range("C22").Formula = "=" & Range("C22").Value & "+" & Range("D22").Value
Range("C23").Formula = "=" & Range("C23").Value & "+" & Range("D23").Value
Range("C24").Formula = "=" & Range("C24").Value & "+" & Range("D24").Value
Range("C25").Formula = "=" & Range("C25").Value & "+" & Range("D25").Value

End Sub

Try this on a copy of your data.

VBA Code:
Public Sub subLooping()
Dim rng As Range
    
    For Each rng In Range("C12:C14,C18:C25").Cells
        rng.Value = (rng.Value + rng.Offset(0, 1).Value)
    Next rng
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,152
Members
452,891
Latest member
JUSTOUTOFMYREACH

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