+ Reply to Thread
Results 1 to 11 of 11

How to select the last cell in a currently selected range

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    09-04-2007
    Location
    Ontario, Ca
    Posts
    624

    How to select the last cell in a currently selected range

    The following code selects all cells from Q12 through the last data row of database.

    Let's assume the last row of data is row 300. After the cells of Q12 thru Q300 are selected, I want the active cell to be 1 cell below the last data row. The previous selection is then no longer needed.

    My request is for a diffeent need, than what this code is used for else where..

    Sub SelectAlleMails()
    
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
        Set Wks = Worksheets("email list")
        Set Rng = Wks.Range("Q12") 
        Set RngEnd = Wks.Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False)
        If Not RngEnd Is Nothing Then
           Set Rng = Rng.Resize(RngEnd.Row - Rng.Row + 1)
           Rng.Select
        Else
           MsgBox "Range is Empty."
        End If
        Selection.Copy
    End Sub
    Thanks for the help
    Matt
    Last edited by Launchnet; 11-20-2011 at 05:11 PM.
    Thanks for helping . . .
    Matt @ Launchnet

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: How to select the last cell in a currently selected range

    Try this,

    Sub SelectFirstCellAfterLastCell()
    
    Dim Rws As Long, Rng As Range
    
    
    Rws = Cells(Rows.Count, "Q").End(xlUp).Row + 1
    
    Set Rng = Cells(Rws, 17)
    
    Rng.Select
    
    
    End Sub

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How to select the last cell in a currently selected range

    A quick way to select the first empty cell in column Q by looking UP from the bottom of the worksheet:
    Range("Q" & Rows.Count).End(xlUp).Offset(1).Select
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Valued Forum Contributor
    Join Date
    09-04-2007
    Location
    Ontario, Ca
    Posts
    624

    Re: How to select the last cell in a currently selected range

    Thanks for the try. I forgot to say that there are blank cells in various cells, so I can't use either method. I happen to know these two ways, but I don't know how to get around the blank cells. The sample code I'm using selects all cells including empty cells, all the way down to and including the last data row of the database.

    Please take another look

    Thanks

    Matt

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: How to select the last cell in a currently selected range

    Hello Matt,

    A minor change is all that is needed.
    Sub SelectAlleMails()
    
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
    
        Set Wks = Worksheets("email list")
        Set Rng = Wks.Range("Q12") 
        Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
    
        If RngEnd.Row < Rng.Row  Then
           MsgBox "Range is Empty."
        Else
           RngEnd.Offset(1,0).Select
        End If
      
    End Sub
    Last edited by Leith Ross; 11-20-2011 at 01:45 AM.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Valued Forum Contributor
    Join Date
    09-04-2007
    Location
    Ontario, Ca
    Posts
    624

    Re: How to select the last cell in a currently selected range

    Good Morning Leith . . . Good To See Your Name

    Only 1 problem . . .

    Column Q does not always have data in any particular cell. There currently happens to be 8 blank cells in column Q at the bottom. Some cells in Column Q will never have data. This causes me a problem, as I need to have the last data row, in column Q, selected. Then, I can move the selection down 1 cell and then move it to column "A" on the same blank row or record.

    I need this macro so that I can add a new record in the 1st row after the current data row.

    Hope that I have explained this to where it can be understood.

    Thanks Much

    Matt

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: How to select the last cell in a currently selected range

    Hello Matt,

    You are up early this morning. If "Q" extends to the end of the database then maybe this change is what you need. This will select one row below the last row of the database.
    Sub SelectAlleMails()
    
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
    
        Set Wks = Worksheets("email list")
        Set Rng = Wks.Range("Q12") 
        Set RngEnd = Wks.Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False)
    
        If Not RngEnd Is Nothing Then
           Set Rng = Wks.Cell(RngEnd.Row + 1, "G")
           Rng.Select
        Else
           MsgBox "Range is Empty."
        End If
    
    End Sub

+ 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