PDA

View Full Version : Dynamically add column to right of last column



Andy2011
08-24-2015, 12:02 PM
Hi

I have used the following code to dynamically add a column to the right of the last column at the time of running the code. It seems to work, however if I then delete one or more of the columns the macro will not work again unless I save the spreadsheet first.


Sub InsertColumn()

Dim lastColumn As Long
lastColumn = ActiveSheet.Range("A1").SpecialCells(xlCellTypeLastCell).Column
Columns(lastColumn).Copy Destination:=Columns(lastColumn + 1)

End Sub



Anyone got any ideas why?

mancubus
08-24-2015, 02:10 PM
repeat detecting the last column.

Andy2011
08-25-2015, 01:56 PM
Sorry can you please explain as I don't understand. Apologies for being thick.

JKwan
08-25-2015, 07:17 PM
run this line again to get the last column again


lastColumn = ActiveSheet.Range("A1").SpecialCells(xlCellTypeLastCell).Column

snb
08-25-2015, 11:39 PM
with cells(1,columns.count).end(xltoleft).entirerow
.offset(,1).value=.value
end with

chlin
08-27-2015, 05:40 AM
Hi snb,

It should be



With Cells(1, Columns.Count).End(xlToLeft).EntireColumn
.Offset(, 1).Value = .Value
End With

snb
08-27-2015, 09:33 AM
@chlin

You are right !