vba将checkbox中选中的值全部写入单元格中。

操作步骤:
1.点击某个单元格时弹出窗体
2.选择窗体中的checkbox(多选)
3.将选中的checkbox的值全部写入指定的单元格中
4.目前存在的问题:前两步都已实现,只是第三步向单元格中写值时,只能把最后一个值写进去,前面选中的其他值均写不进去。
5.寻求帮助,把选中的所有值都能写进单元格中。
谢谢!悬赏50!

我的笨方法:
按钮命令

Private Sub CommandButton1_Click()
Dim SS(1 To 4)
Dim myStr

If CheckBox1.Value Then SS(1) = CheckBox1.Caption
If CheckBox2.Value Then SS(2) = CheckBox2.Caption
If CheckBox3.Value Then SS(3) = CheckBox3.Caption
If CheckBox4.Value Then SS(4) = CheckBox4.Caption
For i = 1 To 4
If SS(i) <> "" Then
myStr = myStr & "," & SS(i)
End If
Next
If myStr <> "" Then
myStr = Right(myStr, Len(myStr) - 1)
End If
UserForm1.Caption = myStr
'ThisWorkbook.ActiveSheet.Range(Mid(myadd, 2, 1) & Right(myadd, 1)).Value = myStr
UserForm1.Hide
CheckBox1.Value = False
CheckBox2.Value = False
CheckBox3.Value = False
CheckBox4.Value = False
End Sub
表格命令:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
UserForm1.Show
Target.Value = UserForm1.Caption
UserForm1.Caption = "Waiting..."
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-04-17
原因是你的区域写的不对。

set r = range("A1:A10")
你要循环每一个单元格,不能用r直接写,如果想直接写入,得用数组,但是要注意数据的转置。
第2个回答  2012-04-16
按照你的需求,写了某个确定按钮
Private Sub CommandButton1_Click()
Dim s As String
For Each chb In UserForm1.Controls
If TypeOf chb Is msforms.CheckBox And chb.Object.Value Then
If s = "" Then
s = chb.Caption
Else
s = s & "_" & chb.Caption
End If
End If
Next
[a1] = s
End Sub
你看看 估计你要的就是这句
“If TypeOf chb Is msforms.CheckBox And chb.Object.Value Then……”追问

“If TypeOf chb Is msforms.CheckBox And chb.Object.Value Then……”
这句话报错了~
Run-time error '438':
Object doesn't support this property or method.

相似回答