2011-10-02 336 views
2

btexcel vba - 檢查單選按鈕是否被選中?

我想檢查這些簡單的單選按鈕組的值,但我的語法關閉,有誰知道該怎麼改? 注意:它們是不包含ActiveX的Excel選項按鈕,它們不在用戶窗體上。

If Worksheets("Input").Shapes("Option Button 3").Select.Value = xlOn Then 
    MsgBox "fir" 
ElseIf Worksheets("Input").Shapes("Option Button 4").Select.Value = xlOn Then 
    MsgBox "sec" 
Else 
    MsgBox "none" 'in case they were deleted off the sheet 
End If 

回答

6

試試這個

Sub ZX() 
    Dim shp3 As Shape 
    Dim shp4 As Shape 

    On Error Resume Next 
    Set shp3 = Worksheets("Input").Shapes("Option Button 3") 
    Set shp4 = Worksheets("Input").Shapes("Option Button 4") 
    If shp3 Is Nothing Then 
     If shp4 Is Nothing Then 
      MsgBox "none" 'in case they were deleted off the sheet 
     ElseIf shp4.ControlFormat.Value = xlOn Then 
      MsgBox "sec" 
     Else 
      MsgBox "Only Button 4 exists and it is off" 
     End If 
    Else 
     If shp3.ControlFormat.Value = xlOn Then 
      MsgBox "fir" 
     Else 
      If shp4 Is Nothing Then 
       MsgBox "Only Button 3 exists and it is off" 
      ElseIf shp4.ControlFormat.Value = xlOn Then 
       MsgBox "sec" 
      Else 
       MsgBox "Both exists, both are off" 
      End If 
     End If 
    End If 

End Sub 
相關問題