编辑所选OptionButton VBA的属性

时间:2017-09-09 08:19:10

标签: excel vba excel-vba radio-button

我正在尝试从大约10x60制作一个选项按钮网格,并希望使用VBA,但我不能让属性改变工作。

到目前为止,我得到了这个:

Sub Buttons()
Dim i As Integer
Dim j As Integer
Dim k As Integer
k = 48
For i = 8 To 9
    For j = 5 To 15
        ActiveSheet.Shapes.Range(Array("OptionButton" & k)).Select
        k = k + 1
        Selection.Copy
        With Sheets("Weekreview")
            .Cells(i, j).Select
            .Paste
            .Shapes.Range(Array("OptionButton" & k)).Select
            .OptionButtons(k).GroupName = i - 1
            .OptionButtons(k).LinkedCell = Range(j, i)
        End With
    Next
Next
End Sub

问题在于.OptionButtons(k).GroupName中的程序错误,并显示消息“无法获取Worksheet类的OptionButtons属性”。 谁可以帮助我?

编辑1:我的第一次尝试(在我尝试使用Google搜索该问题的所有方法之前)是使用Selection.GroupName,这也不起作用。看起来它无法访问属性。所以要么属性改变错误,要么选择错误。

编辑2:除了更改现有OptionButton的GroupName之外,我使整个程序正常工作。即使Selection.LinkedCell有效,Selection.GroupName也不会。

4 个答案:

答案 0 :(得分:0)

您的代码复制并粘贴OptionButton & k然后引用OptionButton & k+1(对象不存在)。 看看线是否增加了k:

k = k + 1

答案 1 :(得分:0)

请更改所有字词

ActiveSheet.Shapes.Range(Array("OptionButton" & k))

ActiveSheet.Shapes.Range("Option Button " & k)

答案 2 :(得分:0)

Please try this code:

Sub Buttons()

    Dim i As Integer
    Dim j As Integer
    Dim k As Integer        

    k = 48
    For i = 8 To 9
        For j = 5 To 15
            ActiveSheet.Shapes.Range(Array("OptionButton" & k)).Select                
            k = k + 1
            Selection.Copy
            ActiveSheet.Paste
            With Selection
                .Name = "OptionButton" & k
                .Top = Worksheets("Weekreview").Cells(i, j).Top
                .Left = Worksheets("Weekreview").Cells(i, j).Left
                .GroupName = i - 1
                .LinkedCell = Range(j, i)
            End With

        Next
    Next

End Sub

答案 3 :(得分:0)

具有TypeName#命名约定的控件是ActiveX控件(例如" OptionButton1"," TextBox1")。对象本身包装在OLEObject中。应使用工作表的 OLEObjects 集合引用工作表上的ActiveX控件。

OLEObject.Object可以访问无法直接从OLEObject获取的属性。

enter image description here

enter image description here     子按钮()         Application.ScreenUpdating = False         Dim opt As OLEObject         Dim cell As Range

    With Sheets("Weekreview")
        For Each cell In Range(Cells(8, 5), Cells(9, 15))
            Set opt = .OLEObjects.Add(ClassType:="Forms.OptionButton.1", Link:=False, DisplayAsIcon:=False, Width:=108, Height:=21)
            With opt
                .Left = cell.Left
                .Top = cell.Top
                .Width = cell.Width
                .LinkedCell = cell
                .Name = cell.Address(False, False)
                With opt.Object
                    .GroupName = cell.Row
                    .Caption = cell.Address(False, False)
                End With
            End With
        Next
    End With
     Application.ScreenUpdating = True
End Sub