Copy Checkbox and ComboBox value to Textbox

chester1993

New Member
Joined
Jan 26, 2016
Messages
36
Hello All,

Need help with my work.

I have a userform that has multiple checkboxes and a label.
What I am looking forward is:
1. I want to copy the label into the textbox when one of the checkbox is selected. If multiple selections, one label only for the entire selection.
2. I want to copy the checkbox captions to the textbox when checked and remove it from the textbox when unchecked. The captions will be under the "label" and would look like a list.
For instance, on the sample, it would look like this when all checkboxes is chosen.
Received:
• Name
• Address
• Country
3. I want the order of the texts on the textbox to be the same order on how it is arranged on the userform.

For the combobox.
1. I want to display a certain text on the textbox when a selection is made.
For instance, one of the option is "Close Account". When selected, I want the ff to display on the textbox:
• Will close account
• Will notify customer

I can't attached a sample workbook for some reason. Not sure if its not allowed or what. I'll just upload of a screenshot on how the userform looks like.

P.S.
I am not that great with VBA yet, I would like to ask if you can put a comment on the code just so I can understand what the code does and mess it up. Haha.
Thank you so much everyone!
 

Attachments

  • 1.PNG
    1.PNG
    17.7 KB · Views: 7

Some videos you may like

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,594
Office Version
  1. 2007
Platform
  1. Windows
You didn't mention the names of your controls.
You will have to adjust them in the code.
I guess the following:
Label1 for "Received",
Checkbox1 to 3 for those below "Received".
Label2 for "Required", Checkbox4 to 6 for those below.

Try this
VBA Code:
Sub FillTextBox()
  With TextBox1
    .Value = ""
    If CheckBox1 Or CheckBox2 Or CheckBox3 Then .Value = Label1.Caption & vbCr
    If CheckBox1 Then .Value = .Value & CheckBox1.Caption & vbCr
    If CheckBox2 Then .Value = .Value & CheckBox2.Caption & vbCr
    If CheckBox3 Then .Value = .Value & CheckBox3.Caption & vbCr
    '
    If CheckBox4 Or CheckBox5 Or CheckBox6 Then .Value = .Value & Label2.Caption & vbCr
    If CheckBox4 Then .Value = .Value & CheckBox4.Caption & vbCr
    If CheckBox5 Then .Value = .Value & CheckBox5.Caption & vbCr
    If CheckBox6 Then .Value = .Value & CheckBox6.Caption & vbCr
  End With
End Sub

Private Sub CheckBox1_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox2_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox3_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox4_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox5_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox6_Click()
  Call FillTextBox
End Sub

Check the code for the checks and then I check the combo.
 

chester1993

New Member
Joined
Jan 26, 2016
Messages
36
You didn't mention the names of your controls.
You will have to adjust them in the code.
I guess the following:
Label1 for "Received",
Checkbox1 to 3 for those below "Received".
Label2 for "Required", Checkbox4 to 6 for those below.

Try this
VBA Code:
Sub FillTextBox()
  With TextBox1
    .Value = ""
    If CheckBox1 Or CheckBox2 Or CheckBox3 Then .Value = Label1.Caption & vbCr
    If CheckBox1 Then .Value = .Value & CheckBox1.Caption & vbCr
    If CheckBox2 Then .Value = .Value & CheckBox2.Caption & vbCr
    If CheckBox3 Then .Value = .Value & CheckBox3.Caption & vbCr
    '
    If CheckBox4 Or CheckBox5 Or CheckBox6 Then .Value = .Value & Label2.Caption & vbCr
    If CheckBox4 Then .Value = .Value & CheckBox4.Caption & vbCr
    If CheckBox5 Then .Value = .Value & CheckBox5.Caption & vbCr
    If CheckBox6 Then .Value = .Value & CheckBox6.Caption & vbCr
  End With
End Sub

Private Sub CheckBox1_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox2_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox3_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox4_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox5_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox6_Click()
  Call FillTextBox
