输入选择到单个文本框vba的多个复选框的值

时间:2016-12-29 14:30:23

标签: excel vba excel-vba

我有多个复选框和一个文本框。如何在VBA Userform中为该单个文本框添加所有复选框的值? 我的表格看起来像这样..

enter image description here

2 个答案:

答案 0 :(得分:2)

总统的IIF:

Textbox1.Value=iif(chk1,"AccountBlaBla","") & iif(chk2,"Orders","") & _
iif(chk3,"ReturningBlaBla","")

等。 IIF是一个很好的内联IF替代方案,你可以在连接中使用它。如果checkbox1的值为true,则在连接中添加一些文本等。您可能希望使用单独的变量来组合文本,可能更容易在眼睛上...

答案 1 :(得分:0)

解决方案方法:
这将为您提供获取厚箱的值的功能,因为它是一个功能,根据需要使用它 的
代码:

Option Base 1
Function Array_Ticked_Checkboxes() As String()
Dim ItemControl As Control
Dim DummyArray() As String
Dim CounterDummy As Long
    For Each ItemControl In Me.Controls
    If TypeName(ItemControl) = "CheckBox" Then ' 1. If TypeName(ItemControl) = "CheckBox"
    'you can't do both checkings at the same time because it will give error on items that are not checkboxes
    If ItemControl.Value = True Then CounterDummy = CounterDummy + 1: ReDim Preserve DummyArray(CounterDummy): DummyArray(CounterDummy) = ItemControl.Caption
    End If ' 1. If TypeName(ItemControl) = "CheckBox"
    Next ItemControl
    Array_Ticked_Checkboxes = DummyArray
End Function
Private Sub CommandButton1_Click()
Dim ArrayTickedCheckboxes() As String
Dim ItemArray As Variant
    ArrayTickedCheckboxes = Array_Ticked_Checkboxes
    On Error GoTo Err01CommandButton1_Click
    For Each ItemArray In ArrayTickedCheckboxes
    MsgBox ItemArray
    Next ItemArray
Err01CommandButton1_Click:
End Sub

<强> 样品: enter image description here