简体   繁体   English

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

[英]How to add option buttons to group in Excel 2010 sheet using VBA?

I want to add many option button to an excel worksheet (not to a VBA-form) and want to group them by row. 我想向Excel 工作表 (而不是VBA表单)添加许多选项按钮,并希望按行对它们进行分组。 The result should look something like this: 结果应如下所示:
在此处输入图片说明

Here is the code I'm using so far: 这是到目前为止我正在使用的代码:

    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

I would expect to assign the option buttons to the group for the current row by setting the GroupBox property (see #1 or #2). 我希望通过设置GroupBox属性(请参阅#1或#2)将选项按钮分配给当前行的组。 But both methods just gave me an error saying 但是两种方法都给我一个错误的说法

'The object does not support the property or methode'. “对象不支持属性或方法”。

Any help or hint is welcome ;-) 任何帮助或提示,欢迎;-)
Based on the tip from snb I have modified my function like this: 基于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 


I don't get any errors using Shapes.Range(...).Group. 使用Shapes.Range(...)。Group我没有任何错误。 But still all option buttons from on the sheet are all mutual exclusive. 但是,工作表上的所有选项按钮仍然都是互斥的。
Seems grouping does not work here. 似乎分组在这里不起作用。

Try the following code on an empty workbook. 在空的工作簿上尝试以下代码。 It will give you an option to choose only ONE optionbutton on each row, which is what you want, as far as I understood (I also created a linked cell reference, just in case you would like to take further action, given the choice of a user.): 据我了解,它将为您提供一个选择,即每行仅选择一个选项按钮(据我所知)(我还创建了一个链接的单元格引用,以防万一,如果您希望采取进一步的措施,请选择用户。):

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

I'd use: 我会用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM