Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA-将多个userform复选框值写入单个单元格_Excel_Vba_Checkbox_Concatenation_Userform - Fatal编程技术网

Excel VBA-将多个userform复选框值写入单个单元格

Excel VBA-将多个userform复选框值写入单个单元格,excel,vba,checkbox,concatenation,userform,Excel,Vba,Checkbox,Concatenation,Userform,我试图从具有4个复选框选项的userform中获取传递的值,并将它们写入一个连接的单元格 当我像这样选择我的用户表单时: 我想将其保存到单个单元格中,如下所示: 我试着用下面的代码(见下文)来实现这一点,但如果只选择了第二、第三或第四项而没有选择第一项,那么使用逗号等就不太合适。我确信有更好的方法,但我无法找到答案,也无法在网上找到答案 Private Sub cmdSave_Click() Dim colors As String If chkRed = True Then

我试图从具有4个复选框选项的userform中获取传递的值,并将它们写入一个连接的单元格

当我像这样选择我的用户表单时:

我想将其保存到单个单元格中,如下所示:

我试着用下面的代码(见下文)来实现这一点,但如果只选择了第二、第三或第四项而没有选择第一项,那么使用逗号等就不太合适。我确信有更好的方法,但我无法找到答案,也无法在网上找到答案

Private Sub cmdSave_Click()
  Dim colors As String

   If chkRed = True Then
      colors = "Red"
   Else
      colors = colors
   End If

   If chkBlue = True Then
      colors = colors & ", Blue"
   Else
      colors = colors
   End If

   If chkGreen = True Then
      colors = colors & ", Green"
   Else
      colors = colors
   End If

   If chkYellow = True Then
      colors = colors & ", Yellow"
   Else
      colors = colors
   End If

   With colorsSheet
      .Cells(ActiveCell.Row, 1).Value = colors
   End With

   Unload Me

End Sub

将框架控件重命名为
framecolors
,然后可以循环其复选框并生成字符串

Dim chk as Control
Dim colors as string, delimiter as string

for Each chk In Me.frameColours.Controls
    if typeOf chk Is msforms.CheckBox then
        if (chk.Value) then
            colors = colors & delimiter & chk.Caption
            delimiter = ","
        end if
    end if
next

With colorsSheet
    .Cells(ActiveCell.Row, 1).Value = colors
End With

将框架控件重命名为
framecolors
,然后可以循环其复选框并生成字符串

Dim chk as Control
Dim colors as string, delimiter as string

for Each chk In Me.frameColours.Controls
    if typeOf chk Is msforms.CheckBox then
        if (chk.Value) then
            colors = colors & delimiter & chk.Caption
            delimiter = ","
        end if
    end if
next

With colorsSheet
    .Cells(ActiveCell.Row, 1).Value = colors
End With

如果要从输出中删除初始逗号,如“蓝、绿、黄”,则必须更改添加这些字符串的代码,以检查
colors
是否为空。比如:

If chkBlue = true Then
    If colors = "" Then
        colors = "Blue"
    Else
        colors = colors & ", Blue"
    End If
End If
由于“红色”是您添加的第一种颜色,因此您可以假定
颜色
为空:

If chkRed = True Then
      colors = "Red"
End If

你不需要
其他。。。colors=colors

如果要从输出中删除初始逗号,如“蓝、绿、黄”,则必须更改添加这些字符串的代码,以检查
colors
是否为空。比如:

If chkBlue = true Then
    If colors = "" Then
        colors = "Blue"
    Else
        colors = colors & ", Blue"
    End If
End If
Private Sub Submit_Click()

Dim lngIndex As Long, strTemp As String


For lngIndex = 1 To 16

'There are 16 check boxex, hence 1 to 16 is given

If Me.Controls("CheckBox" & lngIndex) Then
    strTemp = strTemp & Me.Controls("TextBox" & lngIndex).Value & vbLf 

'Returns you the output in a new line within the same cell.

End If

Next lngIndex

Sheets("Sheet1").Range("A1").Value = Left$(strTemp, Len(strTemp) - 1)

End Sub
由于“红色”是您添加的第一种颜色,因此您可以假定
颜色
为空:

If chkRed = True Then
      colors = "Red"
End If

你不需要
其他。。。colors=colors

我喜欢(并投票赞成)Alex K.的解决方案。它使用更少的代码,并且如果你添加颜色,就不需要更改代码。蒂姆,你说得对。亚历克斯有一个很好的解决办法。我也喜欢你的,因为它修复了我的代码。很高兴有两种方法来完成我开始做的事情。谢谢。我喜欢@Alex K.的解决方案。它使用更少的代码,并且如果你添加颜色,就不需要更改代码。蒂姆,你说得对。亚历克斯有一个很好的解决办法。我也喜欢你的,因为它修复了我的代码。很高兴有两种方法来完成我开始做的事情。谢谢,亚历克斯,看起来很棒。非常感谢你。我会在这里试一试,让你知道它是怎么回事。我以前没有学过如何用这种方法,我知道一定有更好的方法。效果很好。我唯一需要做的更改是在分隔符中添加一个空格,将其从“,”改为“,”Alex,看起来很棒。非常感谢你。我会在这里试一试,让你知道它是怎么回事。我以前没有学过如何用这种方法,我知道一定有更好的方法。效果很好。我要做的唯一更改是在分隔符中添加一个空格,将其从“,”更改为“,”
Private Sub Submit_Click()

Dim lngIndex As Long, strTemp As String


For lngIndex = 1 To 16

'There are 16 check boxex, hence 1 to 16 is given

If Me.Controls("CheckBox" & lngIndex) Then
    strTemp = strTemp & Me.Controls("TextBox" & lngIndex).Value & vbLf 

'Returns you the output in a new line within the same cell.

End If

Next lngIndex

Sheets("Sheet1").Range("A1").Value = Left$(strTemp, Len(strTemp) - 1)

End Sub