Microsoft Excel Webinar

Results 1 to 4 of 4

Thread: select last 20 cells

  1. #1

    select last 20 cells

    vba code for selecting last 20 cells, if cells less than 20, say10
    select A2:B11.
    plse see attached file.
    Attached Files Attached Files
      To view attachments your post count must be 0 or greater. Your post count is 0 momentarily.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master xld's Avatar
    Joined
    Apr 2005
    Posts
    23,552
    Location
    One very direct way
    VB:
    Dim lastrow As Long 
    Dim selectnum As Long 
     
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row 
    Select Case True 
    Case lastrow > 20:  selectnum = 20 
    Case lastrow > 10:  selectnum = 10 
    Case lastrow > 5:   selectnum = lastrow 
    End Select 
     
    Cells(lastrow - selectnum + 1, "A").Resize(selectnum).Select 
    
    
    Formatting tags added by mark007
    ____________________________________________
    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

  3. #3

    select last 20 cells

    Quote Originally Posted by xld
    One very direct way
    VB:
    Dim lastrow As Long 
    Dim selectnum As Long 
     
    lastrow = Cells(Rows.Count, "A").End(xlUp).Row 
    Select Case True 
    Case lastrow > 20:  selectnum = 20 
    Case lastrow > 10:  selectnum = 10 
    Case lastrow > 5:   selectnum = lastrow 
    End Select 
     
    Cells(lastrow - selectnum + 1, "A").Resize(selectnum).Select 
    
    
    Formatting tags added by mark007
    thank you for quick reply.
    your vba select only A11:A30 but I want A11:B30

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master xld's Avatar
    Joined
    Apr 2005
    Posts
    23,552
    Location
    VB:
    Cells(lastrow - selectnum + 1, "A").Resize(selectnum, 2).Select 
    
    
    Formatting tags added by mark007
    ____________________________________________
    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

Posting Permissions

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