+ Reply to Thread
Results 1 to 6 of 6

Assign a macro and pass an argument

  1. #1
    Registered User
    Join Date
    05-26-2006
    Location
    Ohio
    Posts
    17

    Assign a macro and pass an argument

    How do I assign a macro to a button and pass an argument to the macro?
    Sorry if this is too simple but I have had no luck searching forums for the answer.
    ReBoot
    Last edited by VBA Noob; 03-01-2008 at 04:14 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    You can have the macro pop up an InputBox or Application.InputBox to request the argument.

  3. #3
    Registered User
    Join Date
    05-26-2006
    Location
    Ohio
    Posts
    17
    I was hoping I could just pass the argument without the user having to enter it. Each button they press will have it's own value for the argument. The values will not change once it is set up.

    I saw an answer that showed entering this code when assigning the macro.
    Here it is,
    To assign your macro Macro1, passing the number 16 as the parameter, to a button on your worksheet , in the Tools->Assign Macro dialog, type
    'Macro1 16'
    but when I try that, it thinks the macro name is Macro1 16.

    ReBoot

  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    A macro attached to a button can pass a variable to another called macro

    Please Login or Register  to view this content.
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  5. #5
    Registered User
    Join Date
    05-26-2006
    Location
    Ohio
    Posts
    17

    Thank you mudraker

    I was hoping I wouldn't have to code so many macros, and would be able to pass the argument when the button is pressed. I have 18 buttons.

  6. #6
    Registered User
    Join Date
    05-26-2006
    Location
    Ohio
    Posts
    17

    Thumbs up I found the solution to my problem.

    To pass an argument when pressing a button in an Excel sheet.
    When you right click an Excel button to assign a macro, enter the macro name and argument as such. 'Macro1(1)'
    Note the single quotes around the whole thing.
    This example will call the macro Macro1, and pass it the number 1.
    I hope this will help someone else.
    ReBoot

+ 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