Automatically create a button and assign VBA code.. through VBA?

Chronix99

New Member
Joined
Oct 2, 2011
Messages
19
Hi Guys,

On my workbook, the user is presented with several buttons to click and upon clicking one of them - it creates a new worksheet called "SummaryReport" - this worksheet will not exist in the workbook unless the user clicks that specific button.

However, once this button is clicked and the new worksheet is created - I want to insert another button on the "SummaryReport" worksheet say called "Regenerate Report" which will delete the newly created worksheet and then show another form.

The showing the initial form is easy but I want to know how I could code in VBA to create a new button on a worksheet, then assign this button some VBA code to delete that particular worksheet.

Thanks for any help!
 
@GTO
So do you mean create a sheet as a 'template' and add all this modifications to it then hide it. Then every time the button is clicked just create a new sheet that copies the template sheet?

Hi there,

It sounds as if your problem is solved, but to answer the question, yes. I was thinking that you were going to use an ActiveX command button, in which case maybe something like:

  • Add a 'template' sheet
    • Do whatever formatting is to be done,
    • add a button,
    • in the sheet's module:
      • Change the sheet's codename to shtTemplate
      • Change the sheet's Visible property to xlVeryHidden
      • Add code:
Rich (BB code):
Option Explicit
    
Private Sub CommandButton1_Click()
    Call SheetDel(Me.Index)
End Sub

In a Standard Module:

Rich (BB code):
Option Explicit
    
Sub SheetDel(Index As Long)
    Application.DisplayAlerts = False
    ThisWorkbook.Worksheets(Index).Delete
    Application.DisplayAlerts = True
    ThisWorkbook.Worksheets(1).Activate
End Sub

Then the code for the button to create the new sheet would be something like:

Rich (BB code):
Private Sub CommandButton1_Click()
    shtTemplate.Visible = xlSheetHidden
    shtTemplate.Copy Before:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
    ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count - 1).Visible = xlSheetVisible
    shtTemplate.Visible = xlSheetVeryHidden
End Sub
 
Upvote 0

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.

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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