Matt
Matt

Reputation: 1

How to add multiple columns between each column?

My code adds one column after each existing column.

I need to add 14 columns. I want this to start by adding the columns after column 2 for each column with data. I believe my current code covers that.

Dim z As Integer
Columns(2).Select
For z = 2 To 20
    ActiveCell.EntireColumn.Insert
    ActiveCell.Offset(0, 2).Select
Next z

Upvotes: 0

Views: 393

Answers (2)

Naresh
Naresh

Reputation: 3034

Welcome.... Your code inserts 19 columns.. As you have selected second column First column is inserted between A and B. Now if you want to insert first columns between B and C then Select third column first. and then Z from 4 to 17..Oh you mean 14 columns each time after col B? Then...

Dim z As Integer
  Columns(3).Select
  For z = 4 To 22
    Range(ActiveCell, ActiveCell.Offset(0, 13)).EntireColumn.Insert
    'ActiveCell.EntireColumn.Insert
    ActiveCell.End(xlToRight).Offset(0, 1).Select
    'ActiveCell.Offset(0, 2).Select
  Next z

Upvotes: 0

SJR
SJR

Reputation: 23081

Loop backwards, and no need to Select.

This doesn't check if there is any data in the column.

Sub x()

Dim z As Long

For z = 20 To 2 Step -1
  Columns(z).Resize(, 14).Insert
Next z

End Sub

Upvotes: 2

Related Questions