Check boxes on a userform

mummbles74

Board Regular
Joined
Nov 14, 2009
Messages
120
Office Version
  1. 365
Platform
  1. Windows
I have created a template sheet that I will be using for my team to create quotation. I have generated the first part of the form that completes the pricing part.
I have a userform that I want to use to select different conditions to a quotation. I want the user to be able to tick/check the check boxes and for the text in the caption to be used to create a list that is then inserted into the sheet. I learn on the go all the time and it has been a while since I have used this level of code so I want to be able to work through 1 step at a time.

The first task i want to accomplish is creating the body of text (or number of filled rows) that is a result of the selected boxes. i am making the assumption that the captions might not be able to be used for this and I will have to generate the text on a separate sheet or in the code itself.

Thanks in anticipation.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Upload an image, of a sample of your data e also the userform you have already created, so we can start it step by step.
 
Upvote 0
1579039416606.png


i have kept the check boxes as the (name) and the same for the command buttons. There will be a lot more than 4 checkboxes but I assume the process will just repeat

Button 1 will be the OK button that allows what ever boxes are selected to have there data inserted in column A row 55 and the selection to be entered in the next available row so if only checkbox 1 and 4 are selected they would insert into row 55 & 56 etc.

Button 2 would be to clear the checkboxes

Button 3 will be to close the form with no action at all

I hope that all makes sense, if there is anything to clarify then please let me know
 
Upvote 0
Try this

VBA Code:
Private Sub CommandButton1_Click()
  Dim ctrl As MSForms.Control, i As Long
  i = 55
  For Each ctrl In Controls
    If TypeName(ctrl) = "CheckBox" Then
      If ctrl.Value Then
        Range("A" & i).Value = ctrl.Caption
        i = i + 1
      End If
    End If
  Next
End Sub

Private Sub CommandButton2_Click()
  Dim ctrl As MSForms.Control
  For Each ctrl In Controls
    If TypeName(ctrl) = "CheckBox" Then ctrl.Value = False
  Next
End Sub

Private Sub CommandButton3_Click()
  Unload Me
End Sub
 
Upvote 0
Here is the UserForm, and a sheet where the data of each checkbox is filled IF it is checked

Check boxes on a userform.png


And here is the code of this UserForm. There are better ways of doing this code, using a loop go go through each control, but the way I did maybe is easier for you to understand the code :)

VBA Code:
'This is the OK button
Private Sub CommandButton1_Click()
   
    Dim next_row As Long    'This will be variable to store the number of the next empty
                            'row at column A to fill the data of each checkbox in that row
   
'__________________________________________________________________________________________
    If Me.CheckBox1 = True Then
        next_row = Sheets("Sheet1").Range("A500000").End(xlUp).Row + 1
        Sheets("Sheet1").Range("A" & next_row).Value = Me.CheckBox1.Caption
    End If
'___The rest of the code, just repeats this part, changing Only the CheckBox name___________
   
    If Me.CheckBox2 = True Then
        next_row = Sheets("Sheet1").Range("A500000").End(xlUp).Row + 1
        Sheets("Sheet1").Range("A" & next_row).Value = Me.CheckBox2.Caption
    End If
   
    If Me.CheckBox3 = True Then
        next_row = Sheets("Sheet1").Range("A500000").End(xlUp).Row + 1
        Sheets("Sheet1").Range("A" & next_row).Value = Me.CheckBox3.Caption
    End If
   
    If Me.CheckBox4 = True Then
        next_row = Sheets("Sheet1").Range("A500000").End(xlUp).Row + 1
        Sheets("Sheet1").Range("A" & next_row).Value = Me.CheckBox4.Caption
    End If
   
    'And so on...
   
End Sub

'This is the Clear button
Private Sub CommandButton2_Click()
    Me.CheckBox1 = False
    Me.CheckBox2 = False
    Me.CheckBox3 = False
    Me.CheckBox4 = False
End Sub

'This is the Close button. It only closes the userform
Private Sub CommandButton3_Click()
    Unload Me
End Sub
 
Upvote 0
Hi,

The above code is missing the '.value' for each control object.
I think part the above code should read;

VBA Code:
'This is the OK button
Private Sub CommandButton1_Click()
   
    Dim next_row As Long    'This will be variable to store the number of the next empty
                            'row at column A to fill the data of each checkbox in that row
   
'__________________________________________________________________________________________
    If Me.CheckBox1.value = True Then
        next_row = Sheets("Sheet1").Range("A500000").End(xlUp).Row + 1
        Sheets("Sheet1").Range("A" & next_row).Value = Me.CheckBox1.Caption
    End If
'___The rest of the code, just repeats this part, changing Only the CheckBox name___________
   
    If Me.CheckBox2.value = True Then
        next_row = Sheets("Sheet1").Range("A500000").End(xlUp).Row + 1
        Sheets("Sheet1").Range("A" & next_row).Value = Me.CheckBox2.Caption
    End If
   
    If Me.CheckBox3.value = True Then
        next_row = Sheets("Sheet1").Range("A500000").End(xlUp).Row + 1
        Sheets("Sheet1").Range("A" & next_row).Value = Me.CheckBox3.Caption
    End If
   
    If Me.CheckBox4.value = True Then
        next_row = Sheets("Sheet1").Range("A500000").End(xlUp).Row + 1
        Sheets("Sheet1").Range("A" & next_row).Value = Me.CheckBox4.Caption
    End If
   
    'And so on...
   
End Sub

'This is the Clear button
Private Sub CommandButton2_Click()
    Me.CheckBox1.value = False
    Me.CheckBox2.value = False
    Me.CheckBox3.value = False
    Me.CheckBox4.value = False
End Sub

'This is the Close button. It only closes the userform
Private Sub CommandButton3_Click()
    Unload Me
End Sub

Also the next_row value could be calculated as follows;

VBA Code:
next_row = Sheets("Sheet1").UsedRange.Rows.Count + 1

This code also assumes the required sheet is named "Sheet1".
This is a fair assumption, but should also be noted if the OP actually uses a different sheet.

regards,
BenR
 
Upvote 0
All

Thank you all for the responses, i seem to have got the function I am looking for from DanteAmor's code. I will continue to add the additional conditions that I have and ensure the functionality continues.

Many thanks as always!
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
Is there any way to set i as the first blank row in column A after ROW 55?
 
Upvote 0
Try this

VBA Code:
Private Sub CommandButton1_Click()
  Dim ctrl As MSForms.Control, i As Long
  i = Range("A" & Rows.Count).End(xlUp).Row + 1
  If i < 55 Then i = 55
  For Each ctrl In Controls
    If TypeName(ctrl) = "CheckBox" Then
      If ctrl.Value Then
        Range("A" & i).Value = ctrl.Caption
        i = i + 1
      End If
    End If
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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
Back
Top