+ Reply to Thread
Results 1 to 5 of 5

Passing multiple variables to button .onaction

Hybrid View

  1. #1
    Registered User
    Join Date
    10-24-2012
    Location
    Seattle
    MS-Off Ver
    Excel 2011 Mac
    Posts
    3

    Passing multiple variables to button .onaction

    I am having a problem trying to pass multiple variables to a button's .OnAction call. I want to be able to pass multiple variables to the .OnAction property of a button.

    This code works when I want to send one variable:
    Dim strText as String
    Dim intNumber as Integer
    Dim lngLong as Long
    
    shpBar.OnAction = "'Button_Click """ & strText & """'"
    I want to be able to pass all 3 variables into the Button_Click call for each button that I have. I have been trying many different syntaxes and have not been able to get it to work properly. How would I be able to pass "strText, intNumber, lngLong" variables to the .OnAction call?

    Thanks for the help

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Passing multiple variables to button .onaction

    To avoid confusing quotes try this approach.

    Sub AssignOnAction()
        
        Dim shpTemp As Shape
        Dim strArg1 As String
        Dim lngArg2 As Long
        Dim strArg3 As String
        
        strArg1 = Chr(34) & "My Name" & Chr(34)
        lngArg2 = 999
        strArg3 = Chr(34) & "Display This" & Chr(34)
        
        Set shpTemp = ActiveSheet.Shapes(1) ' adjust for relevant button shape
    
        shpTemp.OnAction = "'Button1_Click " & strArg1 & "," & lngArg2 & "," & strArg3 & "'"
    
    End Sub
    
    Sub Button1_Click(Arg1, Arg2, Arg3)
    
        MsgBox Arg1 & " " & Arg2, vbInformation, Arg3
        
    End Sub
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    10-24-2012
    Location
    Seattle
    MS-Off Ver
    Excel 2011 Mac
    Posts
    3

    Re: Passing multiple variables to button .onaction

    When I try to run that, I get a "Run-time error '424'; Object required" error message. Is there something else that I might be missing?

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Passing multiple variables to button .onaction

    Here is a working example
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    10-24-2012
    Location
    Seattle
    MS-Off Ver
    Excel 2011 Mac
    Posts
    3

    Re: Passing multiple variables to button .onaction

    Thanks for the help! I was able to see what I was doing wrong, and you have saved me lots of time.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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