【问题标题】:Enter data from 10 textboxes, but do NOT submit empty textboxes values into cells (using excel vba userform)从 10 个文本框中输入数据,但不要将空文本框值提交到单元格中(使用 excel vba 用户表单)
【发布时间】:2020-08-05 13:45:20
【问题描述】:

我想将大约 10 个文本框用户表单中的数据输入 Excel 工作表,仅用于已填写的文本框。

我目前有从用户表单填充单元格的代码,它可以工作,但如果用户没有填写其余的文本框,它仍然会输入这些空白数据条目。

当前状态:

未来状态: 仅填充了 2 行...

我如何告诉代码说明文本框 1-10 中的任何字段是否为空不输入...仅输入在此 RMA# 和客户名称下填写的文本框??

谢谢!

这是目前的代码(感谢之前问题的帮助):


                Private Sub EnterButton_Click()
                
                
                
                           
                If TB1.Value = "" Then
               
                Else
                
                        Worksheets("RMA 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) = RecForm.Controls("RMATB").Value 
                        Cells(lastrow, 2) = RecForm.Controls("CustCB").Value
                        Cells(lastrow, 3) = RecForm.Controls("TB" & i).Value
                        Cells(lastrow, 4) = RecForm.Controls("SNTB" & i).Value
                        Cells(lastrow, 5) = RecForm.Controls("ReceiveTB").Value
                             
                Next i
                        
                 
                 
                 
                 
                
                End If
                            
                ActiveWorkbook.Save
                
                 
                Call resetform
                
                End Sub



        Sub resetform()
                
                
                RMATB.Value = ""
                CustCB.Value = ""
                TB1.Value = ""
                SNTB1.Value = ""
                TB2.Value = ""
                SNTB2.Value = ""
                TB3.Value = ""
                SNTB3.Value = ""
                TB4.Value = ""
                SNTB4.Value = ""
                TB5.Value = ""
                SNTB5.Value = ""
                TB6.Value = ""
                SNTB6.Value = ""
                TB7.Value = ""
                SNTB7.Value = ""
                TB8.Value = ""
                SNTB8.Value = ""
                TB9.Value = ""
                SNTB9.Value = ""
                TB10.Value = ""
                SNTB10.Value = ""
                
                ReceiveTB = ""
                
                'sets focus on that first textbox again
                RecForm.RMATB.SetFocus
                
                
        End Sub

【问题讨论】:

    标签: excel vba userform


    【解决方案1】:

    在循环内,添加一个检查以查看条目是否为空。像这样的:

       For i = 1 To 10
           If Trim(RecForm.Controls("TB" & i).Value) <> "" And Trim(ecForm.Controls("SNTB" & i).Value) <> "" Then
                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) = RecForm.Controls("RMATB").Value
                Cells(lastrow, 2) = RecForm.Controls("CustCB").Value
                Cells(lastrow, 3) = RecForm.Controls("TB" & i).Value
                Cells(lastrow, 4) = RecForm.Controls("SNTB" & i).Value
                Cells(lastrow, 5) = RecForm.Controls("ReceiveTB").Value
           End If
       Next i
    

    【讨论】:

      【解决方案2】:

      第二个选项是在找到第一个空文本框时退出 For,如下所示:

         For i = 1 To 10
             If Trim(RecForm.Controls("TB" & i).Value) = "" And Trim(ecForm.Controls("SNTB" & i).Value) = "" Then 
                  GoTo MyNextCode 
              End If
              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) = RecForm.Controls("RMATB").Value
              Cells(lastrow, 2) = RecForm.Controls("CustCB").Value
              Cells(lastrow, 3) = RecForm.Controls("TB" & i).Value
              Cells(lastrow, 4) = RecForm.Controls("SNTB" & i).Value
              Cells(lastrow, 5) = RecForm.Controls("ReceiveTB").Value
          Next i
      MyNextCode:
      ...
      

      对于这么多的控​​件,这不会有太大的区别,但是如果您有更多控件,则此方法将在第一个空控件时退出,从而防止代码必须遍历所有控件(在这种情况下)控制... .
      此解决方案的另一个好处是您可以防止例如PN1,PN2,然后突然 PN5(当然不知道这是否相关......)。
      根据经验,我知道如果您要处理大量数据,使用第二个选项会更开心,但 option1 非常适合您拥有的数据量,并且也适用于大量数据或控件虽然速度较慢...

      【讨论】:

      • Exit For代替GoTo MyNextCode
      • @Brian 执行 Exit For 和 GoTo 之间有显着区别吗?
      • 我想它完成了同样的事情,但为什么不使用为此目的存在的语句呢?此外,大多数开发人员出于任何原因不赞成使用过时的 GoTo 语句。 GoTo 有一些用例,但这不是其中之一。
      猜你喜欢
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多