+ Reply to Thread
Results 1 to 6 of 6

How can I assign a macro to each name in a drop down list

  1. #1
    Registered User
    Join Date
    08-24-2005
    Posts
    11

    How can I assign a macro to each name in a drop down list

    I have macros for each name already written. But I want to select a name from a drop down list and have the designated macro run. And if I change the name I want that designated macro to run.

    Please explain how I can do this in as much detail as possible. I am not very familar with macros.

    Thanks for your help

  2. #2
    Jim Thomlinson
    Guest

    RE: How can I assign a macro to each name in a drop down list

    Create a validation list in Cell A1 (you can change this address but you will
    also have to change the code where it references "$A$1") by selecting Cell A1
    -> Data -> Validation. Now change any value to List and add your names to the
    list area provided (separated by commas or reference a range of cells with
    the name listed). Right click on the sheet tab and select view code. The VB
    editor will open up. Paste the following code into the code window... That
    should be it. You will need to modify the names and the call procedures to
    match you list and your macros.

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$1" Then
    Select Case Target.Value
    Case "Dave"
    Call Module1.DaveProcedure
    Case "Bob"
    Call Module1.BobProcedure
    Case "Tom"
    Call Module1.TomProcedure
    Case Else
    MsgBox Target.Value & " Caused an error"
    End Select
    End If
    End Sub
    --
    HTH...

    Jim Thomlinson


    "mgmcdevitt" wrote:

    >
    > I have macros for each name already written. But I want to select a name
    > from a drop down list and have the designated macro run. And if I change
    > the name I want that designated macro to run.
    >
    > Please explain how I can do this in as much detail as possible. I am
    > not very familar with macros.
    >
    > Thanks for your help
    >
    >
    > --
    > mgmcdevitt
    > ------------------------------------------------------------------------
    > mgmcdevitt's Profile: http://www.excelforum.com/member.php...o&userid=26592
    > View this thread: http://www.excelforum.com/showthread...hreadid=466940
    >
    >


  3. #3
    Gord Dibben
    Guest

    Re: How can I assign a macro to each name in a drop down list

    Assuming the drop-down list is one created using Data Validation.

    Copy/paste this code into the sheet module by right-click on sheet tab and
    "view code".

    Paste into that module.

    Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.Count > 1 Then Exit Sub
    On Error GoTo CleanUp
    Application.EnableEvents = False
    If Target.Address = "$E$1" Then
    Select Case LCase(Target.Value)
    Case "bob"
    bob 'run bob macro
    Case "donna"
    donna 'run donna macro
    Case "gord"
    gord 'run gord macro
    Case "pete"
    pete 'run pete macro
    End Select
    End If
    CleanUp:
    Application.EnableEvents = True
    End Sub

    Adjust the target address to suit and add cases to suit also.


    Gord Dibben Excel MVP

    On Mon, 12 Sep 2005 17:01:14 -0500, mgmcdevitt
    <[email protected]> wrote:

    >
    >I have macros for each name already written. But I want to select a name
    >from a drop down list and have the designated macro run. And if I change
    >the name I want that designated macro to run.
    >
    >Please explain how I can do this in as much detail as possible. I am
    >not very familar with macros.
    >
    >Thanks for your help



  4. #4
    Registered User
    Join Date
    08-24-2005
    Posts
    11

    Error With Suggested Code

    I had a "methog or data member not found"

    In my list an example of a name is "CO Wage". My corresponding macro is named COWage. How would the code look if I used these.

    I put in:
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$G$9" Then
    Select Case Target.Value
    Case "CO Wage"
    Call Module1.COWageProcedure
    Case Else
    MsgBox Target.Value & " Caused an error"
    End Select
    End If
    End Sub

    Isn't this correct?

  5. #5
    Rowan
    Guest

    Re: How can I assign a macro to each name in a drop down list

    If your macro is called COWage then change the line:

    Call Module1.COWageProcedure
    to
    Call Module1.COWage

    Regards
    Rowan

    "mgmcdevitt" wrote:

    >
    > I had a "methog or data member not found"
    >
    > In my list an example of a name is "CO Wage". My corresponding macro is
    > named COWage. How would the code look if I used these.
    >
    > I put in:
    > Private Sub Worksheet_Change(ByVal Target As Range)
    > If Target.Address = "$G$9" Then
    > Select Case Target.Value
    > Case "CO Wage"
    > Call Module1.COWageProcedure
    > Case Else
    > MsgBox Target.Value & " Caused an error"
    > End Select
    > End If
    > End Sub
    >
    > Isn't this correct?
    >
    >
    > --
    > mgmcdevitt
    > ------------------------------------------------------------------------
    > mgmcdevitt's Profile: http://www.excelforum.com/member.php...o&userid=26592
    > View this thread: http://www.excelforum.com/showthread...hreadid=466940
    >
    >


  6. #6
    Registered User
    Join Date
    08-24-2005
    Posts
    11

    Thank You Gord and Rowan

    I used a combination of both of your codes and it works well. Thanks for your help.

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$G$9" Then
    Select Case Target.Value
    Case "CO Wage"
    COWage 'run COWage macro
    Case "AZ Wage"
    AZWage 'run AZWage macro
    Case Else
    MsgBox Target.Value & " Caused an error"
    End Select
    End If
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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