End Sub

Check the code for the checks and then I check the combo.
Thank you Dante. I will try this codes.
 

chester1993

New Member
Joined
Jan 26, 2016
Messages
36

ADVERTISEMENT

Actually, I am having an error with the above code. I am just testing everything now. So far, here is my code and I will not include the Call code anymore:

VBA Code:
Sub FillTextBox()
    With TextBox1
        .Value = ""
        If CheckBox1 Or CheckBox2 Or CheckBox3 Then .Value = Label1.Caption & vbCr
        If CheckBox1 Then .Value = .Value & "• " & CheckBox1.Caption & vbCr
        If CheckBox2 Then .Value = .Value & "• " & CheckBox2.Caption & vbCr
        If CheckBox3 Then .Value = .Value & "• " & CheckBox3.Caption & vbCr
        If CheckBox4 Then .Value = .Value & "• " & CheckBox4.Caption & vbCr
        If CheckBox5 Then .Value = .Value & "• " & CheckBox7.Caption & vbCr
        '
        If CheckBox7 Or CheckBox8 Or CheckBox9 Then .Value = Label2.Caption & vbCr
        If CheckBox7 Then .Value = .Value & "• " & CheckBox7.Caption & vbCr
        If CheckBox8 Then .Value = .Value & "• " & CheckBox8.Caption & vbCr
        If CheckBox9 Then .Value = .Value & "• " & CheckBox9.Caption & vbCr
        If CheckBox10 Then .Value = .Value & "• " & CheckBox10.Caption & vbCr
        If CheckBox11 Then .Value = .Value & "• " & CheckBox11.Caption & vbCr
    End With
        
End Sub

What is happening right now is when I click on a checkbox under Label1, it goes as plan. It would appear on the textbox. But, as soon as I tick something under Label2, it overrides the 1st selection under Label1. The goal is to have both labels on the textbox if any of their below checkboxes is ticked.

Thanks again.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,594
Office Version
  1. 2007
Platform
  1. Windows
I am having an error with the above code.
What does the error message say?

This is my code:
Rich (BB code):
If CheckBox4 Or CheckBox5 Or CheckBox6 Then .Value = .Value & Label2.Caption & vbCr

This is your code:
VBA Code:
If CheckBox7 Or CheckBox8 Or CheckBox9 Then .Value = Label2.Caption & vbCr

You need to include .Value & :
Rich (BB code):
If CheckBox7 Or CheckBox8 Or CheckBox9 Then .Value = .Value & Label2.Caption & vbCr
___________________________________________________________________________________________________________________________

With combobox:

VBA Code:
Sub FillTextBox()
  With TextBox1
    .Value = ""
    If CheckBox1 Or CheckBox2 Or CheckBox3 Or CheckBox4 Or CheckBox5 Then .Value = Label1.Caption & vbCr
    If CheckBox1 Then .Value = .Value & "• " & CheckBox1.Caption & vbCr
    If CheckBox2 Then .Value = .Value & "• " & CheckBox2.Caption & vbCr
    If CheckBox3 Then .Value = .Value & "• " & CheckBox3.Caption & vbCr
    If CheckBox4 Then .Value = .Value & "• " & CheckBox4.Caption & vbCr
    If CheckBox5 Then .Value = .Value & "• " & CheckBox5.Caption & vbCr
    '
    If CheckBox7 Or CheckBox8 Or CheckBox9 Or CheckBox10 Or CheckBox11 Then .Value = .Value & Label2.Caption & vbCr
    If CheckBox7 Then .Value = .Value & "• " & CheckBox7.Caption & vbCr
    If CheckBox8 Then .Value = .Value & "• " & CheckBox8.Caption & vbCr
    If CheckBox9 Then .Value = .Value & "• " & CheckBox9.Caption & vbCr
    If CheckBox10 Then .Value = .Value & "• " & CheckBox10.Caption & vbCr
    If CheckBox11 Then .Value = .Value & "• " & CheckBox11.Caption & vbCr
    '
    If ComboBox1.ListIndex > -1 Then .Value = .Value & "Will " & ComboBox1.Value
  End With
