+ Reply to Thread
Results 1 to 19 of 19

Running the code in an option button from a seperate normal button click

  1. #1
    Registered User
    Join Date
    08-29-2015
    Location
    Scotland
    MS-Off Ver
    2013
    Posts
    13

    Running the code in an option button from a seperate normal button click

    Hi,

    I have a series of normal buttons and option buttons in my excel spreadsheet, i would like to edit my normal button macro so that when the button is clicked it will 1) figure out which option button is selected at the time and then 2) run the macro associated with that option button.

    I'm sure there must be a simple way to do this???

    Thanks,
    Cam

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Running the code in an option button from a seperate normal button click

    Try something like this...

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    08-29-2015
    Location
    Scotland
    MS-Off Ver
    2013
    Posts
    13

    Re: Running the code in an option button from a seperate normal button click

    Runtime Error 424: Object Required....

    what does that mean?

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Running the code in an option button from a seperate normal button click

    Let's take a step back.

    First, there are two types of controls you can have on a worksheet; ActiveX-type and Form-type.

    Overview Form controls and ActiveX controls on a worksheet

    What type of Command Button and Option Button controls are you using; ActiveX-type or Form-type?

    What are the names of the controls?

    What are the names of the macros you want to call for each Option Button?

  5. #5
    Registered User
    Join Date
    08-29-2015
    Location
    Scotland
    MS-Off Ver
    2013
    Posts
    13

    Re: Running the code in an option button from a seperate normal button click

    Sorry for the slow reply,

    Okay so i am using Form-Type Command and Option Buttons.

    I have 2 command buttons, one with the macro "Plus" and one with the macro "Minus" that add and subtract respectively a value of 1 from the cell they are in.

    I have 6 option buttons attached to 6 macros called "NoRake", "AveRake", "RatioedRake", "NormalRake", "TurboRake" and "HyperRake"

    Each of these macros attached to the option buttons perform slightly different calculations on a series of cells.

    That all works fine

    However, when i adapt the data (using the command buttons), these calculations are not recalculated until i reclick the option button.

    What i would like, is for this recalculation to be done as soon as i adapt the data. Ie. When i click on the command button.

    Does that make it a bit clearer?

    Cam

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Running the code in an option button from a seperate normal button click

    Add this to the bottom of your Plus and Minus macros. Change the names of the "Option Buttons" to suit. Don't use the option button captions. Use thier names as seen in the name box to the left of the formula bar.

    Please Login or Register  to view this content.

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Running the code in an option button from a seperate normal button click

    Something like

    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  8. #8
    Registered User
    Join Date
    08-29-2015
    Location
    Scotland
    MS-Off Ver
    2013
    Posts
    13

    Re: Running the code in an option button from a seperate normal button click

    Amazing AlphaFrog thank you so much!

  9. #9
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Running the code in an option button from a seperate normal button click

    Quote Originally Posted by camikhazi View Post
    Amazing AlphaFrog thank you so much!
    You're welcome. Thanks for the feedback.

  10. #10
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Running the code in an option button from a seperate normal button click

    Thanks a lot Mr. AlphaFrog for this great code
    Mr. Mike I am trying to test your code but I encountered excel crash
    Here's my attempt to test your code. Can you tell me what's wrong with it?
    Attached Files Attached Files
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  11. #11
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Running the code in an option button from a seperate normal button click

    Quote Originally Posted by YasserKhalil View Post
    Thanks a lot Mr. AlphaFrog for this great code
    Mr. Mike I am trying to test your code but I encountered excel crash
    Here's my attempt to test your code. Can you tell me what's wrong with it?
    Assign your TestA, TestB, and TestC macros to each option button and not your Mike macro.

  12. #12
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Running the code in an option button from a seperate normal button click

    Thanks a lot Mr. AlphaFrog for reply
    So what is the benfit of Mr. Mike's code?
    I thought it would be deal with any option button according to its caption...

    As for your code I assigned one sub (named it AlphaFrog) to all option buttons and it works well and call proper sub ..

  13. #13
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Running the code in an option button from a seperate normal button click

    Quote Originally Posted by YasserKhalil View Post
    Thanks a lot Mr. AlphaFrog for reply
    So what is the benfit of Mr. Mike's code?
    I don't know. You would have to ask Mike.

    I suppose Mike's is not dependent on hard-coding the option button names to their macros. If you wanted to change the assigned macro for a particular option button, you wouldn't have to change the code.

    I would suggest it's a little more concise to use an OptionButton object and the OptionButtons collection instead of looping through the Shapes collection. This does the same thing.

    Please Login or Register  to view this content.

  14. #14
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Running the code in an option button from a seperate normal button click

    But the last code you provided cause excel crash too ..

  15. #15
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Running the code in an option button from a seperate normal button click

    Quote Originally Posted by YasserKhalil View Post
    But the last code you provided cause excel crash too ..
    The last code I provided does the exact same thing as Mike's code. I was simply showing a shorter way to do the exact same thing. So if you didn't change the assigned macros as I stated in post #11, then both Mike's and my shorter version of Mike's will throw an error.

  16. #16
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Running the code in an option button from a seperate normal button click

    I read your problem as that you have existing Option buttons, each of which has an associated Macro. And you wanted a Macro that you could assign to a separate Command button. The Option Buttons should not call the code I posted.
    See attached
    Attached Files Attached Files

  17. #17
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Running the code in an option button from a seperate normal button click

    Thanks a lot Mr. Mike
    So I have to assign a macro for each option button first ..

    Is there a way to assign all option buttons to one sub and call this sub based on the caption of the option button..?

  18. #18
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Running the code in an option button from a seperate normal button click

    Sure,
    Something like this maybe. (note that this must be called by activating the control, pressing F5 while testing will cause an error)
    Please Login or Register  to view this content.

  19. #19
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: Running the code in an option button from a seperate normal button click

    Thanks a lot Mr. Mike for this great and wonderful help
    I tested the following line and everything is ok
    Please Login or Register  to view this content.

+ 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. [SOLVED] userform to select option button without running the code that's in it
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 04-22-2015, 04:09 PM
  2. Macro Option Button Click Cannot be Found Error
    By src16 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-06-2013, 08:42 PM
  3. Replies: 1
    Last Post: 02-26-2013, 08:33 PM
  4. Option button click ... then focus back to sheet ?
    By dannac in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-06-2012, 02:05 AM
  5. Replies: 0
    Last Post: 07-18-2012, 09:45 AM
  6. [SOLVED] Check Criteria On Button Click Before Running Macro
    By swordswinger710 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 06-06-2012, 04:56 PM
  7. Option button click with if else statment
    By peedarp1985 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-03-2010, 12:11 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