how to add a code to programatically created command button

saurabh_546

New Member
Joined
Sep 7, 2009
Messages
19
Dear all
i am adding a command button programmically to the worksheet.
i need to assign a simple code which will activate the main sheet in workbook..
can any body help me.
thanks and regards
Saurabh
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hello,

The most practical way to do this is to avoid ActiveX controls and use Shapes, which are created traditionally from the Forms toolbar.

This way, you don't have to add code to the Project, which requires Trust, and isn't set up by default as such on other people's computers. So, here's an example:

Code:
Sub foo()
Dim cl As Range
Dim shpBt As Button
Application.ScreenUpdating = False
For Each cl In ActiveSheet.Range("B1:B200")
    Set shpBt = ActiveSheet.Buttons.Add( _
        Left:=cl.Left, _
        Top:=cl.Top, _
        Width:=cl.Width, _
        Height:=cl.Height)
    With shpBt
        .Characters.Text = shpBt.Name
        .OnAction = ThisWorkbook.Name & "!bar"
    End With
Next
Application.ScreenUpdating = True
End Sub

Sub bar()
With ActiveSheet.Buttons(Application.Caller)
    MsgBox "Address: " & _
        .TopLeftCell.Address(0, 0)
End With
End Sub
foo() creates the buttons, each button calls bar().
 
Upvote 0
thanx for help nateo
i am able to create button now..
but my problem is i am creating sheets also programically.. so my question is where shoud i write micro for that button...
regards
Saurabh
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,831
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top