Loopi through worksheets

Wheelie686

New Member
Joined
Oct 27, 2012
Messages
11
I am trying to loop through the first 4 sheets of a workbook and for some odd reason, it only seems to be doing it on Sheet 1.
Ideally I'd like it to look by the sheet index but I also tried an array with the names... neither one works.

Maybe a typo I just haven't picked up on?

VBA Code:
Sub Test()
Dim Sh As Worksheet
For Each Sh In Sheets(Array("Consumer", "Business", "Commercial", "Escalations"))
    Range("A2:C182").Select
Next
End Sub

I modified the code to simply select the range just to keep it simple for now.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
That's right
because you select the range
try to copy the range it would be ok
so avoid select
 
Upvote 0
Try doing it this way.... add the code to do whatever you need to the range
VBA Code:
Sub Test()
Dim Sh As Worksheet
For Each Sh In Sheets(Array("Consumer", "Business", "Commercial", "Escalations"))
    With Range("A2:C182")
    '.Do Stuff
    End With
Next
End Sub
 
Upvote 0
Thanks Michael M. That did the trick.
:confused: Surely you must have made some significant change as the suggested code will still only act on the active sheet?

Your initial request was to process the first 4 sheets in the workbook. You could do it like this without having to name the sheets.


VBA Code:
Sub First4Sheets()
  Dim i As Long
 
  For i = 1 To 4
    With Sheets(i).Range("A2:C182")
      'Do something, for example, put a border around
      .BorderAround xlContinuous
    End With
  Next i
End Sub

In most cases in vba there is no need to Select a range to work with it but if for some reason you do need to select, then you would need to Activate each sheet in turn before selecting a range since selection can only apply to the Active Sheet.
 
Upvote 0

Forum statistics

Threads
1,214,402
Messages
6,119,301
Members
448,885
Latest member
LokiSonic

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