Same code in multiple cells on multiple sheets

CookieMonster76

Board Regular
Joined
Apr 30, 2015
Messages
200
Hi

I have created the following to put the code in cells on multiple sheets:

For Each ws In Sheets(Array("C-H-1218 Jenner", "C-H-1219 St Peters", "C-H-1235 Moxley"))

With ws.Range("C12")
.FormulaR1C1 = "=-ROUND(SUMIFS('TB actual'!C4,'TB actual'!C3,RC[4],'TB actual'!C[-2],R3C2,0)"
.Value2 = .Value2
End With


With ws.Range("C13")
.FormulaR1C1 = "=-ROUND(SUMIFS('TB actual'!C4,'TB actual'!C3,RC[4],'TB actual'!C[-2],R3C2,0)"
.Value2 = .Value2
End With


With ws.Range("C16")
.FormulaR1C1 = "=-ROUND(SUMIFS('TB actual'!C4,'TB actual'!C3,RC[4],'TB actual'!C[-2],R3C2),0)"
.Value2 = .Value2
End With

next ws


Sine the code in the cells is the same, is there a way to reduce the lines of code on this (I am thinking specify the cells as an array also, but I don't know how to combine this with the array for the sheets)?

Thanks

Paul
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try

Code:
For Each ws In Sheets(Array("C-H-1218 Jenner", "C-H-1219 St Peters", "C-H-1235 Moxley"))

With ws.Range("C12, C13, C16")
.FormulaR1C1 = "=-ROUND(SUMIFS('TB actual'!C4,'TB actual'!C3,RC[4],'TB actual'!C[-2],R3C2,0)"
.Value2 = .Value2
End With

next ws
 
Upvote 0
Code:
Sub MyMAcro()
For Each ws In Sheets(Array("C-H-1218 Jenner", "C-H-1219 St Peters", "C-H-1235 Moxley"))
    SetRng ws.Range("C12")
    SetRng ws.Range("C13")
    SetRng ws.Range("C16")
Next ws
End Sub




Public Sub SetRng(pRng As Range)
With pRng
.FormulaR1C1 = "=-ROUND(SUMIFS('TB actual'!C4,'TB actual'!C3,RC[4],'TB actual'!C[-2],R3C2,0)"
.Value2 = .Value2
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

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