简体   繁体   中英

It is correct for hide or unhide a control by VBA in Excel 2010?

I have a sheet named "MainSheet" in a Workbook in Excel 2010.

this sheet included an activeX control named "OptionButton1". when the cell's value of "C18" is "2" then this control should be hide.

I wrote below code but does not work.

► a more question: can I have three activeX (Radio Button) related to one cell with three different value like radio button in form control?

Any advice is appreciated. :)

Sub MS_Method()

If Range("C18").Value = 2 Then

    ActiveSheet.MainSheet("OptionButton1").Visible = False

ElseIf Range("C18").Value = 1 Then

    ActiveSheet.MainSheet("OptionButton1").Visible = True

End If

End Sub

Assuming your ActiveX OptionButton is Named "OptionButton1", the code below (tested) will work:

Option Explicit

Sub MS_Method()

Dim Sht                 As Worksheet

' modify "MainSheet" to your sheet name (where you have your OptionButton)
Set Sht = ThisWorkbook.Sheets("MainSheet")

If Range("C18").Value = 2 Then
    Sht.OLEObjects("OptionButton1").Visible = False
ElseIf Range("C18").Value = 1 Then
    Sht.OLEObjects("OptionButton1").Visible = True
End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM