You can use this code. I have tested it, complete with the inputbox and even the columns get numbered accordingly.
Sub insert_cols()
lastcol = Range("A1").End(xlToRight).Value
If lastcol = "Total No" Then
lastcol = Range("A1").End(xlToRight).Offset(0, -1).Value
Range("A1").End(xlToRight).Select
coladd = Left(ActiveCell.Address(1, 0), InStr(1, ActiveCell.Address(1, 0), "$") - 1)
colprev = Left(ActiveCell.Offset(0, -1).Address(1, 0), InStr(1, ActiveCell.Offset(0, -1).Address(1, 0), "$") - 1)
End If
userno = InputBox("Please provide the total number of columns required")
coldiff = userno - lastcol
For i = 1 To coldiff
Columns(colprev & ":" & colprev).Copy
Columns(coladd & ":" & coladd).Select
Selection.Insert shift:=xlToRight
lastcol = lastcol + 1
Range(coladd & "1").Value = Range(colprev & "1").Value + 1
colprev = coladd
coladd = Left(ActiveCell.Offset(0, 1).Address(1, 0), InStr(1, ActiveCell.Offset(0, 1).Address(1, 0), "$") - 1)
Next
End Sub
Thanks,
Arlette Aloysius
If this was useful then please rate it appropriately.
Bookmarks