Find last column with data

  • Thread starter Thread starter poppy
  • Start date Start date
P

poppy

Hi Experts

I am trying to find the last column with data in my sheet. I tried thi
code, but it is giving me errors. Do you have any ideas what I'm doin
wrong?

lastcol = Cells(Columns.Count, "2").End(xlLeft).Column

Kind Regard
 
Hi Poppy,
lastcol = Cells(Columns.Count, "2").End(xlLeft).Column

If you use a numeric column reference, drop the quotes.

The Cells syntaax should be:

Cells(row, column)

To find the last populated cell in row 2, try amending your code to:

Set lastcol = Cells(2, Columns.Count).End(xlToLeft)


To return the number of the find the last populated column in row 2, try:

Cells(2, Columns.Count).End(xlToLeft).Column


To find the last populated column on the worksheet, try the following
function:

Function LastCol(sh As Worksheet)
On Error Resume Next
LastCol = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top