VBA Excel 在我删除另一个 RadioButton 并撤消之前,RadioButton 无法正常工作
VBA Excel RadioButton does not work properly until i delete another RadioButton and undo
我目前正在 VBA 中处理 Option-buttons。我想添加一个Group-box,其中有6个Option-buttons(都连接到某处的固定单元格)。最多只有 Option-buttons 中的一个可以激活。
它应该是这样的:
我想用 VBA 宏创建它。问题是,当我创建它时,Button-1 和 Button-6 已连接。如果我 select 一个,我 select 两个。其他的Radio-buttons都不是这样的,可以单独点击
此外,如果我手动删除其中一个 Radio-buttons,然后单击 Ctrl+Y,一切正常。然后所有 6 个按钮都可以单独 selected。
你知道我能做什么吗,这样我就不必先删除一个按钮然后撤消它来使它正常工作?
编辑:添加代码
Sub ButtonsInABox()
ActiveSheet.GroupBoxes.Delete
ActiveSheet.OptionButtons.Delete
startcell = Array(1, 1)
Add_GroupBox Array(startcell(0), startcell(1))
Add_RadioButton Array(startcell(0), startcell(1)), "Button-1", "A11"
Add_RadioButton Array(startcell(0), startcell(1) + 1), "Button-2", "A11"
Add_RadioButton Array(startcell(0), startcell(1) + 2), "Button-3", "A11"
Add_RadioButton Array(startcell(0) + 1, startcell(1)), "Button-4", "A11"
Add_RadioButton Array(startcell(0) + 1, startcell(1) + 1), "Button-5", "A11"
Add_RadioButton Array(startcell(0) + 1, startcell(1) + 2), "Button-6", "A11"
End Sub
Sub Add_RadioButton(startcell, ButtonName, corresponding_cell)
a = startcell(0)
b = startcell(1)
xx = Cells(a, b).Address(RowAbsolute:=False, ColumnAbsolute:=False)
ActiveSheet.OptionButtons.Add(Range(xx).Left, Range(xx).Top, Range(xx).Width * 1, 4).Select
With Selection
.ShapeRange.ScaleHeight 0.65, msoFalse
.Characters.Text = ButtonName
.LinkedCell = corresponding_cell
.Display3DShading = True
End With
End Sub
Sub Add_GroupBox(startcell)
a = startcell(0)
b = startcell(1)
xx = Cells(a, b).Address(RowAbsolute:=False, ColumnAbsolute:=False)
ActiveSheet.GroupBoxes.Add(Range(xx).Left, Range(xx).Top, Range(xx).Width * 3, Range(xx).Height * 2).Select
Selection.Characters.Text = ""
End Sub
您遇到问题是因为线路
ActiveSheet.GroupBoxes.Add(Range(xx).Left, Range(xx).Top, _
Range(xx).Width * 3, Range(xx).Height * 2).Select
如果你把它改成
ActiveSheet.GroupBoxes.Add(Range(xx).Left, Range(xx).Top, _
Range(xx).Width * 3.5, Range(xx).Height * 2.5).Select
它会起作用的。似乎 Excel 对对象是否在组内感到困惑。最好在选项按钮周围包含额外的边距。
顺便说一下,避免使用 .Select
。直接使用对象,如下例所示。
备选
如果您愿意,还有另一种方法可以实现您的目标。此方法不使用 GroupBox
。它将控件组合在一起。看这个例子
Sub Sample()
Dim shp As Variant
Dim ShpGroup As Variant
Dim ws As Worksheet
Dim startcell As Variant
Dim i As Long, j As Long, k As Long
Dim CellAddr As String
Dim Shapenames As String
Dim ShpAr(1 To 6) As Variant
Set ws = Sheet1
For Each shp In ws.Shapes
shp.Delete
Next shp
k = 1
For i = 1 To 2
For j = 1 To 3
CellAddr = Cells(i, j).Address(RowAbsolute:=False, ColumnAbsolute:=False)
Set shp = ws.OptionButtons.Add(Range(CellAddr).Left, Range(CellAddr).Top, Range(CellAddr).Width, 4)
With shp
.Name = "Button " & k
.ShapeRange.ScaleHeight 0.65, msoFalse
.Characters.Text = "Button " & k
.LinkedCell = "A11"
.Display3DShading = True
End With
'~~> Store the shape name in an array
ShpAr(k) = "Button " & k
k = k + 1
Next j
Next i
Set ShpGroup = ws.Shapes.Range(ShpAr).Group
End Sub
我目前正在 VBA 中处理 Option-buttons。我想添加一个Group-box,其中有6个Option-buttons(都连接到某处的固定单元格)。最多只有 Option-buttons 中的一个可以激活。
它应该是这样的:
我想用 VBA 宏创建它。问题是,当我创建它时,Button-1 和 Button-6 已连接。如果我 select 一个,我 select 两个。其他的Radio-buttons都不是这样的,可以单独点击
此外,如果我手动删除其中一个 Radio-buttons,然后单击 Ctrl+Y,一切正常。然后所有 6 个按钮都可以单独 selected。
你知道我能做什么吗,这样我就不必先删除一个按钮然后撤消它来使它正常工作?
编辑:添加代码
Sub ButtonsInABox()
ActiveSheet.GroupBoxes.Delete
ActiveSheet.OptionButtons.Delete
startcell = Array(1, 1)
Add_GroupBox Array(startcell(0), startcell(1))
Add_RadioButton Array(startcell(0), startcell(1)), "Button-1", "A11"
Add_RadioButton Array(startcell(0), startcell(1) + 1), "Button-2", "A11"
Add_RadioButton Array(startcell(0), startcell(1) + 2), "Button-3", "A11"
Add_RadioButton Array(startcell(0) + 1, startcell(1)), "Button-4", "A11"
Add_RadioButton Array(startcell(0) + 1, startcell(1) + 1), "Button-5", "A11"
Add_RadioButton Array(startcell(0) + 1, startcell(1) + 2), "Button-6", "A11"
End Sub
Sub Add_RadioButton(startcell, ButtonName, corresponding_cell)
a = startcell(0)
b = startcell(1)
xx = Cells(a, b).Address(RowAbsolute:=False, ColumnAbsolute:=False)
ActiveSheet.OptionButtons.Add(Range(xx).Left, Range(xx).Top, Range(xx).Width * 1, 4).Select
With Selection
.ShapeRange.ScaleHeight 0.65, msoFalse
.Characters.Text = ButtonName
.LinkedCell = corresponding_cell
.Display3DShading = True
End With
End Sub
Sub Add_GroupBox(startcell)
a = startcell(0)
b = startcell(1)
xx = Cells(a, b).Address(RowAbsolute:=False, ColumnAbsolute:=False)
ActiveSheet.GroupBoxes.Add(Range(xx).Left, Range(xx).Top, Range(xx).Width * 3, Range(xx).Height * 2).Select
Selection.Characters.Text = ""
End Sub
您遇到问题是因为线路
ActiveSheet.GroupBoxes.Add(Range(xx).Left, Range(xx).Top, _
Range(xx).Width * 3, Range(xx).Height * 2).Select
如果你把它改成
ActiveSheet.GroupBoxes.Add(Range(xx).Left, Range(xx).Top, _
Range(xx).Width * 3.5, Range(xx).Height * 2.5).Select
它会起作用的。似乎 Excel 对对象是否在组内感到困惑。最好在选项按钮周围包含额外的边距。
顺便说一下,避免使用 .Select
。直接使用对象,如下例所示。
备选
如果您愿意,还有另一种方法可以实现您的目标。此方法不使用 GroupBox
。它将控件组合在一起。看这个例子
Sub Sample()
Dim shp As Variant
Dim ShpGroup As Variant
Dim ws As Worksheet
Dim startcell As Variant
Dim i As Long, j As Long, k As Long
Dim CellAddr As String
Dim Shapenames As String
Dim ShpAr(1 To 6) As Variant
Set ws = Sheet1
For Each shp In ws.Shapes
shp.Delete
Next shp
k = 1
For i = 1 To 2
For j = 1 To 3
CellAddr = Cells(i, j).Address(RowAbsolute:=False, ColumnAbsolute:=False)
Set shp = ws.OptionButtons.Add(Range(CellAddr).Left, Range(CellAddr).Top, Range(CellAddr).Width, 4)
With shp
.Name = "Button " & k
.ShapeRange.ScaleHeight 0.65, msoFalse
.Characters.Text = "Button " & k
.LinkedCell = "A11"
.Display3DShading = True
End With
'~~> Store the shape name in an array
ShpAr(k) = "Button " & k
k = k + 1
Next j
Next i
Set ShpGroup = ws.Shapes.Range(ShpAr).Group
End Sub