gpt4 book ai didi

Excel VBA - 将多个用户表单复选框值写入单个单元格

转载 作者:行者123 更新时间:2023-12-04 21:25:39 26 4
gpt4 key购买 nike

我正在尝试获取从具有 4 个复选框选项的用户窗体传递的值并将它们写入单个连接单元格。

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

enter image description here

我想将它保存到这样的单个单元格中:

enter image description here

我尝试使用以下代码完成此操作(见下文),但如果仅选择第 2、第 3 或第 4 项而不选择第一项,则逗号等操作不太正确。我相信有更好的方法,但我无法弄清楚或在网上找到答案。

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

最佳答案

将框架控件重命名为 frameColours然后你可以循环它的复选框并构建你的字符串;

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

关于Excel VBA - 将多个用户表单复选框值写入单个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15343473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com