+ Reply to Thread
Results 1 to 4 of 4

Click a button - pass some information - How To?

Hybrid View

Askjerry Click a button - pass some... 01-23-2014, 05:21 PM
judgeh59 Re: Click a button - pass... 01-23-2014, 05:26 PM
Askjerry Re: Click a button - pass... 01-23-2014, 05:34 PM
6StringJazzer Re: Click a button - pass... 01-23-2014, 05:44 PM
  1. #1
    Registered User
    Join Date
    11-08-2012
    Location
    Kyle, TX, USA
    MS-Off Ver
    Excel 97, 2003, 2007, 2010
    Posts
    3

    Question Click a button - pass some information - How To?

    This should be simple... but I'm missing something.

    Imagine two buttons on a form.

    When you click either button you want the same MACRO to run, but do slightly different tasks. (Different range, different color, etc.)
    You install a variable called "mydata" and if it has a value of "1" it will behave one way, if "2" it will behave another.

    For now... let's just put this value into cell "B3"... just so I can see that the data is actually passed.

    Sub DO_SOMETHING()
        Dim myinfo As String
        Range("B3").Select
        ActiveCell.FormulaR1C1 = myinfo
        Range("A1").Select
    End Sub
    I thought that I could just change the routine to

    Sub DO_SOMETHING_ELSE(myinfo)
        Dim myinfo As String
        Range("B3").Select
        ActiveCell.FormulaR1C1 = myinfo
        Range("A1").Select
    End Sub
    But if I do... when I try to assign it to a button...

    BUTTON 1 would call DO_SOMETHING_ELSE("1")
    ...OR...
    BUTTON 2 would call DO_SOMETHING_ELSE("2")

    It say's it's too complex.

    I can call WORKBOOK!DO_SOMETHING just fine... but if I add the data field... FAIL.

    So...

    How do I call DO_SOMETHING and pass the variable... or call DO_SOMETHING_ELSE(data) ?

    Thanks,
    Jerry

  2. #2
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Click a button - pass some information - How To?

    something like this.....un-tested

    SUB Button1Click()
        DO_SOMETHING_ELSE 1
    SUB END
    
    SUB Buuton2Click()
        DO_SOMETHING_ELSE 2
    SUB END
    
    SUB DO_SOMETHING_ELSE(myinfo)
        
        If myinfo = 1 then
            play guitar ' do something
       else
           play fiddle ' do something else
       end
    SUB END
    if you have more button you might consider switching to a CASE/SELECT construct....
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  3. #3
    Registered User
    Join Date
    11-08-2012
    Location
    Kyle, TX, USA
    MS-Off Ver
    Excel 97, 2003, 2007, 2010
    Posts
    3

    Re: Click a button - pass some information - How To?

    Right at the time you were sending a reply... I tried this... it worked... not elegant... but it worked.

    Sub DO_SOMETHING(myinfo)
        Range("B3").Select
        ActiveCell.Value = myinfo
        Range("A1").Select
    End Sub
    
    Sub BTN1()
    DO_SOMETHING (1)
    End Sub
    
    Sub BTN2()
    DO_SOMETHING (2)
    End Sub
    So it looks like that's the way to do it.
    Thanks for the help!!

    Jerry
    Last edited by Askjerry; 01-23-2014 at 05:36 PM. Reason: Clean up display

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,006

    Re: Click a button - pass some information - How To?

    I think you will find this to be a better technique, not related to your original question. No need to Select and then operate on ActiveCell.

    Sub DO_SOMETHING(myinfo)
        Range("B3").Value = myinfo
        Range("A1").Select
    End Sub
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

+ 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. [SOLVED] click on a color button and after button click it will color entire row of cell you click
    By swade730 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-11-2014, 06:06 PM
  2. [SOLVED] Can you pass a button name to a macro?
    By brucemc777 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-28-2013, 03:05 PM
  3. how can ListIndex can be pass when botton click
    By jay11 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-23-2010, 06:25 AM
  4. Call button click event from a button on anther sheet
    By ajaykgarg in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-25-2010, 09:27 AM
  5. [SOLVED] [SOLVED] Create floating button based on button click in menu
    By ExcelMonkey in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-12-2005, 02:05 PM

Tags for this Thread

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