How would I get VBA to include the result of multiple checkboxes into one cell

DreyFox

Board Regular
Joined
Nov 25, 2020
Messages
61
Office Version
  1. 2016
Platform
  1. Windows
For example, I have multiple checkboxes shown below:
1608647623528.png

When I select the options I want, I would like the "Additional Options" cell to be filled with associated values of the checkboxes. For example, If I select the Arctic/Salinity Package Option, the Drill Depth Sensor Option, and the Feed Rail Lubrication Option, then the cell Additional Options would show ADL. I tried to set this up using the following below:
VBA Code:
Range("M109").Value = IIf(Me.CheckBox33, "A", "")
But it only does it for each individual option, not for all of it.

Any help would be greatly appreciated!
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
This is based on the check boxes being Active-x controls. The checkbox from the Form Controls tool box requires a different syntax. The text in the checkbox is a caption, so we first have to establish that the checkbox is checked by using a loop to go through all controls on the sheet and pick out the checkboxes, see if they are 'True' , then use the Left function to clip off the letter designation and put in in M109.

VBA Code:
Dim ctl As Object
    For Each ctl In ActiveSheet.OLEObjects
        If TypeName(ctl.Object) = "CheckBox" Then
            If ctl.Object.Value = True Then
                If Range("M109") = "" Then
                    Range("M109") = Left(ctl.Object.Caption, 1)
                Else
                    Range("M109") = Range("M109").Value & vbLf & Left(ctl.Object.Caption, 1)
                End If
            End If
        End If
    Next
 
Upvote 0
This is based on the check boxes being Active-x controls. The checkbox from the Form Controls tool box requires a different syntax. The text in the checkbox is a caption, so we first have to establish that the checkbox is checked by using a loop to go through all controls on the sheet and pick out the checkboxes, see if they are 'True' , then use the Left function to clip off the letter designation and put in in M109.

VBA Code:
Dim ctl As Object
    For Each ctl In ActiveSheet.OLEObjects
        If TypeName(ctl.Object) = "CheckBox" Then
            If ctl.Object.Value = True Then
                If Range("M109") = "" Then
                    Range("M109") = Left(ctl.Object.Caption, 1)
                Else
                    Range("M109") = Range("M109").Value & vbLf & Left(ctl.Object.Caption, 1)
                End If
            End If
        End If
    Next
Thank you for your response! Where would I put this in? Would they go in each of the checkboxes?
 
Upvote 0
The code would go into a standard code module, like module1. It can then be called from other code where needed.

VBA Code:
Sub t()
Dim ctl As Object
    For Each ctl In ActiveSheet.OLEObjects
        If TypeName(ctl.Object) = "CheckBox" Then
            If ctl.Object.Value = True Then
                If Range("M109") = "" Then
                    Range("M109") = Left(ctl.Object.Caption, 1)
                Else
                    Range("M109") = Range("M109").Value & vbLf & Left(ctl.Object.Caption, 1)
                End If
            End If
        End If
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

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