Adding Column to the Next Empty Column in a Worksheet while Maintaining Formatting

aryaden

Board Regular
Joined
Jun 9, 2021
Messages
101
Office Version
  1. 2019
Platform
  1. Windows
I am currently using the code below to insert a column near the last occupied cell.

VBA Code:
Sub xx()
Dim LastRow As Long
Dim LastCol As Long
Dim iRow As Long
Set ws = Sheet1 ' Data is 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 = "New Column Title"
End With
End Sub

Currently the output of this is (using example data)
Example Sheet.xlsm
ABCDEFGH
1DescriptionMondayTuesdayWednesdayThursdayFridaySaturdayNew Column Title
2111111
3AAAAXX
4BBBBXXX
5CCCCXX
6DDDDXXXX
7EEEEXX
8FFFFXXXX
9GGGGXX
10
Sheet1


I would like the output to be
Example Sheet.xlsm
ABCDEFGH
1DescriptionMondayTuesdayWednesdayThursdayFridaySaturdayNew Column Title
2111111
3AAAAXX
4BBBBXXX
5CCCCXX
6DDDDXXXX
7EEEEXX
8FFFFXXXX
9GGGGXX
10
11
Sheet1 (3)


Is there anyway to edit my code to achieve this?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try:
VBA Code:
Sub xxV2()
 Dim LastCol As Long
  With Sheet1
   LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
   .Cells(1, LastCol + 1) = "New Column Title"
   .Columns(LastCol).Copy
   .Columns(LastCol + 1).PasteSpecial xlFormats
  End With
End Sub
 
Upvote 0
Try:
VBA Code:
Sub xxV2()
 Dim LastCol As Long
  With Sheet1
   LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
   .Cells(1, LastCol + 1) = "New Column Title"
   .Columns(LastCol).Copy
   .Columns(LastCol + 1).PasteSpecial xlFormats
  End With
End Sub
Thank you so much, that worked for me! Do you know if there is a way to merge cells E1 and F1 (in this example) using the LastCol + 1 and LastCol +2 reference instead of E1 and F1?

Example would look like this:
Example Sheet.xlsm
ABCDEFGHI
1DescriptionMondayMondayTuesdayNew Column
212112
3AAAAXX
4BBBBXX
5CCCCX
6DDDDXX
7EEEEX
8FFFFX
9GGGGX
10
11
Sheet1
 
Upvote 0
Please, try this modified one.
VBA Code:
Sub xxV3()
 Dim LastCol As Long, k As Long
  Application.ScreenUpdating = False
  With Sheet1
   LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
   k = IIf(.Cells(1, LastCol).MergeCells, 2, 1)
   .Columns(LastCol).Resize(, k).Copy
   .Columns(LastCol + k).Resize(, 2).PasteSpecial xlFormats
   With .Cells(LastCol + k).Resize(, 2)
    .Merge
    .HorizontalAlignment = xlCenter
    .Value = "New Column Title"
    .Select
   End With
  End With
End Sub
 
Upvote 0
Solution
Please, try this modified one.
VBA Code:
Sub xxV3()
 Dim LastCol As Long, k As Long
  Application.ScreenUpdating = False
  With Sheet1
   LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
   k = IIf(.Cells(1, LastCol).MergeCells, 2, 1)
   .Columns(LastCol).Resize(, k).Copy
   .Columns(LastCol + k).Resize(, 2).PasteSpecial xlFormats
   With .Cells(LastCol + k).Resize(, 2)
    .Merge
    .HorizontalAlignment = xlCenter
    .Value = "New Column Title"
    .Select
   End With
  End With
End Sub
Thank you! That worked
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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