VBA to Add a Control Button that allows for the sheet to be deleted

SeagullWardy

New Member
Joined
Jul 20, 2018
Messages
2
I am trying to get some code to work. What I am trying to do is write some code that when a user clicks on a cell it


  • Creates a new worksheet based on the value of the selected cell
  • Queries a different worksheet in the same workbook and posts the required information in to the new worksheet
  • It then adds a button and assigns it a macro
  • This macro deletes the current worksheet

The idea is that the user will be looking at summary sheet and can click on a cell to get some more detailed information.

At the moment I have the code working that creates the new sheet, does the query, posts the information, creates the button and assigns the macro to it. The problem is when I click the button I get an error message about the macro not being available in the worksheet. However, if I run the macro manually it does work so I am a little confused.

My code for creating the button and the deleting the sheet are as follows:

Public Sub AddButton()
Application.ScreenUpdating = False
ActiveSheet.Buttons.Delete
ActiveSheet.Buttons.Add(Range("K1").Left, Range("K1").Top, 90, 25).Select
Selection.Name = "btnDeleteSheet"
Selection.OnAction = "DeleteCurrentSheet"
ActiveSheet.Shapes("btnDeleteSheet").Select
With Selection
.Characters.Text = "Close Sheet"
With .Font
.Name = "Arial"
.FontStyle = "Bold"
.Size = 12
End With
End With
ActiveSheet.Cells(1, 1).Select
End Sub


Public Sub DeleteCurrentSheet()
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True

End Sub

Anyone able to identify where I am going wrong?

Thanks
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Button code must be in the sheet code module of the host sheet and uses 'Private' as the Sub type.
You can put the delete code in a public code module and call it from the button code.
Code:
Private Sub CommandButton1_Click()
delSheet
End Sub
The above goes in the sheet module
Code:
Sub delSheet()
Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True
End Sub
The above code goes in module1.
 
Last edited:
Upvote 0
Button code must be in the sheet code module of the host sheet and uses 'Private' as the Sub type.
You can put the delete code in a public code module and call it from the button code.
Code:
Private Sub CommandButton1_Click()
delSheet
End Sub
The above goes in the sheet module
Code:
Sub delSheet()
Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True
End Sub
The above code goes in module1.

Thanks for the answer JLGWhiz.

If I am understanding you correctly what you are saying is that the first code snippet goes against the sheet with the button on and the second code snippet goes against the workbook or somewhere else that it is public.

I am very new to VBA though and not quite sure how to achieve via code what you have said. The sheet that I am putting the button on is created by code and the button is added by code. So I guess I need a way to add the required code against that sheet.

Sorry if it is a silly question.
 
Upvote 0
Yes, the Active-X button code must be entered in the sheet code module. To access the code module, right click the sheet name tab, then click 'View Code' in the pop up menu. That displays the vbeditor with a code pane for that sheet. I don't know what type of button you are putting on the sheet, but I assume it will be active=X. .If your button is Forms Contol, then you do not need the sheet code to use the delSheet macro. With a Form Control button, you can assign the delSheet macro directly to the button by right clicking o the button, then click 'Attach Macro'. or 'Assign Macro', I forget the wording. The delSheet code goes in Module1. To access Module1, press Alt + F11 while the Excel sheet is viible, or double click on the name Module1 in the Project box at upper left in the vb editor.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,881
Messages
6,122,074
Members
449,064
Latest member
MattDRT

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