insert a column after last column with data

vijaycs

New Member
Joined
Aug 17, 2019
Messages
8
I want to insert a new column (with the name) after last column with data.

i tried using the below code by writing it in VBA module, however it does not work.


[
Sub add_column()


Dim LastRow As Long
Dim LastCol As Long
Dim iRow As Long
Set ws = Sheet1 ' NOTE: Change this if your data is not in Sheet1.

With ws
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

.Cells(1, LastCol + 1).Value = "XXXX"
.Cells(1, LastCol + 2).Value = "YYYY"
End With
End Sub
]
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
It works for me. How is it not working for you?
 
Upvote 0
Hi & welcome to MrExcel.
In what way doesn't it work?
 
Upvote 0
I want to insert a new column (with the name) after last column with data.

i tried using the below code by writing it in VBA module, however it does not work.

Code:
Sub add_column()
Dim LastRow As Long
    Dim LastCol As Long
    Dim iRow As Long
    Set ws = Sheet1   ' NOTE: Change this if your data is not in Sheet1.
    
    With ws
        LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        LastCol = .Cells([SIZE=3][B][COLOR=#ff0000]1[/COLOR][/B][/SIZE], .Columns.Count).End(xlToLeft).Column
        
        .Cells([B][COLOR=#FF0000]1[/COLOR][/B], LastCol + 1).Value = "XXXX"
        .Cells([B][COLOR=#FF0000]1[/COLOR][/B], LastCol + 2).Value = "YYYY"
    End With
End Sub

To make it work, you must put the row where you have data, if your headings are in row 2, then change the 1 to the 2.
 
Upvote 0
Hi,
How about this?

Code:
Sub add_column()


Dim LastRow As Long
Dim LastCol As Long
Dim iRow As Long
Set ws = Sheets(1) ' NOTE: Change this if your data is not in Sheet1.

With ws
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

.Columns(LastCol+1).EntireColumn.Insert
.Cells(1,LastCol+1).Value = "XXXX"
.Columns(LastCol+2).EntireColumn.Insert
.Cells(1, LastCol + 2).Value = "YYYY"
End With
End Sub
 
Upvote 0
In that case how about
Code:
Sub add_column()


Dim LastRow As Long
Dim LastCol As Long
Dim iRow As Long
[COLOR=#0000ff]Set ws = ActiveSheet[/COLOR]

With ws
   LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
   LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
   
   .Cells(1, LastCol + 1).Value = "XXXX"
   .Cells(1, LastCol + 2).Value = "YYYY"
End With
End Sub
This will work on whatever sheet is active
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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