+ Reply to Thread
Results 1 to 4 of 4

Assign a macro to a button via VBA code

Hybrid View

  1. #1
    Registered User
    Join Date
    01-19-2012
    Location
    Niagara Falls, Canada
    MS-Off Ver
    Office 2010
    Posts
    8

    Re: Assign a macro to a button via VBA code

    Quote Originally Posted by abousetta View Post
    ... Deleted Previous Post...

    A stripped down version of your code works.

    Sub getData()
    Dim btn As Object
            Set btn = ActiveSheet.Buttons.Add(Range("A1").Left, Range("A1").Top, Range("A1").Width, Range("A1").Height)
            With btn
                .Caption = "Generate"
                .OnAction = "btnS"
            End With
    End Sub
    
    Sub btnS()
         MsgBox ("Test")
    End Sub
    So will have to study the problem closer because it isn't with creating the button or calling it.
    Thanks.

    Also interesting, the above code works on your computer? I just ran it on a new workbook on my computer and I get the same error, so it might not be code related but computer related?

    However if I let the VBA code generate the button, and then right click on the button and assign the macro ThisWorkbook.btnS it works.

  2. #2
    Registered User
    Join Date
    01-19-2012
    Location
    Niagara Falls, Canada
    MS-Off Ver
    Office 2010
    Posts
    8

    Re: Assign a macro to a button via VBA code

    Thanks for the help and pointing me in the correct direction. I figured it out.

    I changed
    .OnAction = "btnS"
    to
    .OnAction = "ThisWorkbook.btnS"

    and all is good now. Now lets see what can stump me next

+ 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