+ Reply to Thread
Results 1 to 14 of 14

Show button based on OptionButton being selected

  1. #1
    Registered User
    Join Date
    09-16-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    8

    Show button based on OptionButton being selected

    Hi all,

    I only have quite a basic knowledge of excel and am new to macros and buttons. I'd really appreciate some help with the following issue:

    I have 3 option buttons on Sheet 3 (named OptionButton3, OptionButton4 and OptionButton5)
    On sheet 4, there is a Button (a grey hyperlinked one) called 'Button 33'.
    However, I only want Button 33 to appear when the OptionButton3 or OptionButton5 are selected.

    So far, I have got as far as:

    Please Login or Register  to view this content.
    Written into the code for Sheet 3.

    But this doesn't work - when I select OptionButton3 I get an error message ('Cannot run the macro "Tool Mock Up - pesticides 1.xlsm!OptionButton3_Click'. The macro may not be available in this workbook or all macros may be disabled'). Macros are enabled in the workbook.

    Any help very much appreciated.

    Moderator Note:

    Pls use code tags around your code as per forum rules.
    Last edited by niltiac88; 09-17-2013 at 12:57 PM.

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Show button based on OptionButton being selected

    Hi

    remove "Private" from the sub.
    Please use [CODE]-TAGS
    When your problem is solved mark the thread SOLVED
    If an answer has helped you please click to give reputation
    Read the FORUM RULES

  3. #3
    Registered User
    Join Date
    09-16-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Show button based on OptionButton being selected

    Hi,
    Thanks very much for the response. I've tried doing this and then right clicking OptionButton3 and choosing the correct name under 'assign macro', however, I get the message "! Object required".

    Any ideas why this is?

  4. #4
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Show button based on OptionButton being selected

    Please check this example file containing ActiveX controls: Opt and Cmd.xlsm

  5. #5
    Registered User
    Join Date
    09-16-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Show button based on OptionButton being selected

    Hi, I replaced the buttons in my workbook with activeX buttons and copy+pasted the code from the doc you sent. I altered the names appropriately (b/c i replaced the buttons with new ActiveX buttons, and have added an new sheet earlier in the workbook).

    When I run this now, I get the message: 'Runtime error '9': Subscript out of range', and when I click 'debug' it highlights the following line in the code in yellow:
    'For Each objOLE In ThisWorkbook.Worksheets("Sheet5").OLEObjects'
    (the sheet has become sheet 5 because of the new sheet added)

    Again, I'm afraid I can't spot what the issue might be.

    Thanks for your help with this.

  6. #6
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Show button based on OptionButton being selected

    Hi,

    The code is looking for the button on the worksheet with the name "Sheet5", if your sheet has a different name please adapt the code accordingly.

    Alternatively you can use the Code-name of the sheet, that is the name displayed in the VBA project explorer before the bracket. e.g. Sheet5 (MyWorksheet) where "MyWorksheet" is the name displayed in the tab of excel.

  7. #7
    Registered User
    Join Date
    09-16-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Show button based on OptionButton being selected

    Aha! I got it; I was naming the sheet based on its default name/number/position, rather than the name I had given it.

    Thank you very much for your help.

  8. #8
    Registered User
    Join Date
    09-16-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Show button based on OptionButton being selected

    Hi, one last quick question; in reality I will probably have several buttons on sheet 5, most of which will need to be visible/invisible based on the same OptionButton selections as CommandButton1.
    I tried to modify the code adding an 'or' option, but have evidently not got it quite right:

    Please Login or Register  to view this content.
    I'd want to add CommandButton2, CommandButton3, and CommandButton4 to the above code.

    I'd also like CommandButton 5 to instead be visible when OptionButton1 is FALSE and OptionButton2 is TRUE or OptionButton3 is TRUE.

    If you can show me how/where to edit the code, I'd be very grateful. Many thanks.
    Last edited by niltiac88; 09-17-2013 at 11:40 AM. Reason: Adding code tags.

  9. #9
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Show button based on OptionButton being selected

    Hi,

    there are lots of possibilities to handle this, I'd do it like this where:
    Op1 checked = Profile 1 --> cmd1-4 visible, cmd5 hidden
    Op2 checked = Profile 2 --> cmd1-4 hidden, cmd5 visible
    Op3 checked = Profile 3 --> all cmd's visible

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    09-16-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Show button based on OptionButton being selected

    Thanks, this works great.

  11. #11
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Show button based on OptionButton being selected

    New member here and i don't think that you start correctly choosing to ignore Moderators request.

    I kindly asked for you to add code tags around your codes in your first post. 3 hours! later you post without code tags..Post# 8

    It;s a warning now. Pls don't do this next time..

    Also.
    As that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thank you.

    Also, as a new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

  12. #12
    Registered User
    Join Date
    09-16-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Show button based on OptionButton being selected

    (Apologies again for missing the code tags0.
    I have one small issue with this code still; it was working fine for the first few tests and I added some more buttons to the worksheet, and successfully got the code to work for these too.
    However, for some reason, when I select the Option buttons now, I get an error message: Runtime error '1004': Unable to get the OLEObjects property of the Worksheet class.

    When I click debug, it highlights the red line of code below:
    Please Login or Register  to view this content.
    When I click on/hover over (aCmd(i)), it says aCmd(i) = "CommandButton7" which is only halfway through the array.

    I have made sure that ActiveX is enabled. Also, I don't know if it could be related but each time i open the file i get a security warning that says 'Automatic update of links has been disabled'. Whether I enable or choose 'Help me protect...', I still get the Runtime error.

    If you can help, it would be much appreciated.

  13. #13
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Show button based on OptionButton being selected

    Hi,

    I assume that there is no ActiveX button named "CommandButton7" on the worksheet.

  14. #14
    Registered User
    Join Date
    09-16-2013
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    8

    Re: Show button based on OptionButton being selected

    You're right. I'd removed a button and forgotten to update the code.

    Many thanks for all your help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Grey out check box based on a selected radio button
    By tlacloche in forum Excel General
    Replies: 16
    Last Post: 08-28-2013, 05:36 AM
  2. Replies: 10
    Last Post: 02-12-2013, 01:52 AM
  3. Replies: 2
    Last Post: 01-30-2013, 03:16 AM
  4. [SOLVED] Unhide sheets based on which Button is selected
    By johnw993 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-29-2013, 04:57 AM
  5. Formatting text of Forms.OptionButton (radio button)
    By Linking to specific cells in pivot table in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 08-16-2005, 08:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1