0

I am unsure how to create this loop so I did a recorded macro of it. How can I make this a loop that runs through all worksheets?

Sheets("AT").Select
    Columns("F:F").Select
    Selection.Cut
    Columns("E:E").Select
    Selection.Insert Shift:=x1ToRight
    Columns("H:H").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("I:I").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Columns("K:K").Select
    Selection.Cut
    Columns("J:J").Select
    Selection.Insert Shift:=x1ToRight
Alex Knauth
  • 8,133
  • 2
  • 16
  • 31
worldCurrencies
  • 417
  • 3
  • 13

1 Answers1

1

You cannot .Select a cell/range on a worksheet that isn't the ActiveSheet so essentially you have two choices; either .Activate each worksheet in a loop before running your code on that worksheet (not recommended) or modify your code so that .Select and .Activate is no longer needed (recommended).

Option Explicit

Sub Macro1()

    Dim w As Long

    For w = 1 To Worksheets.Count
        With Worksheets(w)
            .Columns("F:F").Cut
            .Columns("E:E").Insert Shift:=xlToRight

            .Columns("H:I").Insert Shift:=xlToRight

            .Columns("K:K").Cut
            .Columns("J:J").Insert Shift:=xlToRight
        End With
    Next w

End Sub