[英]Excel VBA combining results from multiple checkboxes into single worksheet cell
I have a Userform frame containing 20 checkboxes, each checkbox represents a different coloured shirt. 我有一个包含20个复选框的Userform框架,每个复选框代表一个不同颜色的衬衫。
When a user makes their checkbox selection/s, I'd like the results (the caption, not true/false) to end up in a single cell (column 8) of the active row, separated by a comma. 当用户进行复选框选择时,我希望结果(标题,而不是真/假)最终在活动行的单个单元格(第8列)中,以逗号分隔。
I'm a newbie at VBA coding and learning on the fly - can anyone please help? 我是VBA编码和动态学习的新手 - 有人可以帮忙吗?
Edit1: Removed line continuation _
on If statement
. Edit1:在If statement
删除了行继续_
。
Create a sub routine to check all the Checkboxes
and pass the data to the selected cell. 创建子例程以检查所有Checkboxes
并将数据传递到所选单元格。
Try something like this: 尝试这样的事情:
Private Sub GetSelection()
Dim c As MSForms.Control
Dim cbk As MSForms.CheckBox
Dim sel As String
For Each c In Me.Controls '/* Iterate all controls */
If TypeOf c Is MSForms.CheckBox Then '/* check if it is a checkbox */
Set cbk = c '/* Set as Checkbox to get the caption property */
If cbk Then '/* Check value, get caption if true */
If sel = "" Then
sel = cbk.Caption
Else
sel = sel & "," & cbk.Caption
End If
End If
End If
Next
'/* Transfer currently selected values in desired cell */
Sheet1.Range("H" & ActiveCell.Row).Value2 = sel '/* Used H for column 8 */
End Sub
Then all you need to do is call this sub routine in all your CheckBox_Click
event. 然后,您需要做的就是在所有CheckBox_Click
事件中调用此子例程。
Private Sub CheckBox1_Click()
GetSelection
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.