Loop Though all worksheets in work book

Cooky857

New Member
Joined
Jan 9, 2019
Messages
16
HI all

i know I'm missing something very small but i'm lost, i have the below code that i need to loop though every worksheet in the workbook, but it only works on the worksheet i'm on what am i missing Please

VBA Code:
Sub Copy()

Dim WB As Workbook

Dim ws As Worksheet

For Each ws In Worksheets

     
 Range("A1:L2").Copy
  
 Range("A6").PasteSpecial Transpose:=True
 
Next ws

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Looping through the worksheets does NOT select/activate them. You need to add sheet references to your ranges, i.e.
Rich (BB code):
Sub Copy()

Dim WB As Workbook

Dim ws As Worksheet

For Each ws In Worksheets

     
 ws.Range("A1:L2").Copy
  
 ws.Range("A6").PasteSpecial Transpose:=True
 
Next ws

End Sub
 
Upvote 0
Solution
Looping through the worksheets does NOT select/activate them. You need to add sheet references to your ranges, i.e.
Rich (BB code):
Sub Copy()

Dim WB As Workbook

Dim ws As Worksheet

For Each ws In Worksheets

   
 ws.Range("A1:L2").Copy
 
 ws.Range("A6").PasteSpecial Transpose:=True
 
Next ws

End Sub[/CODE
thank you so much cant believe i missed that relay helpful thanks
 
Upvote 0
You are welcome.

Also, you should not use reserved words like "Copy" as the names of variables, procedures, or functions.
Doing that can cause unexpected behavior or errors.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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