Use VBA to add new button AND its code

sspicer

New Member
Joined
Jun 27, 2008
Messages
48
I have a spreadsheet which walks the user through how to add data to it in the correct manner.

As such, I want to be able to write a macro that will add a new page AND on that page there to be a button with code behind it (only simple code to take the user back to the main menu).

So far I have:
Code:
Sub NewPage()
 
Sheets.Add
ActiveSheet.Name = ComboBox1.Value
 
    ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
        , DisplayAsIcon:=False, Left:=300, Top:=25, Width:=150, Height:=40) _
        .Select


This all works, but I'll need something to say that on the "click" method of the new button then sheets("HomePage").activate. That's all I'll need. Any ideas? Or is there an easier way to do this?

Thanks in advance!

Sam
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Place this in your worksheet module :

Code:
Private WithEvents cmbEvents  As CommandButton
Private oNewSheet As Worksheet
 
Private Sub cmbEvents_Click()
 
    Sheets("HomePage").Activate
 
End Sub
 
Private Sub CommandButton1_Click()
 
    On Error GoTo err_Handler
    Set oNewSheet = Sheets.Add
    
    With oNewSheet
        .Name = (ComboBox1.Value)
       Set cmbEvents = _
       .OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
       , DisplayAsIcon:=False, Left:=300, _
       Top:=25, Width:=150, Height:=40).Object
    End With
    Set oNewSheet = Nothing
    Exit Sub
err_Handler:
    
    MsgBox Err.Description
 
End Sub

Regards.
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,537
Members
449,316
Latest member
sravya

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