【问题标题】:Add 10 entries from userform to sheet将用户表单中的 10 个条目添加到工作表
【发布时间】:2020-09-09 18:36:23
【问题描述】:

我正在寻找一种方法来缩短我的代码以从 10 个条目的形式输入数据。

这是我的用户表单,其中包含一个 RMA 编号(适用于所有 10 个 PN)、一个客户名称、10 个部件号以及每个部件号附带的 10 个序列号。

这就是我希望将数据传输到工作表的方式。

部件号文本框命名为 TB#。
序列号文本框命名为 SNTB#。

这是我第一个条目的代码。我正在考虑添加代码来表示“TB”&“i”和“SNTB”&“i”,但我不知道在哪里放置该语句或如何开始。

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

【问题讨论】:

    标签: excel vba userform


    【解决方案1】:

    您可以合并一个 for 循环,其中“i”代表您正在使用的行。当您追加数据时,您需要将该引用放入循环中,以便重新计算新行。

     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
    

    【讨论】:

    • 谢谢!我如何告诉代码说“如果任何文本框为空白,请不要输入该数据..”??
    猜你喜欢
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    相关资源
    最近更新 更多