+ Reply to Thread
Results 1 to 5 of 5

How to trigger code from a dropdown box ?

  1. #1
    Forum Contributor
    Join Date
    04-15-2008
    Location
    Texas, USA
    MS-Off Ver
    M365 Excel Version 2210
    Posts
    216

    How to trigger code from a dropdown box ?

    I have a cell in a worksheet (in Excel 2013) that contains a dropdown box. I want to run 3 different strings of code depending on which of the 3 dropdown choices the user picks. The code below is a portion of what I'd run if the user picks Option 2. How do I change this to get it to run after they choose Option 2 then enter?

    Thanks for your help.
    RR
    -------------------------------
    Sub Macro2()
    '
    ' Macro2 Macro
    'Set Column Widths for Option 2
    Application.ScreenUpdating = False

    Range("C1").Select
    Selection.ColumnWidth = 13
    Range("D1").Select
    Selection.ColumnWidth = 13

    End Sub

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: How to trigger code from a dropdown box ?

    You can do this using a Worksheet Change event. Copy and paste this macro into the worksheet code module. Do the following: right click the tab for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet, make your selection in your drop down and exit the cell. This macro assumes that your drop down is in A1. You will have to change that to suit your needs. Also, this will only take into account one option which is "Option 2". If you want to include the other two options, then they would have to be added to the code.
    Please Login or Register  to view this content.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: How to trigger code from a dropdown box ?

    Change the "$A$1" to whatever cell the dropdown is in (keep the dollar signs), and change "Option 2" to whatever the text actually say.

    You can see, too, that I changed your code a bit. You don't need to be selecting cells. Just modify them directly.

    Please Login or Register  to view this content.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: How to trigger code from a dropdown box ?

    Per Forum Rules, please use code tags when posting.

    http://www.excelforum.com/forum-rule...rum-rules.html

    Maybe one of these may help?

    Please Login or Register  to view this content.
    Or

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    04-15-2008
    Location
    Texas, USA
    MS-Off Ver
    M365 Excel Version 2210
    Posts
    216

    Re: How to trigger code from a dropdown box ?

    I'm still having problems with this code since I'm new to VBA. The code below gives me a Run-time error '1004': that says "Unable to set the ColumnWidth property of the Range class". The error occurs on the line "Selection.ColumnWIdth = 13". How do I correct this problem?

    Thanks for your help.
    RR

    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. Adjust a code from cell range trigger to button trigger
    By ld2x07 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-01-2014, 10:02 AM
  2. [SOLVED] Trigger Code by Dropdown
    By rachaelgoldman1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-07-2013, 10:31 AM
  3. Replies: 1
    Last Post: 05-27-2011, 12:27 PM
  4. How to trigger code execution?
    By alwaysgo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-11-2008, 09:11 AM
  5. How do I trigger a macro by selecting from a dropdown list?
    By Modell in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-26-2006, 12:10 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