+ Reply to Thread
Results 1 to 9 of 9

Selecting Range to last row with data

  1. #1
    Forum Contributor dagindi's Avatar
    Join Date
    06-02-2008
    Location
    New York, NY
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    295

    Smile Selecting Range to last row with data

    I cannot figure out the correct code that will replace

    Please Login or Register  to view this content.
    with the code that will select everything in column B starting with B2 and go all the way to the absolute last row with data (not just stop at the first row that might be blank).
    Last edited by dagindi; 04-20-2010 at 12:19 PM.

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Selecting Range to last row with data

    Try this

    Please Login or Register  to view this content.
    Cheers

  3. #3
    Forum Contributor dagindi's Avatar
    Join Date
    06-02-2008
    Location
    New York, NY
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    295

    Re: Selecting Range to last row with data

    Marcol,

    Worked like a charm! Thanks.

    Can you explain to me how the code knows to continue search for data even if it comes across a blank row?

  4. #4
    Registered User
    Join Date
    04-19-2010
    Location
    San Diego, CA USA
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Selecting Range to last row with data

    Please Login or Register  to view this content.

  5. #5
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Selecting Range to last row with data

    It doesn't

    LastRow = works up from the last cell in the column (LastRow is just a variable name)

    Assuming that iit is blank it will find the first cell with data and get the row number.

    1/. Rows.Count is the number of rows in a sheet
    Therefore
    Range("B" & Rows.Count).=Range("B65536"). in Excel 2003

    2/. .End(xlUp) is the direction to go this can also be (xlDown), (xlToRight) or (xlToLeft)

    3/. .Row is the row number


    Hope this helps.

    If you need any further information, please feel free to ask further

    However

    If this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody!
    Also
    If you are satisfied by any members response to your problem please consider using the scales icon top right of thier post to show your appreciation.
    Last edited by Marcol; 04-20-2010 at 12:16 PM. Reason: Typos

  6. #6
    Registered User
    Join Date
    04-20-2010
    Location
    leeds, uk
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Selecting Range to last row with data

    you can aslo use

    lastrow=activecell.SpecialCells(xlCellTypeLastCell).Row

    Range("B2:B" & lastrow).select

  7. #7
    Forum Contributor dagindi's Avatar
    Join Date
    06-02-2008
    Location
    New York, NY
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    295

    Re: Selecting Range to last row with data

    So if I am using Excel 2007 will the code start at Row 1048576?

  8. #8
    Registered User
    Join Date
    04-19-2010
    Location
    San Diego, CA USA
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Selecting Range to last row with data

    If you use
    Please Login or Register  to view this content.
    It will find the last used row. You can try either way and see which is faster for your macro

  9. #9
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Selecting Range to last row with data

    boringg and dmaesj

    Your solutions will work on some occasions

    They are both based on UsedRange

    Lets say Column B has 30 rows of data and your sheet also has Data in say Range("A500")

    Your solution will highlight Range("B2:B500") not Range("B2:B30")

    The solution I offered is almost universally used among Excel Users, best go with it.

    dagindi

    So if I am using Excel 2007 will the code start at Row 1048576?
    Yes it will work with any version of Excel (Except the recent Mac Versions)
    Last edited by Marcol; 04-20-2010 at 12:54 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1