+ Reply to Thread
Results 1 to 9 of 9

Calling macro from change in Data Validation List

  1. #1
    Registered User
    Join Date
    10-08-2010
    Location
    North Yorkshire, England
    MS-Off Ver
    Excel 2003
    Posts
    69

    Calling macro from change in Data Validation List

    Hello

    could someone please advise why the following macro does not work. It is supposed to run macro FindLocation_New when I change the selection in cell D4.

    PHP Code: 
    Private Sub Worksheet_Change(ByVal Target As Range)
        
    Dim FindLocation_New As String
        
        Application
    .EnableEvents True
         
        
    If Target.Address "$d$4" Then
        Application
    .Run FindLocation_New
        End 
    If
         
    End Sub 
    Thank you

  2. #2
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Calling macro from change in Data Validation List

    David,

    a couple of possibilities:
    - you are updating a range of cells whereby D4 is not the first cell (upper left) in the range, hence the address of Target would not equal $d$4
    - you are using selection_change are you sure you do not mean to trigger this routine when the value changes?

    and last

    the address returned by Target.Address is in uppercase so you should compare against $D$4
    If you like my contribution click the star icon!

  3. #3
    Registered User
    Join Date
    10-08-2010
    Location
    North Yorkshire, England
    MS-Off Ver
    Excel 2003
    Posts
    69

    Re: Calling macro from change in Data Validation List

    Thanks Ollie

    I changed the target to D4 which is a positive step as the rountine now goes further but now I get the message - Run-time error 1004, the macro " cannot be found.
    I think it's to do with your second point whereby I just want to trigger the rountine i.e. run the same macro irrespective of the selection in D4. The FindLocation_New macro simply uses the value in D4 to generate different results.
    Please advise how I can make the necessary change.

  4. #4
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Calling macro from change in Data Validation List

    David,
    In order for the macro to be found it needs to:
    - exist, and
    - be declared as public

    So "the first line of your macro needs to read 'Public Sub FindLocation_New' (obviously without the quotes)

    I am not entirely clear on when you want to execute the macro:
    - when the users makes a change to the value in D4?
    - when the user selected cell D4?
    - other?

  5. #5
    Registered User
    Join Date
    10-08-2010
    Location
    North Yorkshire, England
    MS-Off Ver
    Excel 2003
    Posts
    69

    Re: Calling macro from change in Data Validation List

    Ollie
    - The macro exists
    - I have now made it public but it is still not being found (I tried Worksheet_SelectionChange but still no luck)
    - the macro needs to execute when the user changes the value in D4
    thanks

  6. #6
    Registered User
    Join Date
    10-08-2010
    Location
    North Yorkshire, England
    MS-Off Ver
    Excel 2003
    Posts
    69

    Re: Calling macro from change in Data Validation List

    Ok after a bit of trial & error the following very simple code seems to work

    HTML Code: 
    Thanks for your help!

  7. #7
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Calling macro from change in Data Validation List

    You are welcome

  8. #8
    Registered User
    Join Date
    02-25-2014
    Location
    CT, USA
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: Calling macro from change in Data Validation List

    I am doing something very similar, however the code provided crashes my excel. Here is what I am doing:

    1: Designed a Vlookup macro to replace cell values with the lookup value.
    2: The cell value is selected via validation list dropdown.
    3: When I run my vlookup by itself it works flawlessly.
    4: When I run the vlookup with the code provided and print the results to a message box, it works flawlessly.
    5: When I run the vlookup with the code provided and print the results to the cell it crashes.
    6: When the code is finished it will update any cell that has had a drop down item selected. The dropdown is only in column K but there will be an undefined number of rows.

    My other code

    Please Login or Register  to view this content.
    Last edited by arlu1201; 02-28-2014 at 01:43 AM.

  9. #9
    Registered User
    Join Date
    02-25-2014
    Location
    CT, USA
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: Calling macro from change in Data Validation List

    Additionally:

    The error that I get is "Run-time error '-2147417848 (80010108)': Method 'Value' of object 'Range' failed

  10. #10
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Calling macro from change in Data Validation List

    jep5156,

    Welcome to the forum. 2 pointers.

    1. I have added code tags to your post. As per forum rule 3, you need to use them whenever you put any code in your post. Please add them in future. In order to put code tags, either type [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] at the end of it, OR you can highlight your code and click the # icon at the top of your post window.

    2. Unfortunately you need to post your question in a new thread, it's against the forum rules to post a question in the thread of another user. If you create your own thread, any advice will be tailored to your situation so you should include a description of what you've done and are trying to do. Also, if you feel that this thread is particularly relevant to what you are trying to do, you can surely include a link to it in your new thread.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ 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