2016-12-29 101 views
-3

我有多個複選框和單個文本框。如何將所有複選框的值勾選到VBA Userform中的單個文本框? 我的形式看起來像這樣..多個複選框的輸入值選擇爲單個文本框vba

enter image description here

+4

您的代碼嘗試在哪裏? –

+0

@ShaiRado我仍然試圖找出它..我沒有任何示例代碼呢.. :( – Meedee

+2

@Meedee看看這個網站,以幫助你去http://www.excel-easy.com/ vba/userform.html – Mick17

回答

2

IIF總統:

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

等IIF是一個不錯的直列IF替代,您可以在級聯使用它。如果checkbox1的價值是真實的,然後一些文本添加到拼接等,您可能需要使用一個單獨的變量來組裝文字,可能是對眼睛更容易...

+0

謝謝..我之前沒有使用過IIF,但我也會嘗試使用這些代碼。 .. – Meedee

0

解決辦法:
這會給你的函數來獲取的thicked框中的值,因爲它是一個功能,使用它需要
代碼:

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

+0

謝謝!我也會試試這個.. – Meedee

+0

如果答案有幫助/適合您的需求,請不要忘記點擊有用/接受的答案:) – Sgdva