+ Reply to Thread
Results 1 to 4 of 4

Focus on Last Row

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-13-2013
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    464

    Lightbulb Focus on Last Row

    I have the following code:


    Private Sub cmdSubmit_Click()
    
    Dim lastrow As Long
    
    With Sheets("DATABASE")
      lastrow = .Range("A2").End(xlDown).Row
      .Cells(lastrow + 1, 1).Value = txtLastName.Text & ", " & txtFirstName.Text
      .Cells(lastrow + 1, 2).Value = txtCCN.Text
      .Cells(lastrow + 1, 3).Value = txtDateofhire.Text
      .Cells(lastrow, 4).Resize(1, 6).Copy .Cells(lastrow + 1, 4)
        
    Sheets("DATABASE").Cells(lastrow).Show
    End With
    Application.CutCopyMode = False
    
    txtFirstName = ""
    txtLastName = ""
    txtCCN = ""
    txtDateofhire = ""
    
    End Sub
    and I would like when the Command Button is clicked for the DATABASE sheet to show with the focus on the last row. I thought

    Sheets("DATABASE").Cells(lastrow).Show

    would do it, but apparently not.

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Focus on Last Row

    Try:

    With Sheets("DATABASE")
      lastrow = .Range("A2").End(xlDown).Row
      .Cells(lastrow + 1, 1).Value = txtLastName.Text & ", " & txtFirstName.Text
      .Cells(lastrow + 1, 2).Value = txtCCN.Text
      .Cells(lastrow + 1, 3).Value = txtDateofhire.Text
      .Cells(lastrow, 4).Resize(1, 6).Copy .Cells(lastrow + 1, 4)
        
      .Select
      .Cells(lastrow, 1).Select
    End With
    Edit: you can try with this if you want the focus to be somewhere in the middle (vertically) of the screen
      ...
      ...
        
      .Select
      .Cells(1, 1).Select
      .Cells(Application.ActiveWindow.VisibleRange.rows.Count + lastrow, 1).Select
      .Cells(lastrow, 1).Select
    End With
    Last edited by millz; 03-03-2014 at 10:59 PM.
    多么想要告诉你 我好喜欢你

  3. #3
    Forum Contributor
    Join Date
    11-13-2013
    Location
    Florida
    MS-Off Ver
    Excel 2010
    Posts
    464

    Re: Focus on Last Row

    Quote Originally Posted by millz View Post
    Try:

    With Sheets("DATABASE")
      lastrow = .Range("A2").End(xlDown).Row
      .Cells(lastrow + 1, 1).Value = txtLastName.Text & ", " & txtFirstName.Text
      .Cells(lastrow + 1, 2).Value = txtCCN.Text
      .Cells(lastrow + 1, 3).Value = txtDateofhire.Text
      .Cells(lastrow, 4).Resize(1, 6).Copy .Cells(lastrow + 1, 4)
        
      .Select
      .Cells(lastrow, 1).Select
    End With
    Edit: you can try with this if you want the focus to be somewhere in the middle (vertically) of the screen
      ...
      ...
        
      .Select
      .Cells(1, 1).Select
      .Cells(Application.ActiveWindow.VisibleRange.rows.Count + lastrow, 1).Select
      .Cells(lastrow, 1).Select
    End With
    Worked perfectly! Thank you so much.

  4. #4
    Registered User
    Join Date
    08-06-2013
    Location
    Quebec
    MS-Off Ver
    Excel 2013
    Posts
    31

    Re: Focus on Last Row

    You could try:

    Cells(Application.Rows.Count, 1).End(xlUp).Select
    The "1" being the number of one of your columns.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] How to put focus to IE
    By keymuu in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-11-2012, 01:09 AM
  2. Who's got focus
    By Mats Samson in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-20-2011, 04:37 AM
  3. Set Focus
    By rvc81 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-04-2010, 12:01 PM
  4. [SOLVED] focus
    By Capp in forum Excel General
    Replies: 2
    Last Post: 11-18-2005, 03:30 PM
  5. [SOLVED] Focus
    By David Unger in forum Excel Formulas & Functions
    Replies: 20
    Last Post: 03-21-2005, 03:06 PM

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