Deselect Single Item (Button) from Multiple Selection

KingB69

New Member
Joined
Feb 19, 2025
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I have a worksheet which a user can populate with text, pictures and shapes. To prevent the user from causing issues with the worksheet, it is protected, and there is a Command Button which the user can click to insert another worksheet which is an identical copy of the original.

I have been able to delete all text and format relevant cells etc., but when I try to delete the other items, pictures and shapes using an ActiveSheet.Shapes.SelectAll command, I need to "deselect" the Command Button "Button 1" from the selection.

I have tried to change button visibility to false and then deleting the shapes, which generally works. However, if I try the delete command on a sheet without any shapes, the button does not reappear. It still exists within the worksheet as I can go into the VBA Editor and change its visibilty in the Immediate window, but I do not know why the code only works once, as in the button never successfully re-appears more than once?
 
Welcome to the MrExcel board!

Would something like this be any use? (Test with a copy of your workbook)

VBA Code:
Sub Test()
  Dim sh As Shape
  Dim ShapesToSelect As String
  
  For Each sh In ActiveSheet.Shapes
    If sh.Name <> "Button1" Then ShapesToSelect = ShapesToSelect & "," & sh.Name
  Next sh
  If Len(ShapesToSelect) > 0 Then ActiveSheet.Shapes.Range(Split(Mid(ShapesToSelect, 2), ",")).Delete
End Sub
 
Upvote 1
Solution
For Each sh In ActiveSheet.Shapes If sh.Name <> "Button1" Then ShapesToSelect = ShapesToSelect & "," & sh.Name Next sh If Len(ShapesToSelect) > 0 Then ActiveSheet.Shapes.Range(Split(Mid(ShapesToSelect, 2), ",")).Delete
Dear Peter_SSs, after a minor change (it was Button 1) it works perfectly!

Many thanks!
 
Upvote 0

Forum statistics

Threads
1,226,812
Messages
6,193,116
Members
453,777
Latest member
Miceal Powell

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