End Sub

Private Sub ComboBox1_Change()
  If ComboBox1.ListIndex > -1 Then
    Call FillTextBox
  End If
End Sub

Private Sub CheckBox1_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox2_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox3_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox4_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox5_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox7_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox8_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox9_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox10_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox11_Click()
  Call FillTextBox
End Sub
 

chester1993

New Member
Joined
Jan 26, 2016
Messages
36

ADVERTISEMENT

What does the error message say?

This is my code:
Rich (BB code):
If CheckBox4 Or CheckBox5 Or CheckBox6 Then .Value = .Value & Label2.Caption & vbCr

This is your code:
VBA Code:
If CheckBox7 Or CheckBox8 Or CheckBox9 Then .Value = Label2.Caption & vbCr

You need to include .Value & :
Rich (BB code):
If CheckBox7 Or CheckBox8 Or CheckBox9 Then .Value = .Value & Label2.Caption & vbCr
___________________________________________________________________________________________________________________________

With combobox:

VBA Code:
Sub FillTextBox()
  With TextBox1
    .Value = ""
    If CheckBox1 Or CheckBox2 Or CheckBox3 Or CheckBox4 Or CheckBox5 Then .Value = Label1.Caption & vbCr
    If CheckBox1 Then .Value = .Value & "• " & CheckBox1.Caption & vbCr
    If CheckBox2 Then .Value = .Value & "• " & CheckBox2.Caption & vbCr
    If CheckBox3 Then .Value = .Value & "• " & CheckBox3.Caption & vbCr
    If CheckBox4 Then .Value = .Value & "• " & CheckBox4.Caption & vbCr
    If CheckBox5 Then .Value = .Value & "• " & CheckBox5.Caption & vbCr
    '
    If CheckBox7 Or CheckBox8 Or CheckBox9 Or CheckBox10 Or CheckBox11 Then .Value = .Value & Label2.Caption & vbCr
    If CheckBox7 Then .Value = .Value & "• " & CheckBox7.Caption & vbCr
    If CheckBox8 Then .Value = .Value & "• " & CheckBox8.Caption & vbCr
    If CheckBox9 Then .Value = .Value & "• " & CheckBox9.Caption & vbCr
    If CheckBox10 Then .Value = .Value & "• " & CheckBox10.Caption & vbCr
    If CheckBox11 Then .Value = .Value & "• " & CheckBox11.Caption & vbCr
    '
    If ComboBox1.ListIndex > -1 Then .Value = .Value & "Will " & ComboBox1.Value
  End With
End Sub

Private Sub ComboBox1_Change()
  If ComboBox1.ListIndex > -1 Then
    Call FillTextBox
  End If
End Sub

Private Sub CheckBox1_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox2_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox3_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox4_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox5_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox7_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox8_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox9_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox10_Click()
  Call FillTextBox
End Sub
Private Sub CheckBox11_Click()
  Call FillTextBox
End Sub
Oh I messed that up. I will try again. Thank you!
 

chester1993

New Member
Joined
Jan 26, 2016
Messages
36
Hello @DanteAmor . Everything looks good now and I am slowly understanding everything now. I have another query, though.

How would the codes look if I am transferring the texts from textbox1 to textbox2.
For instance, on the same example above. I will add another TextBox in the UserForm. A textbox wherein user can enter their "name" and I want that "name" be displayed on the same textbox where the checkboxes and comboboxes are populated.

Thank you so much.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,594
Office Version
  1. 2007
Platform
  1. Windows
It seems to me that it is a new request. I consider that the original request was solved unless something doesn't work.
So I suggest you create a new thread for the new request.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,867
Messages
5,598,542
Members
414,245
Latest member
allyciv

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top