替你問Claude AI
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim opt As OptionButton
Set opt = ws.OptionButtons("OptionButton1")
opt.Value = True
End Sub
一樣會出現 應用程式或物件定義錯誤
但我問ChatGPT得出答案了
Sub ControlOptionButton()
Dim ws As Worksheet
Dim optBtn As Object
' 引用Sheet2
Set ws = ThisWorkbook.Sheets("Sheet2")
' 检查Sheet2上的OptionButton1是否存在
On Error Resume Next
Set optBtn = ws.OLEObjects("OptionButton1").Object
On Error GoTo 0
If Not optBtn Is Nothing Then
' 控制OptionButton1的操作
optBtn.Value = True ' 设置OptionButton1为选中状态
' 或者进行其他操作,比如获取其状态:optBtn.Value
Else
MsgBox "Sheet2上找不到名称为OptionButton1的控件。"
End If
End Sub