Consulting

Results 1 to 6 of 6

Thread: Solved: Find the last cell in my spreadsheet.

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    Solved: Find the last cell in my spreadsheet.

    This is what I have so far:

    [VBA]Dim totalrow, lastColumn As Long
    Dim finalCell As Range

    totalrow = Cells(65536, 1).End(xlUp).Offset(1, 0).Row
    lastColumn = Cells(1, 100).End(xlToLeft).Column
    finalCell = Range(totalrow, lastColumn)[/VBA]

    it is giving me an error on the finalCell line.

  2. #2

  3. #3
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi again,

    When assigning a cell to a range variable, you need to use the word Set as the type Range is an object:[vba]Set finalCell = Range(totalrow, lastColumn)[/vba]Also, you'll need to make a slight change to your dim statements:[vba]'Dim totalrow, lastColumn As Long 'cant do it this way, need to specify each
    Dim totalrow As Long, lastColumn As Long[/vba]
    Some other things to keep in mind:
    -Your uses of End will only work if something is in row 1 for the last column, and something is in column A for the last row, otherwise you'll miss some data
    -Instead of using 65536, you might want to use Rows.Count as the next version of excel will allow over a million rows.
    -Similarly, instead of using 100 for the column, you could use Columns.Count (256 currently, many many more for the next version)
    You could also take a look at http://vbaexpress.com/kb/getarticle.php?kb_id=417 and http://vbaexpress.com/kb/getarticle.php?kb_id=418 to see how to use the Find method of determining the last used row/column, makes things much more useful for sheets without a specific setup.
    Matt

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    LOL! I'll stop doing that now Matt.

  5. #5
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Whats that, posting before me?

  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Yeah, same solutions.

Posting Permissions

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