+ Reply to Thread
Results 1 to 10 of 10

Passing a sheet name to a macro on a command button click.

  1. #1
    Registered User
    Join Date
    11-29-2018
    Location
    Sunny Cairns, QLD AUST
    MS-Off Ver
    2010
    Posts
    97

    Passing a sheet name to a macro on a command button click.

    I have 30 buttons on a sheet. At the moment when I click on each button it calls the related macro that loads data from its related sheet to charts on the sheet with the buttoms.. Each macro is identical except for the embedded sheet name. I would like to reduce the 30 macros down to 1.

    Is it possible to have a "Case" function that detects which button has been clicked and pass the sheet name to the single macro?

  2. #2
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Passing a sheet name to a macro on a command button click.

    Hello,

    each button will have one sub that fires when the button is clicked. The only command in that sub is to call the main routine, passing the sheet name as a text parameter.

    Here are some ways to do that.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    11-29-2018
    Location
    Sunny Cairns, QLD AUST
    MS-Off Ver
    2010
    Posts
    97

    Re: Passing a sheet name to a macro on a command button click.

    Thanks for the reply.

    I am not sure that will work. I am already calling macros for the sheets I need. What I really want to do is have a variable name in a single macro that will change depending on the button click. This would save having 30 macros.

    For example the small bit of code below is called from button 1 and the bold text DG # 1 is the sheet name for that macro. If I click on button 2 I want to change it to DG # 2 and so on. This reference appears 7 times in the macro.

    Hope that makes sense.

    Please Login or Register  to view this content.

  4. #4
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    419

    Re: Passing a sheet name to a macro on a command button click.

    You can create a global variable (it must be in a code module, not a workbook or worksheet module) e.g.
    Please Login or Register  to view this content.
    and then for each button code in whatever worksheet you need
    Please Login or Register  to view this content.
    Also, looking at your code snippet
    Please Login or Register  to view this content.
    you should be aware that in VBA, each variable must be declared individually. Your line of code declares c as a long integer, but the remaining ones will be variants.

  5. #5
    Registered User
    Join Date
    11-29-2018
    Location
    Sunny Cairns, QLD AUST
    MS-Off Ver
    2010
    Posts
    97

    Re: Passing a sheet name to a macro on a command button click.

    So how does this achieve what I am trying to do?

    Also I was told that the Dim as I have it declares them all to be long as shown below.

    Please Login or Register  to view this content.

  6. #6
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Passing a sheet name to a macro on a command button click.

    So you have 30 buttons, each calling the same macro?

  7. #7
    Registered User
    Join Date
    11-29-2018
    Location
    Sunny Cairns, QLD AUST
    MS-Off Ver
    2010
    Posts
    97

    Re: Passing a sheet name to a macro on a command button click.

    No. I have 30 buttons calling 30 macros. The only thing different in each macro is the sheet name for the data as I stated in post # 3.

    I want something that can detect the button that was clicked and apply the relevent sheet name to a variable that would replace the DG # 1.

  8. #8
    Registered User
    Join Date
    11-29-2018
    Location
    Sunny Cairns, QLD AUST
    MS-Off Ver
    2010
    Posts
    97

    Re: Passing a sheet name to a macro on a command button click.

    I thought it might be an idea to give you the whole of the main macro. There is also a module full of the button clicks.
    Attached Files Attached Files

  9. #9
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    419

    Re: Passing a sheet name to a macro on a command button click.

    It is possible to do this using shapes instead of buttons. I don't have time to show you how to do that today, but I should be able to do it tomorrow.

    As for my other point , try this:
    Please Login or Register  to view this content.
    It will not run, as you cannot assign a string or floating point number to a long variable. Then try this:
    Please Login or Register  to view this content.
    It will run just fine, as a and b are Variants.

  10. #10
    Registered User
    Join Date
    11-29-2018
    Location
    Sunny Cairns, QLD AUST
    MS-Off Ver
    2010
    Posts
    97

    Re: Passing a sheet name to a macro on a command button click.

    Its done. Not how I expected it to be but even simpler. Sometimes I amaze myself having virtually no programming experience I stumble on to the simplest answers.

    Thank you to all that helped.

+ 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. Command Button Execute Macro Event Upon Click
    By huntethic in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-24-2019, 02:46 PM
  2. Replies: 2
    Last Post: 01-09-2018, 11:39 AM
  3. Command button to execute macro on click...
    By ruub7 in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-23-2017, 07:42 AM
  4. [SOLVED] Trying to add a macro to activate another macro to "click" a command button to clear cells
    By nthpl888 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-13-2016, 02:52 PM
  5. Replies: 0
    Last Post: 12-16-2015, 07:00 AM
  6. Passing String Variable from Command Button Macro to a List Box in same User Form
    By wz4np1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-01-2012, 11:31 AM
  7. Passing value between Command Button and Form
    By nivassrii in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-30-2011, 12:21 AM

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