[英]How to access (check or uncheck) radio(option) button in active sheet by c# vsto addin
[英]How to check if Option Button exists on sheet?
我有一個包含多張工作表的工作簿,我在其中使用不同腳本的選項按鈕處於活動狀態。
並非所有按鈕都用於所有工作表。 它們寫在一個子中,所以如果我不在工作表上使用選項按鈕,除非我使用On Error Resume Next
,否則Set shp
會給我一個錯誤,因為它找不到按鈕。
我怎樣才能擺脫On Error Resume Next
? 我想到了一些事情,例如檢查選項按鈕是否存在,並且僅當工作表上存在按鈕時才使用Set 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
我會建議這種方法。 希望能幫助到你:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.