Consulting

Results 1 to 4 of 4

Thread: Solved: Difference between 2 count last row/column command

  1. #1

    Solved: Difference between 2 count last row/column command

    hi

    I'm wondering what's the difference between these 2 code?

    [VBA]
    lrow = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
    [/VBA]

    and this

    [VBA]
    lrow = ActiveSheet.Range("A65536").End(xlUp).Row
    [/VBA]

    since these 2 are to count last row then what about counting column? for the first code, will it work if I just change the rows to columns?

    [VBA]
    lrow = ActiveSheet.Range("A" & ActiveSheet.Columns.Count).End(xlUp).Column
    [/VBA]

    Please advise.

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    The difference between the codes is that
    lrow = ActiveSheet.Range("A" & ActiveSheet.Rows.Count).End(xlUp).Row
    will find the last row in column A that has an entry.
    lrow = ActiveSheet.Range("A65536").End(xlUp).Row
    will find the last row above 65536 that has an entry. Before 2007 came out there was no practical difference.

    To find the last column in row 1, I would use this code.
    LCol = ActiveSheet.Cells(1,ActiveSheet.Columns.Count).End(xlToLeft).Column

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Rows.Count is a built-in constant, it is the number of rows in a worksheet. In Excel pre-2007, that is 65536, in 2007 it is 1048576.

    Your first bit of code cries out for a With clause

    [vba]

    With ActiveSheet
    lrow = .Range("A" & .Rows.Count).End(xlUp).Row
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    i see thanks a lot!

Posting Permissions

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