Results 1 to 4 of 4

Code to create Macro Button and Assigning Macro code

Threaded View

  1. #1
    Valued Forum Contributor
    Join Date
    01-18-2007
    Location
    Georgia
    MS-Off Ver
    2010
    Posts
    4,434

    Code to create Macro Button and Assigning Macro code

    Hello:

    Please refer to attached file.

    i have below code which successfully create a macro button and assign the macro correctly.
    This is only doing for one sheet and i need to modify the code so that it does for all sheets of the workbook.

    Sub CreateButton_New()
    
        Dim btn As Button
    
        'create the button in the active sheet.
        'The 4 arguments are left, top, width and height --- i chose to base them off of cellular dimensions
        Set btn = ActiveSheet.Buttons.Add( _
            ActiveSheet.Columns(2).Left, _
            ActiveSheet.Rows(2).Top, _
            ActiveSheet.Columns(2).Width * 2, _
            ActiveSheet.Rows(1).Height * 2)
    
        'this is where you assing a macro to the btn
        btn.OnAction = "ShowMessageBox"
    
        'add a caption
        btn.Text = "Click Me"
    
        Set btn = Nothing
    
    End Sub
    
    Sub ShowMessageBox()
    
        MsgBox "Hello"
    
    End Sub
    Let me know if you have any questions.
    Thanks.

    Riz
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macro to Create Button and assign code
    By swatsp0p in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-06-2014, 12:21 PM
  2. code needed to create macro for button
    By tryingtolearn in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-17-2012, 08:24 AM
  3. create control button and assigning it a macro
    By ers in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-03-2012, 01:24 PM
  4. Macro to create Command Button and assign code
    By dagindi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-07-2011, 09:19 AM
  5. Assigning dynamic named ranges to listfillrange via macro code
    By Schwizer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-30-2007, 11:44 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