Insert columns VBA multiple

nmc

New Member
Joined
Aug 25, 2022
Messages
38
Office Version
  1. 2021
Platform
  1. Windows
Hello

I'm a newbie and I'm struggling for reach out the solution

I want introduce 3 columns between each existing column in a range.
Note: this range is dynamic. sometimes it has 5 cells other times 10.

Example: it starts on cell O5 and I want insert three columns between O5 and P5 and so on...
And the I need to merge four cells -> example O5 and the three columns that were insert xlRight.

I tried to do a loop but it doesn't work

Dim startcell As Integer
startcell = 5
Do While range("O" & startcell).Value <> ""
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
startcell = startcell + 3
Loop


Can someone help me?
Thanks
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Just to keep you going, this macro will insert 3 empty columns in between the columns. If you need to copy contents from the previous column just add the parameter CopyOrigin: :
VBA Code:
Option Explicit
Sub Insert3Columns()
    Dim x      As Long
    Dim LC     As Long
    LC = Cells(5, Cells.Columns.Count).End(xlToLeft).Column 'find last used column in row 5
    For x = LC To 16 Step -1                      'from last column to column P (16)
        Cells(5, x).Resize(, 3).EntireColumn.Insert Shift:=xlToRight   ', CopyOrigin:=xlFormatFromLeftOrAbove
    Next x
End Sub
As for merging cells I'll leave it to you, I hate merged cells ;).
 
  • Like
Reactions: nmc
Upvote 0
Thanks for your help it worked!!

I'm struggling till know to merge the cells :(
 
Upvote 0
I'm struggling till know to merge the cells
Note that merged cells in Excel are a really, REALLY bad idea.
Merged cells are probably the absolute worst feature of Excel.
They cause nothing but issues for things like sorting and VBA.
As such, many advanced programmers will not touch nor have anything to do with merged cells!
 
  • Like
Reactions: nmc
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,031
Members
448,940
Latest member
mdusw

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