我有一个包含多个工作表的工作簿,我使用选项按钮激活不同的脚本。
我的问题是,并非所有的按钮都在所有的工作表上使用,并且它们是用单个子写的,所以如果我不在工作表上使用选项按钮,设置shp < / strong>会给我一个错误,因为它找不到按钮,除非我使用 On Error Resume Next
我的问题是,我怎么能摆脱 On Error Resume Next 如果可能的话?我教过类似检查是否有选项按钮,并且仅当工作表上有按钮时才使用设置shp ,但我无法使其正常工作。
Private Sub ob(opt As String)
Dim shp1 As Shape
Dim shp2 As Shape
Dim shp3 As Shape
Dim shp4 As Shape
Dim shp5 As Shape
On Error Resume Next ' <-I want to get rid of this
Set shp1 = ActiveSheet.Shapes("OptionButton1")
Set shp2 = ActiveSheet.Shapes("OptionButton2")
Set shp3 = ActiveSheet.Shapes("OptionButton3")
Set shp4 = ActiveSheet.Shapes("OptionButton4")
Set shp5 = ActiveSheet.Shapes("OptionButton5")
Select Case xlOn
Case shp1.ControlFormat.Value
opt = "ob1"
Case shp2.ControlFormat.Value
opt = "ob2"
Case shp3.ControlFormat.Value
opt = "ob3"
Case shp4.ControlFormat.Value
opt = "ob4"
Case shp5.ControlFormat.Value
opt = "ob5"
End Select
End Sub
谢谢
编辑:
在Ralph的版本之后,实际的脚本看起来像这样:
Private Sub ob(opt As String)
Dim shp1 As Shape
Dim shp2 As Shape
Dim shp3 As Shape
Dim shp4 As Shape
Dim shp5 As Shape
For Each ws In ActiveWorkbook.Worksheets
For Each shp In Worksheets(ws.Name).Buttons
Select Case btn.Name
Case "OptionButton1"
Set shp1 = ActiveSheet.Shapes("OptionButton1")
Case "OptionButton2"
Set shp2 = ActiveSheet.Shapes("OptionButton2")
Case "OptionButton3"
Set shp3 = ActiveSheet.Shapes("OptionButton3")
Case "OptionButton4"
Set shp4 = ActiveSheet.Shapes("OptionButton4")
Case "OptionButton5"
Set shp5 = ActiveSheet.Shapes("OptionButton5")
End Select
Next shp
Next ws
Select Case xlOn
Case shp1.ControlFormat.Value
opt = "ob1"
Case shp2.ControlFormat.Value
opt = "ob2"
Case shp3.ControlFormat.Value
opt = "ob3"
Case shp4.ControlFormat.Value
opt = "ob4"
Case shp5.ControlFormat.Value
opt = "ob5"
End Select
End Sub
我在线获得了对象所需的错误:
Select Case btn.Name
答案 0 :(得分:1)
我建议采用这种方法。希望它有所帮助:
Public Sub AllShapesOnAllSheets()
For Each btn In ActiveSheet.OptionButtons
Select Case btn.Name
Case "OptionButton1"
If btn.Value = xlOn Then opt = "ob1"
Case "OptionButton2"
If btn.Value = xlOn Then opt = "ob2"
Case "OptionButton3"
If btn.Value = xlOn Then opt = "ob3"
Case "OptionButton4"
If btn.Value = xlOn Then opt = "ob4"
Case "OptionButton5"
If btn.Value = xlOn Then opt = "ob5"
End Select
Next btn
End Sub