Deleting CommandButton after running macro

Anonymous
2016-07-18T14:57:37+00:00

I'm trying to delete the CommandButton that runs my macro. The rest of the code simply locks every sheet after the first sheet. Here's my code:

Private Sub CommandButton1_Click()

For i = 2 To Sheets.Count

    Sheets(i).Activate                                   'Locks every other sheet

    ActiveSheet.Protect Password:="Microsoft"

Next i

Sheets(1).Shapes("CommandButton1").Delete                'Deletes button

Sheets(1).Protect Password:="Microsoft"                  'Locks first sheet

End Sub

Everything runs fine with no error, but the CommandButton is still there after the macro has ran.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
Anonymous
2016-07-18T16:22:54+00:00

Try deleting the button with the sheet active:

Private Sub CommandButton1_Click()

For i = 2 To Sheets.Count

    Sheets(i).Activate                                   'Locks every other sheet

    ActiveSheet.Protect Password:="Microsoft"

Next i

Sheets(1).Activate

Sheets(1).Protect Password:="Microsoft", UserInterfaceOnly:=True 'Locks first sheet

Sheets(1).Shapes("CommandButton1").Delete                'Deletes button

End Sub

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more