PDA

View Full Version : vba insert multiple columns after each existing column



ahc234
12-20-2012, 02:45 PM
Hello,
I am new to VBA and need help amending the following code, which inserts a column after each existing column:

Dim colx As Integer
For colx = 1 To 30 Step 2
Columns(colx).Insert Shift:=xlToRight
Next

This code only inserts 1 additional column in between each column. I'd like to insert 3. Any help would be greatly appreciated!

parttime_guy
12-20-2012, 07:40 PM
Hi ahc234,

Just run the macro twice you should get an extra column, but remember to increase the number of columns (eg, For colx = 1 To 60 Step 2)

If you would like to run the same through a macro a little tweek could help as below by calling the same macro twice eg below


Sub insertcol()
Dim colx As Integer
For colx = 2 To 60 Step 2
Columns(colx).Insert Shift:=xlToRight
Next
Call insertcol2
End Sub
Sub insertcol2()
Dim colx As Integer
For colx = 2 To 60 Step 2
Columns(colx).Insert Shift:=xlToRight
Next
End Sub

Hope this helps

Happy Excelling :friends:
Best regards

Paul_Hossler
12-20-2012, 08:12 PM
Option Explicit
Sub test()
Dim iCol As Long, iNum As Long
Application.ScreenUpdating = False

For iCol = 30 To 1 Step -1
For iNum = 1 To 3
ActiveSheet.Columns(iCol).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Next iNum
Next iCol
Application.ScreenUpdating = True
End Sub


Paul

ahc234
12-21-2012, 08:28 AM
Thank you for your help!