Macro insert new column in all worksheet

tuytuy

Board Regular
Joined
Mar 28, 2013
Messages
75
Hi i have a workbook with 21 worksheet.
i was trying to write a macro that would add a column at the beginning of each sheet with the name of the month entered by the user.
Here is my code, i don't know what is wrong with it but all i does is add 21 new column in sheet 1.
Code:
Sub Month ()
' ask user for month name

      Dim monthName As String
    monthName = InputBox(Prompt:="What Month", _
          Title:="Please entre name of the month", Default:="January 2013")


' insert a column
  Dim sht As Worksheet
    
    For Each sht In ActiveWorkbook.Worksheets
         
         Columns("A:A").Insert Shift:=xlToLeft, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("A1").Value = monthName
         
      Next sht
  
 End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I could be wrong, but I THINK that this:
Code:
Columns("A:A").Insert Shift:=xlToLeft,
should be this:
Code:
Columns("A:A").Insert Shift:=xlToRight,
 
Upvote 0
Hi!
try this
(activate the sheets first like below)

Sub Month()
' ask user for month name


Dim monthName As String
monthName = InputBox(Prompt:="What Month", _
Title:="Please entre name of the month", Default:="January 2013")




' insert a column
Dim sht As Worksheet

For Each sht In ActiveWorkbook.Worksheets
sht.Activate
Columns("A:A").Insert Shift:=xlToLeft, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Value = monthName

Next sht

End Sub
 
Upvote 0
Put this in a new module and amend as required.

Code:
Option Explicit

Sub AddColumn()

Dim sht As Worksheet
Dim result As String

result = InputBox("Please enter month...")

For Each sht In ActiveWorkbook.Worksheets
    sht.Range("A1").EntireColumn.Insert xlShiftToRight
    sht.Cells(1, 1) = result
Next sht

End Sub
 
Upvote 0
Put this in a new module and amend as required.

Code:
Option Explicit

Sub AddColumn()

Dim sht As Worksheet
Dim result As String

result = InputBox("Please enter month...")

For Each sht In ActiveWorkbook.Worksheets
    [B][COLOR=#a52a2a]sht.Range("A1").EntireColumn.Insert xlShiftToRight[/COLOR][/B]
    sht.Cells(1, 1) = result
Next sht

End Sub
You should be able to replace the highlighted line with this as well...

sht.Columns("A").Insert xlShiftToRight

It works for me.
 
Upvote 0

Forum statistics

Threads
1,215,336
Messages
6,124,331
Members
449,155
Latest member
ravioli44

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