Need loop to run on specific sheets

Lball

Board Regular
Joined
Oct 4, 2005
Messages
197
How do i write the code to tell excel to perform same loop of data on specific sheets?

Mcode would apply to all worksheets. How do i loop this to run on each of the following named sheets? 102,104,114,118,203,204,401,703,802,901,902,903

Range("G26").Formula = "=Sum(G14:G25)"
Range("H26").Formula = "=Sum(H14:H25)"

thanks in advance!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try this
Code:
Sheets(Array("102","104","114","118","203","204","401","703","802","901","902","90")).
 
Upvote 0
Sub Answer()
For each x in array(102,104,114,118,203,204,401,703,802,901,902,903)
Sheets(x & "").Range("G26").Formula = "=Sum(G14:G25)"
Sheets(x & "").Range("H26").Formula = "=Sum(H14:H25)"
Next
End Sub

the reason for the X & "" is that when x is 102, this would be looking for the 102nd sheet, not sheet "102".

HTH
 
Upvote 0
Try something like:

Code:
Sub test()
For Each a In Array("102","104","114","118","203","204","401","703","802","901","902","903")
    With Sheets(a)
        .Range("G26:H26").Formula = "=Sum(G14:G25)"
    End With
Next a
End Sub
Also you can combine the fomrula part as it will increment it for you when it inputs it, so you have one less line of code. ;)

Hope that helps.
 
Upvote 0
HI THANKS!

I have never used arrays and couldn't figure it out from my books. That did the trick, thank you again!
 
Upvote 0
Without using arrays, you could use if statements, but it is not as clean:

Code:
Sub test()
For Each sh In Worksheets
if sh.name = "102" or sh.name =  "104" or sh.name =  "114" or sh.name =  "118" or sh.name =  "203" or sh.name =  "204" or sh.name =  "401" or sh.name =  "703" or sh.name =  "802" or sh.name =  "901" or sh.name =  "902" or sh.name =  "903" then
    With sh
        .Range("G26:H26").Formula = "=Sum(G14:G25)"
    End With
Next sh
End Sub
Hope that makes sense.
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,258
Members
452,901
Latest member
LisaGo

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