如何将用户窗体的多个复选框的值设置为单个单元格
how to set the value of multiple checkbox of the userform to a single cell
情况:
我有一个用户表单,如下所示:
当前解决方案
当我单击确定时,显示以下结果
这意味着复选框值存储在单独的单元格中。
想要的解决方案
我是否可以修改我的代码,以便在单个单元格中输入值,例如下图中显示的复选框结果的各种组合。
我的代码如下:
Private Sub CommandButton1_Click()
If CheckBox1.Value = True Then Worksheets("Sheet1").Cells(10, 2).Value = "Checked" Else Worksheets("Sheet1").Cells(10, 15).Value = ""
If CheckBox3.Value = True Then Worksheets("Sheet1").Cells(10, 3).Value = "Forwarded" Else Worksheets("Sheet1").Cells(10, 14).Value = ""
If CheckBox4.Value = True Then Worksheets("Sheet1").Cells(10, 4).Value = "Notified" Else Worksheets("Sheet1").Cells(10, 16).Value = ""
结束子
您应该能够在变量中构建一个字符串,然后用它填充单元格:
Private Sub CommandButton1_Click()
Dim Value as String
If CheckBox1.Value Then Value = "Checked"
If CheckBox3.Value Then Value = Value & IIf(Value = "", "", " / ") & "Forwarded"
If CheckBox4.Value Then Value = Value & IIf(Value = "", "", " / ") & "Notified"
Worksheets("Sheet1").Cells(10, 2).Value = Value
End Sub
请注意使用 IIf 来确定是否在适当的时候有条件地添加分隔符 (" / "
),而不是稍后尝试 trim 将其关闭。
这是我能找到的最佳解决方案。我期待更正,但到目前为止它符合我的需要。
Private Sub CommandButton1_Click()
Dim Value As String
If CheckBox1.Value Then Value = "Checked"
If CheckBox3.Value Then Value = "Forwarded"
If CheckBox1.Value And CheckBox3.Value Then Value = IIf(Value = "", "", "Checked/") & "Forwarded"
If CheckBox4.Value Then Value = "Notified"
If CheckBox3.Value And CheckBox4.Value Then Value = IIf(Value = "", "", "Forwarded/") & "Notified"
If CheckBox1.Value And CheckBox4.Value Then Value = IIf(Value = "", "", "Checked/") & "Notified"
If CheckBox1.Value And CheckBox3.Value And CheckBox4.Value Then Value = IIf(Value = "", "", "Checked/Forwarded") & "Notified"
Worksheets("Sheet1").Cells(10, 2).Value = Value
End Sub
谢谢。
情况: 我有一个用户表单,如下所示:
当前解决方案
当我单击确定时,显示以下结果
这意味着复选框值存储在单独的单元格中。
想要的解决方案
我是否可以修改我的代码,以便在单个单元格中输入值,例如下图中显示的复选框结果的各种组合。
我的代码如下:
Private Sub CommandButton1_Click()
If CheckBox1.Value = True Then Worksheets("Sheet1").Cells(10, 2).Value = "Checked" Else Worksheets("Sheet1").Cells(10, 15).Value = ""
If CheckBox3.Value = True Then Worksheets("Sheet1").Cells(10, 3).Value = "Forwarded" Else Worksheets("Sheet1").Cells(10, 14).Value = ""
If CheckBox4.Value = True Then Worksheets("Sheet1").Cells(10, 4).Value = "Notified" Else Worksheets("Sheet1").Cells(10, 16).Value = ""
结束子
您应该能够在变量中构建一个字符串,然后用它填充单元格:
Private Sub CommandButton1_Click()
Dim Value as String
If CheckBox1.Value Then Value = "Checked"
If CheckBox3.Value Then Value = Value & IIf(Value = "", "", " / ") & "Forwarded"
If CheckBox4.Value Then Value = Value & IIf(Value = "", "", " / ") & "Notified"
Worksheets("Sheet1").Cells(10, 2).Value = Value
End Sub
请注意使用 IIf 来确定是否在适当的时候有条件地添加分隔符 (" / "
),而不是稍后尝试 trim 将其关闭。
这是我能找到的最佳解决方案。我期待更正,但到目前为止它符合我的需要。
Private Sub CommandButton1_Click()
Dim Value As String
If CheckBox1.Value Then Value = "Checked"
If CheckBox3.Value Then Value = "Forwarded"
If CheckBox1.Value And CheckBox3.Value Then Value = IIf(Value = "", "", "Checked/") & "Forwarded"
If CheckBox4.Value Then Value = "Notified"
If CheckBox3.Value And CheckBox4.Value Then Value = IIf(Value = "", "", "Forwarded/") & "Notified"
If CheckBox1.Value And CheckBox4.Value Then Value = IIf(Value = "", "", "Checked/") & "Notified"
If CheckBox1.Value And CheckBox3.Value And CheckBox4.Value Then Value = IIf(Value = "", "", "Checked/Forwarded") & "Notified"
Worksheets("Sheet1").Cells(10, 2).Value = Value
End Sub
谢谢。