Consulting

Results 1 to 6 of 6

Thread: Solved: Find Last used Column

  1. #1
    VBAX Regular
    Joined
    May 2006
    Posts
    35
    Location

    Solved: Find Last used Column

    I am looking for a way to find the last used column so I am able to insert text into the next empty column to the right.

  2. #2
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    From MWE's KB entry:

    [VBA] Function xlLastCol(Optional WorksheetName As String) As Long

    ' finds the last populated col in a worksheet

    If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name
    With Worksheets(WorksheetName)
    On Error Resume Next
    xlLastCol = .Cells.Find("*", .Cells(1), xlFormulas, _
    xlWhole, xlByColumns, xlPrevious).Column
    If Err <> 0 Then xlLastCol = 0
    End With

    End Function
    [/VBA]
    Peace of mind is found in some of the strangest places.

  3. #3
    VBAX Regular
    Joined
    May 2006
    Posts
    35
    Location
    Thank you!

  4. #4
    Here's an oversimplified technique that assumes the last column has a value in the row (CurrRow) that is searched:

    Range("IV" & CurrRow).End(xlToLeft).Offset(0, 1).Select

    Not elegant, but is useful sometimes.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Don't forget SpecialCells
    LastCol = Cells.SpecialCells(xlCellTypeLastCell).Column
    LastRow = Cells.SpecialCells(xlCellTypeLastCell).Row
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Regular
    Joined
    May 2006
    Posts
    35
    Location
    Quote Originally Posted by mdmackillop
    Don't forget SpecialCells
    LastCol = Cells.SpecialCells(xlCellTypeLastCell).Column
    LastRow = Cells.SpecialCells(xlCellTypeLastCell).Row
    FYI, When I did this one (tried first) It gave me 256 as the answer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •