【问题标题】:Multiple textbox value to one cell多个文本框值到一个单元格
【发布时间】:2019-08-15 03:48:00
【问题描述】:

我有一个用户表单,在用户表单中有一个带有 4 个文本框的框架,我如何将这 4 个文本框的值传递到一个单元格中?用逗号或空格分隔。

我尝试在提交按钮中执行以下操作。

Dim t As MSForms.Control

For Each t In Me.Frame1.Controls
    If TypeOf t Is MSForms.TextBox Then
        If IsEmpty(stCode1Box) Then
        Exit For
        End If

        If stCode1Box Is Nothing Then
           'Cells(emptyRow, 15).Value = stCode1Box.Value
        ElseIf Not IsEmpty(stCode1Box) Then
           Cells(emptyRow, 15).Value = stCode1Box.Value
        ElseIf stCode2Box Is Nothing Then
           'Cells(emptyRow, 15).Value = stCode1Box.Value & ", " & stCode2Box.Value
        ElseIf Not IsEmpty(stCode2Box) Then
           Cells(emptyRow, 15).Value = stCode1Box.Value & ", " & stCode2Box.Value
        ElseIf stCode3Box Is Nothing Then
           'Cells(emptyRow, 15).Value = stCode1Box.Value & ", " & stCode2Box.Value & ", " & stCode3Box.Value
        ElseIf Not IsEmpty(stCode3Box) Then
           Cells(emptyRow, 15).Value = stCode1Box.Value & ", " & stCode2Box.Value & ", " & stCode3Box.Value
        ElseIf stCode4Box Is Nothing Then
           'Cells(emptyRow, 15).Value = stCode1Box.Value & ", " & stCode2Box.Value & ", " & stCode3Box.Value & ", " & stCode4Box.Value
        ElseIf Not IsEmpty(stCode4Box) Then
           Cells(emptyRow, 15).Value = stCode1Box.Value & ", " & stCode2Box.Value & ", " & stCode3Box.Value & ", " & stCode4Box.Value

        End If
    End If
Next t

结果会在该单元格上弹出,如果有多个文本框有值,则会用“,”逗号分隔。

【问题讨论】:

    标签: vba textbox cell frame userform


    【解决方案1】:

    未经测试:

    Dim t As MSForms.Control, v
    v = ""
    For Each t In Me.Frame1.Controls
        If TypeOf t Is MSForms.TextBox Then
            v = v & iif(v <> "", "," , "") & Trim(t.Value)
        End If
    Next t
    Cells(emptyRow, 15).Value = v
    

    【讨论】:

      【解决方案2】:

      简单试试

      Cells(emptyRow, 15).Value = Cells(emptyRow, 15).Value & "," & stCode1Box.Value
      

      【讨论】:

        【解决方案3】:

        您可以像已经拥有的那样循环您的控件,而不是使用一系列 if...elseif 语句,您可以检查 texbox.value 是否不是 "",将值添加到数组中,然后加入数组被你喜欢的任何东西分开。

        请参阅下面的示例,它将您的值写入单元格 C5 Sheet1,假设您的 userform 有一个名为 cmdSubmit 的 commandbutton(这将适用于任意数量的 textboxes):

        示例

        Private Sub cmdSubmit_Click()
        
            Dim temp As Variant
            Dim c As Control
            Dim myCount As Long
        
            '0 based array starting with a single element
            ReDim temp(0 To 0)
            myCount = 0
        
            'Check each control for a textbox
            For Each c In Me.Frame1.Controls
                If TypeOf c Is MSForms.TextBox Then
                    'if there is a value in the texbox then assign it to an array
                    If c.Value <> "" Then
                        temp(myCount) = c.Value
                        myCount = myCount + 1
                        'set upperbound of the array +1 when a new value is found
                        ReDim Preserve temp(0 To UBound(temp) + 1)
                    End If
                End If
            Next c
            myCount = 0
        
            'Remove the last array element as it must be blank
            ReDim Preserve temp(0 To UBound(temp) - 1)
        
            'Create a string of each value joined with a comma and space
            Dim myString As String
            myString = Join(temp, ", ")
        
            ThisWorkbook.Sheets(1).Range("C5").Value = myString
        
        End Sub
        

        参考文献

        1. Array Function
        2. Join Function

        【讨论】:

        • 最近有人向我指出,所以我应该提一下,将数组的上限设置为其最大潜力会更有效,例如ReDim temp (0 to Me.Frame1.Controls.Count,然后运行循环并填充数组。不要在循环之后设置myCount = 0,而是保留它的值并且只保留一次ReDim Preserve temp (0 to myCount - 1)。这将删除所有空数组元素,并为您留下一个充满值的数组,并返回与上述相同的结果。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-31
        • 1970-01-01
        • 2017-01-09
        • 2017-05-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多