davesexcel.com

Excel information

Most Popular Excel Questions

Add Buttons to a Range of Cells

Posted by davesexcel on April 12, 2012 at 4:15 AM

I want to add buttons, name them and assign macros to them.


Use this code, it will add buttons to A1:A3, name each button and assign a macro to them.

Make sure you have the macros in place before you run the CreateButtons code.


 

Sub CreateButtons()

    Dim butn As Button, Rng As Range, c As Range, i

    i = 1

    With Worksheets("Sheet1")

        Set Rng = .Range("A1:A3")

        For Each c In Rng.Cells

            Set butn = .Buttons.Add(c.Left, c.Top, c.Width, c.Height)

            With butn

                .Caption = "Macro" & i

                .OnAction = "Macro" & i

            End With

            i = i + 1

        Next c

    End With

End Sub

Sub Macro1()

    MsgBox "Macro1"

End Sub

Sub Macro2()

    MsgBox "Macro2"

End Sub

Sub Macro3()

    MsgBox "Macro3"

End Sub

 

 


Categories: None

Post a Comment

Oops!

Oops, you forgot something.

Oops!

The words you entered did not match the given text. Please try again.

You must be a member to comment on this page. Sign In or Register

0 Comments