Find last column with Data

olympiac

Board Regular
Joined
Sep 26, 2010
Messages
158
I need to find the last column with data in a spreradsheet and add 2 new columns on the right considering that the headings will start from row 3

How can I modify this code to get the result?

Sub FindLastColumn()
Dim LastColumn As Integer
If WorksheetFunction.CountA(Cells) > 0 Then
'Search for any entry, by searching backwards by Columns.
LastColumn = Cells.Find(What:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column

End If
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try:
Code:
Sub FindLastColumn()

Dim LastColumn As Integer

If WorksheetFunction.CountA(Cells) > 0 Then _
  LastColumn = Cells(3,Columns.Count).End(xlToLeft).Column

End Sub
 
Upvote 0
Thanks for your answer
What about now if I want to add the heading "bal bla bla" in the new column?
 
Upvote 0
Try:
Code:
Sub FindLastColumn()
 
Dim LastColumn As Integer
 
Application.ScreenUpdating = False
 
If WorksheetFunction.CountA(Cells) > 0 Then _
  LastColumn = Cells(3,Columns.Count).End(xlToLeft).Column
 
Cells(3, LastColumn + 1) = "Your new heading"
Cells(3, LastColumn + 2) = "Next to your new heading"
 
Application.ScreenUpdating = True
 
End Sub
 
Upvote 0
Last more thing.

Do you know how to enrich the code to copy the format from the last column to the new columns added?
 
Upvote 0
Try:
Code:
Sub FindLastColumn()
 
Dim LastColumn As Integer
 
Application.ScreenUpdating = False
 
If WorksheetFunction.CountA(Cells) > 0 Then _
  LastColumn = Cells(3, Columns.Count).End(xlToLeft).Column
 
Cells(3, LastColumn).Copy
 
With Cells(3, LastColumn + 1)
    .PasteSpecial Paste:=xlPasteFormats
    .Value = "Your new hearding"
End With
 
With Cells(3, LastColumn + 2)
    .PasteSpecial Paste:=xlPasteFormats
    .Value = "Next to your new heading"
End With
 
With Application
    .ScreenUpdating = True
    .CutCopyMode = False
End With
 
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,835
Members
452,947
Latest member
Gerry_F

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