Button (Forms Control) Problem in VBA

Rocky0201

Active Member
Joined
Aug 20, 2009
Messages
278
Hi,

From my UserForm1 (Active Sheet is Project Definitions), when I make a selection, I would like to add a Form Control Button on my Sheet called "Start".

This part works when the selection is made in my UserForm1:

Sheets("Start").Buttons.Add(48, 196.5, 192, 19.5).Select

The Button is actually add to my Sheet called Start.

The problem I'm having is referencing the newly added Button (could be Button 1 thru Button x where x is some other number) so that I can assign a macro to the Button (Selection.OnAction = "ReturnToStart_Click"), and name the button i.e., Test.

When I ran the Record Macro this is the code produced:
Code:
Sub Macro4()
'
' Macro4 Macro
'

'
    ActiveSheet.Buttons.Add(48, 196.5, 192, 19.5).Select
    Selection.OnAction = "ReturnToStart_Click"
    ActiveSheet.Shapes("Button 7").Select
    Selection.Characters.Text = "Test"
    With Selection.Characters(Start:=1, Length:=4).Font
        .Name = "Calibri"
        .FontStyle = "Regular"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = 2
        .TintAndShade = 0
        .ThemeFont = xlThemeFontNone
    End With
    Range("B14").Select
End Sub

Two issues in the above. 1) the code requires that the Sheet Start be active (I am on the Project Definitions sheet) and 2) how do I reference the newly added Button? Meaning How can I tell the Button added is Button 1, Button 2, etc.

In advance, thanks for the help.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Maybe ...

Code:
Sub Macro4()
    With Worksheets("Start").Buttons.Add(48, 196.5, 192, 19.5)
        MsgBox .Name
        .OnAction = "ReturnToStart_Click"
        With .Characters
            .Text = "Test"
            With .Font
                .Name = "Calibri"
                .FontStyle = "Regular"
                .Size = 11
            End With
        End With
    End With
End Sub
 
Upvote 0
shg, is there a way to loop on my sheet called Start and, if there are Buttons previously created on Start, delete these buttons? I cannot seem to find a way to loop. I've tried For Each but I cannot find the appropriate syntax.

Thanks again...
 
Upvote 0
Code:
    Worksheets("Start").Buttons.Delete
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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