Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.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 根据单元格的值将选项按钮的值设置为true_Excel_Vba_Radio Button - Fatal编程技术网

Excel 根据单元格的值将选项按钮的值设置为true

Excel 根据单元格的值将选项按钮的值设置为true,excel,vba,radio-button,Excel,Vba,Radio Button,我有一个Excel工作表“TestSheet”和一个用户表单frmTestForm,其中有一个名为OptionButton1的选项按钮。我从编辑器中手动添加了该按钮,因此我相信它是一个表单控件选项按钮。我想根据工作表“TestSheet”中单元格C2的值打开(表示显示为选中)选项按钮 错误消息是“Object variable or With lock not set”(对象变量或未设置锁),我认为这表明选项按钮未正确定义,但我不知道如何修复它。提前感谢你的帮助 我编辑了代码以反映这两个评论。在

我有一个Excel工作表“TestSheet”和一个用户表单frmTestForm,其中有一个名为OptionButton1的选项按钮。我从编辑器中手动添加了该按钮,因此我相信它是一个表单控件选项按钮。我想根据工作表“TestSheet”中单元格C2的值打开(表示显示为选中)选项按钮

错误消息是“Object variable or With lock not set”(对象变量或未设置锁),我认为这表明选项按钮未正确定义,但我不知道如何修复它。提前感谢你的帮助


我编辑了代码以反映这两个评论。在OptionButton1行中仍然有一条正常的错误消息,“对象变量或未设置锁”。Value=True。再次感谢您的帮助。

成功了!更改代码如下:


        Sub Test_Form()
             Dim OptionButton1 As Variant

             Set OptionButton1 = frmTest_Form.OptionButton1
             If Range("C2").Value = 5 Then
                 OptionButton1.Value = True
             End If

             frmTest_Form.Show
        End Sub

谢谢你的帮助

对象需要前面的
Set
来分配其值,还有
frmTest\u Form
frmTestForm
哪一个?对于控件声明,必须使用新控件或当前控件对其进行设置,例如dim myopt as optionbutton:Set myopt=optionbutton1

        Sub Test_Form()
             Dim OptionButton1 As Variant

             Set OptionButton1 = frmTest_Form.OptionButton1
             If Range("C2").Value = 5 Then
                 OptionButton1.Value = True
             End If

             frmTest_Form.Show
        End Sub