如何使用VBA添加选项按钮以在Excel 2010工作表中进行分组?

时间:2015-05-30 11:45:17

标签: excel vba excel-vba excel-2010

我想在excel 工作表(不是VBA表单)中添加许多选项按钮,并希望按行对它们进行分组。结果看起来应该是这样的:
enter image description here

这是我到目前为止使用的代码:

    For d = 1 To 31
            Set checkboxKrankCell = Range("H" + Trim(Str(d)))
            Set checkboxUrlaubCell = Range("I" + Trim(Str(d)))
            Set checkboxJazCell = Range("J" + Trim(Str(d)))
            groupWidth = checkboxKrankCell.Width + checkboxUrlaubCell.Width + checkboxJazCell.Width
            Set groupBoxOptionButtons = ActiveSheet.GroupBoxes.Add(checkboxKrankCell.Left - 1, checkboxKrankCell.Top - 2, groupWidth + 1, checkboxKrankCell.Height)
            With groupBoxOptionButtons
                .Name = "GroupBox_" + Trim(Str(d))
                .Caption = ""
            End With
            Set checkboxKrank = ActiveSheet.OptionButtons.Add(checkboxKrankCell.Left, checkboxKrankCell.Top - 1, checkboxKrankCell.Width, checkboxKrankCell.Height)
            With checkboxKrank
              .Caption = ""
            End With
  #1          checkboxKrank.GroupBox = groupBoxOptionButtons
            Set checkboxUrlaub = ActiveSheet.OptionButtons.Add(checkboxUrlaubCell.Left, checkboxUrlaubCell.Top - 1, checkboxUrlaubCell.Width, checkboxUrlaubCell.Height)
            With checkboxUrlaub
              .Caption = ""
            End With
            Set checkboxJaz = ActiveSheet.OptionButtons.Add(checkboxJazCell.Left, checkboxJazCell.Top - 1, checkboxJazCell.Width, checkboxJazCell.Height)
            With checkboxJaz
              .Caption = ""
 #2           .GroupBox = groupBoxOptionButtons
            End With
        Next d

我希望通过设置GroupBox属性(请参阅#1或#2)将选项按钮分配给当前行的组。 但这两种方法只是给我一个错误说

'对象不支持属性或方法'。

欢迎任何帮助或提示;-) 根据 snb 的提示,我修改了我的功能:

Sub AddOptionButtons()
    ActiveSheet.OptionButtons.Delete
    For d = 1 To 31
        Set checkboxKrankCell = Range("H" + Trim(Str(d + 4)))
        Set checkboxUrlaubCell = Range("I" + Trim(Str(d + 4)))
        Set checkboxJazCell = Range("J" + Trim(Str(d + 4)))
        option1Name = "Krank_" + Trim(Str(d))
        option2Name = "Urlaub_" + Trim(Str(d))
        option3Name = "Jaz_" + Trim(Str(d))
        Set checkboxKrank = ActiveSheet.OptionButtons.Add(checkboxKrankCell.Left, checkboxKrankCell.Top - 1, checkboxKrankCell.Width, checkboxKrankCell.Height)
        With checkboxKrank
          .Caption = ""
          .Name = option1Name
        End With
        Set checkboxUrlaub = ActiveSheet.OptionButtons.Add(checkboxUrlaubCell.Left, checkboxUrlaubCell.Top - 1, checkboxUrlaubCell.Width, checkboxUrlaubCell.Height)
        With checkboxUrlaub
          .Caption = ""
          .Name = option2Name
        End With
        Set checkboxJaz = ActiveSheet.OptionButtons.Add(checkboxJazCell.Left, checkboxJazCell.Top - 1, checkboxJazCell.Width, checkboxJazCell.Height)
        With checkboxJaz
          .Caption = ""
          .Name = option3Name
        End With
        ActiveSheet.Shapes.Range(Array(option1Name, option2Name, option3Name)).Group
    Next d
End Sub


我使用Shapes.Range(...)没有任何错误。组。 但是,表格上的所有选项按钮都是互斥的。
似乎分组在这里不起作用。

2 个答案:

答案 0 :(得分:1)

在空工作簿上尝试以下代码。根据我的理解,它会为你提供一个选择,每行只选择一个选项按钮,这是你想要的(我还创建了一个链接的单元格引用,以防万一你想采取进一步的行动,给出选择用户。):

Sub AddOptionButtons()
    Dim btn1 As OptionButton
    Dim btn2 As OptionButton
    Dim btn3 As OptionButton
    Dim grbox As GroupBox
    Dim t As Range
    Dim s As Range
    Dim p As Range
    Dim i As Integer

    ActiveSheet.OptionButtons.Delete
    ActiveSheet.GroupBoxes.Delete
    For i = 5 To 35 Step 1
        Set t = ActiveSheet.Range(Cells(i, 8), Cells(i, 8))
        Set s = ActiveSheet.Range(Cells(i, 9), Cells(i, 9))
        Set p = ActiveSheet.Range(Cells(i, 10), Cells(i, 10))
        Set btn1 = ActiveSheet.OptionButtons.Add(t.Left, t.Top, t.Width, t.Height)
        Set btn2 = ActiveSheet.OptionButtons.Add(s.Left, s.Top, s.Width, s.Height)
        Set btn3 = ActiveSheet.OptionButtons.Add(p.Left, p.Top, p.Width, p.Height)
        Set grbox = ActiveSheet.GroupBoxes.Add(t.Left, t.Top, t.Width + 100, t.Height)
        With btn1
          .Caption = ""
          .Display3DShading = True
          .LinkedCell = "M" & i
        End With

        With btn2
          .Caption = ""
          .Display3DShading = True
        End With

        With btn3
          .Caption = ""
          .Display3DShading = True
        End With

        With grbox
          .Caption = ""
          .Visible = False
        End With
    Next i
End Sub

答案 1 :(得分:0)

我会用:

Sub M_snb()
  ReDim sn(2)

  For j = 1 To 2
   For jj = 1 To 3
    With Sheet1.OptionButtons.Add(Cells(j, jj).Left, Cells(j, jj).Top - 1, Cells(j, jj).Width, Cells(j, jj).Height)
     sn(jj - 1) = .Name
    End With
   Next
   Sheet1.Shapes.Range(sn).Group
  Next
End Sub