Mari2212
Mari2212

Reputation: 25

Add 10 entries from userform to sheet

I am looking for a way to shorten my code to input data from a form of 10 entries.

This is my userform with one RMA number (applies to all 10 PN), one customer name, 10 part numbers, and 10 serial numbers that go with each part number.
USERFORM10PARTNUMBERS

This is how I want data transferred to the worksheet.
SHEETDATA

The part number textboxes are named TB#.
The serial number textboxes are named SNTB#.

This is the code I have for the first entry. I was thinking of adding code to say "TB"&"i" and "SNTB"&"i", but I don't know where to place that statement or how to start it.

Private Sub EnterButton_Click()
                
    'this assigns receiving data to first columns of log Sheet          
                
    If TB1.Value = "" Then
                       
    Else
                
        Worksheets("RM Tracker").Activate
        Dim lastrow
                             
        lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
        lastrow = lastrow + 1
                            
        Cells(lastrow, 1) = RMATB.Value
        Cells(lastrow, 2) = CustCB.Value
        Cells(lastrow, 3) = TB1.Value
        Cells(lastrow, 4) = SNTB1.Value
        Cells(lastrow, 5) = ReceiveTB.Value
                             
        ActiveCell.Offset(1, 0).Select
                
   End If
                
   ActiveWorkbook.Save
   
   Call resetform
                
End Sub

                
Sub resetform()
                
    RMATB.Value = ""
    CustCB.Value = ""
    TB1.Value = ""
    SNTB1.Value = ""
                
    ReceiveTB = ""
                
    'sets focus on that first textbox again
    RecForm.RMATB.SetFocus     
                
End Sub

Upvotes: 0

Views: 72

Answers (1)

roses56
roses56

Reputation: 130

You can incorporate a for loop where "i" represents the row you are working with. When you are appending data you need to put that reference within the loop so the new row is recalculated.

 Private Sub EnterButton_Click()
                
                 
                'this assigns receiving data to first columns of log Sheet
                
                
                
                If TB1.Value = "" Then
                
                
                Else
                            
                            Worksheets("RM Tracker").Activate
                            dim i as long
                        For i = 1 To 10
                            
                             Dim lastrow as long ' should put a data type with dim statements
                             
                             lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
                             lastrow = lastrow + 1
                            
                             Cells(lastrow, 1) = Userform1.Controls("RMATB" & i).Value ' change userform name to fit your need
                             Cells(lastrow, 2) = Userform1.Controls("CustCB" & i).Value
                             Cells(lastrow, 3) = Userform1.Controls("TB1" & i).Value
                             Cells(lastrow, 4) = Userform1.Controls("SNTB1" & i).Value
                             Cells(lastrow, 5) = Userform1.Controls("ReceiveTB" & i).Value
                             
                       Next i
                 
                
                End If
                            
                ActiveWorkbook.Save
                
                 
                Call resetform
                
                End Sub
                
                 
                
                
        Sub resetform()
                
                
                RMATB.Value = ""
                CustCB.Value = ""
                TB1.Value = ""
                SNTB1.Value = ""
                
                ReceiveTB = ""
                
                'sets focus on that first textbox again
                RecForm.RMATB.SetFocus

Upvotes: 1

Related Questions