+ Reply to Thread
Results 1 to 4 of 4

Assign a macro to a button via VBA code

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

    Assign a macro to a button via VBA code

    Edit: Sorry, wrong thread, can somebody move to programming please?

    Hello,

    Looking for a little help here. Basically what I need to do is assign a macro to a button I create in VBA code. Ultimately what is happening in my program is:

    I need a simple way for our program assistant to generate certain data in a fixed way so she can do a mail merge later on. The data she needs is stored in a SQL database. What I want to do is pull a list of SessionID's for her in column A, in column B create a Button that she can click and when clicked with create the excel document that she can use in her mail merge.

    I have the SQL code done(for both sections, just showing part 1 now), now I just need to get my button to work. So far this is what I have.

    Please Login or Register  to view this content.
    The above code populates column A with the SessionID and column B with a button with the title 'Generate' however when I click the button I get this following error:

    "Cannot run the macro 'Book2.xlsm!btnS'. The macro may not be available in this workbook or all macros may be disabled."

    even though the btnS call at the end of my code works. Perhaps I am looking at this all wrong but any help would be greatly appreciated. I've spent a few hours scouring Google but nothing has helped so far.
    Last edited by AverageCanadian; 03-09-2012 at 11:10 AM.

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Assign a macro to a button via VBA code

    ... Deleted Previous Post...

    A stripped down version of your code works.

    Please Login or Register  to view this content.
    So will have to study the problem closer because it isn't with creating the button or calling it.
    Last edited by abousetta; 03-09-2012 at 10:50 AM.
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    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.

    Please Login or Register  to view this content.
    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.

  4. #4
    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