Share via

Macro To Insert Column Not Working

Anonymous
2012-04-20T16:32:10+00:00

Hello again helpful people,

My goal is to insert one column before column A in multiple worksheets in a large workbook.  I recorded my keystrokes while I did that, then copied that code between the 2 lines of this code:

For Each [worksheet] In ActiveWorkbook.Worksheets

Next [worksheet]

and came up with the code below:

Sub InsertColA()    Dim WSheet As Worksheet    For Each WSheet In ActiveWorkbook.Worksheets        Columns("A:A").Select        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove    Next WSheetEnd Sub

But the macro is not proceeding from worksheet to worksheet.  It just keeps inserting a column in the first worksheet.  I can't figure out why.  Could someone please help me?

Many, many thank yous,

-Lynne

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2012-04-20T16:48:57+00:00

Columns("A:A") refers to to the active sheet, not to WSheet.

Try this version:

Sub InsertColA()

    Dim WSheet As Worksheet

    For Each WSheet In ActiveWorkbook.Worksheets

        WSheet.Columns(1).Insert

    Next WSheet

End Sub

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2012-04-20T16:57:21+00:00

    HansV, thank you so much!  Of course, it works like a charm.

    And thank you for your explanation:

    Columns("A:A") refers to to the active sheet, not to WSheet.

    Have a wonderful day,

    Lynne

    Was this answer helpful?

    0 comments No comments