+ Reply to Thread
Results 1 to 11 of 11

Running a macro from combobox selection

  1. #1
    Registered User
    Join Date
    07-24-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    6

    Running a macro from combobox selection

    Hello there,

    So this is my first attempt at using VB, besides using record to create a macro, and I am having issues.

    I have an ActiveX ComboBox with a list having the values: 25 Stages, 26 Stages, 27 Stages......38 Stages.
    I would like for a specific macro to run when I select each option in the combobox. I have recorded these macros and named them Stages_25, Stages_26, so on and so forth.
    When I select 25 Stages in the combobox I would like for the macro Stages_25 to run.

    Can anyone help me with this?

    I right clicked the combobox (ComboBox1) and entered this in to try to see if I can get the first one to run;


    Private Sub ComboBox1_Change()
    Select Case ComboBox1.Value
    Case "25%Stages"
    Call Stages_25
    End Select

    End Sub


    Keep in mind I am completely virgin to VB, Thanks!!

    Josh

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: Running a macro from combobox selection

    Welcome to the board!

    You say you have values like 25 Stages, but your Case is looking for "25%Stages". They have to match exactly, even letter case.

    Add a breakpoint to the Select Case line and step through it to see what's going on.
    David
    (*) Reputation points appreciated.

  3. #3
    Registered User
    Join Date
    07-24-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Running a macro from combobox selection

    I changed it to "25 Stages" and it worked! From looking at other code I had assumed you put the % in place of spaces. I'll go through and try to add all the other items in and let you know if I have any more issues.

    Thanks, David

  4. #4
    Registered User
    Join Date
    07-24-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Running a macro from combobox selection

    Okay, now it is giving me an error after adding in my other commands.

    The error is- Compile error: Sub or Function not defined

    This is what I have,


    Private Sub ComboBox1_Change()

    Select Case ComboBox1.Value
    Case "25 Stages"
    Call Stages_25
    Case "26 Stages"
    Call Stages_26
    Case "27 Stages"
    Call Stages_27
    Case "28 Stages"
    Call Stages_28
    Case "29 Stages"
    Call Stages_29
    Case "30 Stages"
    Call Stages_30
    Case "31 Stages"
    Call Stages_31
    Case "32 Stage"
    Call Stages_32
    Case "33 Stages"
    Call Stages_33
    Case "34 Stages"
    Call Stages_34
    Case "35 Stages"
    Call Stages_35
    Case "36 Stages"
    Call Stages_36
    Case "37 Stages"
    Call Stages_37
    End Select

    End Sub




    It highlights the Private Sub line when giving me the error.

    Thanks

  5. #5
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: Running a macro from combobox selection

    Press F8 when it stops and see if you can progress any further in the code.

  6. #6
    Registered User
    Join Date
    07-24-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Running a macro from combobox selection

    When pressing F8 it continuously pops up the same error message.

  7. #7
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: Running a macro from combobox selection

    And is the combo box named Combobox1? If not, go into design mode and double click the combobox. Then copy all of the code into that sub.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Running a macro from combobox selection

    What are the actual names of the subs and where are they located?

    By the way, what do the subs do?

    Are they basically the same apart from the no of stages involved?
    If posting code please use code tags, see here.

  9. #9
    Registered User
    Join Date
    07-24-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Running a macro from combobox selection

    I got it all working, David. Thanks a lot for your help!

  10. #10
    Registered User
    Join Date
    07-24-2013
    Location
    Houston, TX
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Running a macro from combobox selection

    My subs were basically reformatting a table of data. Deleting the extra data when those stages weren't included, drag and dropping cells, and then reformatting the borders to fit. Got it working now!

    Thanks

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Running a macro from combobox selection

    You could probably do everything with one sub, passing the no of stages as an argument to it.

+ 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] Macro not running through each checkbox selection
    By narrowgate88 in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 02-01-2013, 03:50 PM
  2. how to assign a different macro to each*combobox selection in form control....
    By kritigupta in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-03-2012, 03:04 AM
  3. Selection change macro keeps running
    By Subject_Name_Here in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-02-2012, 05:17 PM
  4. Running 1 macro on a selection of cells
    By rjm12 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-01-2011, 07:26 AM
  5. Running a macro on combo box selection
    By Nashy19 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-16-2008, 02:38 PM

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