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 选项按钮索引问题_Excel_Radio Button_Vba - Fatal编程技术网

Excel 选项按钮索引问题

Excel 选项按钮索引问题,excel,radio-button,vba,Excel,Radio Button,Vba,我在每行的前五个单元格上有五个选项按钮(表单控件)。选项按钮分别链接到每行的第六个单元格 我想得到第六个单元格中的索引号 (Option Button表单控件将指示其状态的选项按钮的值返回到链接单元格。) 现在问题是从第二行开始,选项按钮的索引编号大于5 所以第六个单元格中的值大于5。我希望它们在1到5之间 (索引是OptionButton类的只读属性) 如何做到这一点?(最好没有vba) 感谢您的关注。如评论中所述,在每套按钮周围使用一个分组框将允许每套按钮独立运行。在下面的图片中,我删除了G

我在每行的前五个单元格上有五个选项按钮(表单控件)。选项按钮分别链接到每行的第六个单元格 我想得到第六个单元格中的索引号

(Option Button表单控件将指示其状态的选项按钮的值返回到链接单元格。)

现在问题是从第二行开始,选项按钮的索引编号大于5 所以第六个单元格中的值大于5。我希望它们在1到5之间

(索引是OptionButton类的只读属性)

如何做到这一点?(最好没有vba)


感谢您的关注。

如评论中所述,在每套按钮周围使用一个分组框将允许每套按钮独立运行。在下面的图片中,我删除了GroupBox的名称,并将GroupBox的Board与cell boarders对齐,然后使用cell boarders几乎遮住了GroupBox,因此除了主动选择的一个(最上面的一个)之外,您无法分辨它们是否在那里:

根据要求,这里有一些VBA代码。我建议在运行此代码之前,将行的高度设置为至少20点。由于groupbox的最小高度为19.5,单元格的宽度也将用于确定OptionButtons的宽度,因此请确保使用的宽度足以容纳OptionButton的文本

Sub Sample()

Dim Top As Variant, Left As Variant, Height As Variant, Width As Variant
Dim rngActiveRowA As Range, rngEndOfBox As Range
Dim lngActiveRow As Long, lngActiveColumn As Long

With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    .EnableEvents = False
End With

For lngActiveRow = 1 To 5

Set rngActiveRowA = Range("A" & lngActiveRow)

Set rngEndOfBox = Range("F" & lngActiveRow + 1)

Top = rngActiveRowA.Top
Left = rngActiveRowA.Left
Height = rngEndOfBox.Top - Top
Width = rngEndOfBox.Left - Left

ActiveSheet.GroupBoxes.Add(Left, Top, Width, Height).Caption = ""

    For lngActiveColumn = 1 To 5

    With ActiveSheet
        Top = .Cells(lngActiveRow, lngActiveColumn).Top
        Left = .Cells(lngActiveRow, lngActiveColumn).Left
        Height = .Cells(lngActiveRow + 1, lngActiveColumn + 1).Top - Top
        Width = .Cells(lngActiveRow + 1, lngActiveColumn + 1).Left - Left
    End With

    With ActiveSheet.OptionButtons.Add(Left, Top, Width, Height)
        .Characters.Text = "OB" & lngActiveColumn
        .LinkedCell = "$F$" & lngActiveRow
    End With

    Next lngActiveColumn

Next lngActiveRow


With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
    .EnableEvents = True
End With

End Sub

此代码将在前5行的每行的前5列周围创建一个groupbox。它还将在该区域的每个单元格内填充一个选项按钮,该区域链接到相应行中的F单元格。并将每个按钮重命名为OB+它所在的列号

这似乎不起作用。但是,如果您为相关的选项按钮放置一个组框控件,它将按照您希望的方式工作。正如@shahkalpesh所述,每个独立的选项按钮组都必须位于它们自己的组框内。感谢您的回复。这张照片正是我要找的。是否可以使用vba创建此groupbox和optionbutton?如果是,你能分享代码吗?@Santosh根据要求,我已经更新了我的答案,加入了VBA.Super示例。谢谢。