VBA code not looping through sheets

matratus34

Board Regular
Joined
Nov 21, 2013
Messages
76
Hi,
Would anyone be able to tell me why this code doesn’t loopto the next worksheet and only performs the task on the activesheet?

I realise my code is poor but this is just a tester for afew additional tasks to be performed within the loop – but at this stage I can’teven get the loop to work!
As always thanks for all the support!

Code:
Sub InsertColumn()
For Each ws In ActiveWorkbook.Worksheets
Columns("A:AH").Select
Selection.EntireColumn.Hidden = False
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A6").Select
ActiveCell.FormulaR1C1 = "Project"

Range("B3").Select
Selection.Copy
Range("A7").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("A7:A399")
Next ws
End Sub

Thanks
Matt
 
Last edited by a moderator:

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
When you say Columns, Range etc in VBA you are not qualifying those ranges with a sheet. All ranges have a corresponding sheet. Therefore in an attempt to be helpful VBA when faced with an unqualified range will use the activesheet at the time. You need to qualify your ranges with your created variable, ws, and get rid of your select lines or you need to select ws first. If you get stuck come back.
 
Upvote 0
You didn't qualify any of your ranges with a worksheet object. You need something like this:
Code:
Sub InsertColumn()
For Each ws In ActiveWorkbook.Worksheets
   with ws
      .Columns("A:AH").EntireColumn.Hidden = False
      .Columns("A:A").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
      .Range("A6").Value = "Project"
      .Range("B3").Copy .Range("A7")
      .Range("A7").AutoFill Destination:=.Range("A7:A399")
   end with
Next ws
End Sub
 
Last edited:
Upvote 0
When you use Columns("A:AH") then it will apply to the active sheet. The quickest win is to add a line just under the For loop:

Code:
ws.Activate

Failing that, you'd have to change to:

Code:
ws.Columns("A:AH").Select

etc. although it would be even better to remove the reliance you have on the Selection object e.g.

Code:
ws.Columns("A:AH").EntireColumn.Hidden = False

Here's a re-written version with no Selection objects:

Code:
Sub InsertColumn()

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
    With ws
        .Columns("A:AH").EntireColumn.Hidden = False
        .Columns("A:A").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        .Range("A6").Value = "Project"
        .Range("B3").Copy Destination:=.Range("A7")
        .Range("A7").AutoFill Destination:=.Range("A7:A399")
    End With
Next ws

End Sub

WBD
 
Upvote 0
Thanks for all the replies guys - I'll go and look at what you've told me and try to apply it into what I'm trying to do.

All your help is greatly appreciated as always.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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