Insert column into same spot in multiple worksheets

HookEm

Board Regular
Joined
Jun 26, 2002
Messages
85
Using VBE, how do I insert a new column into multiple worksheets at the same time?

Here's what I tried which did not work:
Sub Macro1()

Sheets.Select
Range("D4").Select
Selection.EntireColumn.Insert
Range("D5").Select
ActiveCell.FormulaR1C1 = "CODE"
End Sub

Thanks.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Assuming you want to insert a column in sheets 1, 2 & 3 and between columns A & B:

Sub InsertColumnSheets1and2and3()
Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select
Sheets("Sheet1").Activate
Columns("B:B").Select
Selection.Insert Shift:=xlToRight
End Sub
 
Upvote 0
I have to do this to several workbooks created by others. The names of the individual worksheets will be different. Is there a way I can select them all regardless of what they are named?

Sorry, forgot to add this: format will be the same on all worksheets. In other words, I can select cell D4 and insert column on each. Just the name of the worksheet will be different.

Thanks,
HookEm
This message was edited by HookEm on 2002-07-05 12:29
 
Upvote 0
You're correct, as far as I can see; you would have to edit the macro and change all the "Sheet1", etc., to the new names before the macro would run properly. How often do you have to do this? Wouldn't it be simpler just to do it manually? Select all the sheets, right-click on the column header of the column you want to insert in front of, and click on "Insert" Fini.
 
Upvote 0
I'm sure it would be easier than the time and effort that I'll put into making the macro work. Just one of those "There ought to be a way to ..." things!
 
Upvote 0
The VBA isn't too bad, the following ought to be variable:<pre>
Sub insrt()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
On Error Resume Next 'trap for data in "iv"
ws.[d:d].Insert
Next ws
End Sub</pre>

Hope this helps.

_________________
Cheers,<font size=+2><font color="red"> Nate<font color="blue"> Oliver</font></font></font>
drink.gif

This message was edited by NateO on 2002-07-05 12:54
 
Upvote 0
NateO, yours works beautifully, with the one caveat that it will insert a column in EVERY worksheet. If that's okay, o.k. but if one wanted to insert the column in, say, the first 5 of 7 worksheets, no go.
 
Upvote 0
True, I figured for each worksheet. A simple if statement corrects for your caveat:<pre>
Sub insrt()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Index = 6 Then Exit Sub
On Error Resume Next 'trap for data in "iv"
ws.[d:d].Insert
Next ws
End Sub</pre>

One could also use an if command to pick a variable range, say sheets 3 through 5.

Hope this helps.

_________________
Cheers,<font size=+2><font color="red"> Nate<font color="blue"> Oliver</font></font></font>
drink.gif

This message was edited by NateO on 2002-07-05 13:11
 
Upvote 0
Hello All,

As discussed by nato i have one question , Is there any possibility where ws.[d:d].Insert ,here can "d " be dynamic anything we insert in one sheet should be done in all other sheets.

Thank you ,
Sampath
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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