Consulting

Results 1 to 8 of 8

Thread: How to Go to Next Row on a UserForm

  1. #1

    How to Go to Next Row on a UserForm

    Hi all,

    I'm new to VBA and I have to create a userform that asks the user for FirstName, LastName, and Uniqname and also has a cancel button that closes the program. I have got that down as follows:

    Private Sub btncancel_Click()
    Me.Hide
    End Sub
    
    
    Private Sub buttonAdd_Click()
    Cells(2, 1) = txtFirstName.Text
    Cells(2, 2) = txtLastName.Text
    Cells(2, 3) = txtUniqname.Text
    Columns("A:C").EntireColumn.AutoFit
    
    
    txtFirstName = ""
    txtLastName = ""
    txtUniqname = ""
    
    
    
    
    End Sub
    However, once the user has entered the information, I need to input the second set of data into the next row. Right now, if I enter a set of data into the userform, then add another set of data, it just occupies the same cells as the ones before. What code do I use so that new data is inputed into the next row? Any help is appreciated.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Dim NextRow as Long
    NextRow = Worksheet (?).Cells(Rows.Count, 1).End(xlUp).Row + 1
    Cells(nextRow, 1) = ...
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Perhaps this will work for you

    Private Sub buttonAdd_Click()
        With Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
            .Cells(1, 1) = txtFirstName.Text 
            .Cells(1, 2) = txtLastName.Text 
            .Cells(1, 3) = txtUniqname.Text
        End With 
        Columns("A:C").EntireColumn.AutoFit 
         
         
        txtFirstName = "" 
        txtLastName = "" 
        txtUniqname = "" 
         
    End Sub

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    That is a cool technique.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    It works as long as Row 1 is not blank. Puts it on Row 2 instead

    Non-UF example

    Sub test()
        Dim txtFirstName      As String
        Dim txtLastName    As String
        Dim txtUniqname    As String
        
        txtFirstName = "John"
        txtLastName = "Smith"
        txtUniqname = "Smith, John"
        
        With Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
            .Cells(1, 1) = txtFirstName
            .Cells(1, 2) = txtLastName
            .Cells(1, 3) = txtUniqname
        End With
        Columns("A:C").EntireColumn.AutoFit
         
         
        txtFirstName = ""
        txtLastName = ""
        txtUniqname = ""
         
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    The OP started in row two. Sounds like there is a header row already present.

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    Quote Originally Posted by mikerickson View Post
    The OP started in row two. Sounds like there is a header row already present.
    Your assumption is probably correct, but I've seen people leave row 1 empty just to have a little space at the top
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    VBAX Regular
    Joined
    Feb 2016
    Location
    Bangalore,India
    Posts
    11
    Location
    This will be the dynamic solution for this problem i hope so

    sub add_data()

    dim f_name,l_name,u_id as string
    'change the userform1 and textbox names as you given

    f_name=userform1.textbox1.value
    l_name=userform1.textbox2.value
    u_id=userform1.textbox3.value

    'am assuming sheet name as sheet1 change as you given
    thisworkbook.activate
    sheet1.select

    range("a1000000").end(xlup).offset(1,0).select
    activecell.value=f_name
    activecell.offset(0,1).value=l_name
    activecell.offset(0,2).value=u_id

    thisworkbook.save

    unlode userform1

    End sub

Posting Permissions

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