vba 一次重新设置所有选项按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18691864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:13:13  来源:igfitidea点击:

Re-setting all option buttons at once

vbaexcel-vbaexcel-2007excel

提问by datacentric

I have written a code for re-setting all option button on one click but its giving an error, "object doesnt support the property or matter".

我已经编写了一个代码,用于一键重新设置所有选项按钮,但它给出了一个错误,“对象不支持该属性或问题”。

Sub Add_New_Record()
Dim i As Integer
For i = 1 To 30
    With Sheets("Form")
        '-- unlock the worksheet
        .Unprotect
        .OptionButton(i).Value = False

        '-- lock the worksheet
        '.Protect

        .Activate
        .Range("Q12").Select
     End With
     Next i
End Sub

Can anyone please suggest me how to fix the code and make the value of all option buttons "false" at one.

任何人都可以建议我如何修复代码并使所有选项按钮的值“假”为一个。

I know how to do it individually like:

我知道如何单独执行,例如:

Sub Add_New_Record()
    With Sheets("Form")
        '-- unlock the worksheet
        .Unprotect
        .OptionButton1.Value = False

        '-- lock the worksheet
        '.Protect
        .Activate
        .Range("Q12").Select
     End With
End Sub

but since I have too many buttons, the code will get really long and inefficient.

但由于我有太多按钮,代码会变得非常长且效率低下。

Thanks for your help and time.

感谢您的帮助和时间。

回答by AKS

Loop through all the OLEObjects on a particular sheet and if it is an optionbutton then set it to false.

循环遍历特定工作表上的所有 OLEObjects,如果它是一个选项按钮,则将其设置为 false。

For i = 1 To ActiveSheet.OLEObjects.Count
    If TypeName(ActiveSheet.OLEObjects(i).Object) = "OptionButton" Then
        ActiveSheet.OLEObjects(i).Object = False
    End If
Next i

Embedding this snippet in your code:

将此代码段嵌入您的代码中:

Sub Add_New_Record()
    With Sheets(1)
        .Unprotect
        For i = 1 To .OLEObjects.Count
            If TypeName(.OLEObjects(i).Object) = "OptionButton" Then
                .OLEObjects(i).Object = False
            End If
        Next i
        .Protect
        .Range("Q12").Select
    End With
End Sub

Read more about OLEObjects here

在此处阅读有关 OLEObjects 的更多信息

回答by PatricK

First, the Withstatement should be before the Forloop. And it should be .OptionButtons. Try this one.

首先,With语句应该在For循环之前。它应该是.OptionButtons。试试这个。

Sub Add_New_Record()
    Dim i As Integer

    With Sheets("Form")
        .Unprotect
        For i = 1 To 30
            .OptionButtons(i).Value = False
        Next i
        .Protect
    End With
End Sub