How do I delete all buttons on a worksheet? - excel

I've got a bit of code that creates a Save button on a worksheet (held in the wsReport variable), but it doesn't remove previous buttons. Over time, they tend to build up. Is there any way to do something like this?
wsReport.Buttons.All.Delete
(Not right, obviously, but gives an idea of what I'm looking for.)

See code below :)
Sub RemoveButtons()
Dim i As Integer
If ActiveSheet.ProtectContents = True Then
MsgBox "The Current Workbook or the Worksheets which it contains are protected." & vbLf & " Please resolve these issues and try again."
End If
On Error Resume Next
ActiveSheet.Buttons.Delete
End Sub
Source:
http://www.mrexcel.com/forum/excel-questions/609668-delete-all-buttons-sheet-visual-basic-applications.html
or could you use code below:
(Buttons in VBA are in the Shapes collection).
Sub DelButtons()
Dim btn As Shape
For Each btn In ActiveSheet.Shapes
If btn.AutoShapeType = msoShapeStyleMixed Then btn.Delete
Next
End Sub
source:
Deleting a collections of VBA buttons

See the code below:
Sub DeleteAllShapes()
ActiveSheet.Shapes.SelectAll
Selection.Delete
End Sub

If somebody found this question, but needs to delete only one button, this helped me:
ActiveSheet.Shapes("my_button_name").Delete

There's an easier method that doesn't use VB:
Click the Developer tab
Click Design Mode
Click on any button in the worksheet
Press Ctrl + A to select all buttons in the worksheet
Press Delete

Related

How to start macro with a left click NOT on a cell in excel?

I am trying since 2 days to find how to do the following without finding anything that suits the aim:
Steps by order :
user open excel file
he chose between folowing :
Paste an image directly in the worksheet (may be an limited area)
activate some video in the workbook (may be a webcam for start)
he select with a button to activate his clicks detection
he clicks anywhere on the picture and i get the coordinates of clicked points
So far i've seen ppl using (and tested myself) :
mouse event ==> this does not work as i need to know the name of what he is clicking on and it may be a brand new picture he just pasted
BeforeDoubleClick (same, i'd prefer avoid doubleclick but even then it doesnt work when i click on something else but cells)
Selectionchange ==> doesnt work if im not clicking on cells
Place hidden button over the area i want : i cant click a button if its not visible, and it becomes visible when i click it if i put as transparent
If anyone has ideas about this...
(nb: im not a pro of vba so i may have missed something)
Just forgot : my issue is not getting the coordinates of mouse, its just triggering the macro when i want, for now im jsut trying to get a simple msgbox to see if trigger works.
Thanks if anyone has any ideas
BR
Not sure if this fits your need (for example couldn't test it with a video).
a) Place a "button" of any kind on your sheet. I usually use a square shape for that and format it a little bit (color, shade, text). Assign the subroutine SetEvents to it.
b) Declare a global variable that remembers that click-activation is active
Option Explicit
Global EventCatchingActive As Boolean
c) In the event routine of your button, set the OnAction-method for all shapes of the sheet - see the routine setEvents. This ensures that even newly added images handle the click event.
Sub setEvents()
' This routine is called from the magic button.
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(1) ' Set this to whatever sheet you need
Dim button As Shape
Set button = ws.Shapes(Application.Caller)
If EventCatchingActive Then
EventCatchingActive = False
button.TextFrame2.TextRange.Characters.Text = "Start Clicking"
Else
Debug.Print "Setting EventHandler"
Dim sh As Shape
For Each sh In ThisWorkbook.Sheets(1).Shapes
' Debug.Print sh.Name, sh.Type
If sh.Name <> button.Name Then sh.OnAction = "ClickedMe"
Next
EventCatchingActive = True
button.TextFrame2.TextRange.Characters.Text = "Stop Clicking"
End If
End Sub
d) Declare a routine that is called if any of the shapes is clicked. With Application.Caller you can check what was clicked.
Sub ClickedMe(Optional target As Range = Nothing)
If Not EventCatchingActive Then Exit Sub
If target Is Nothing Then
Debug.Print "I clicked on " & Application.Caller
Else
Debug.Print "I clicked on cell " & target.Address
End If
End Sub
(note that code of steps b) to d) goes into a regular module)
e) If you also want to handle clicks on a cell, add the following into the sheet-module of the worksheet you are dealing with.
Private Sub Worksheet_SelectionChange(ByVal target As Range)
ClickedMe target
End Sub

VBA - Using go back button shows data from first sheet

I have an Excel that functions as a sort of decision tool, where questions are being asked and one needs to navigate through the workbook with buttons.
I have made a macro to function as a "go back" button, which activates the previously active sheet and hides the one you are on now. It works, but it keeps showing the navigation buttons from the first sheet. The text from the correct sheet does appear. If I go to another sheet and back, the data appears correct.
Is there a way to refresh the sheet so the correct information shows up, or is this a problem to do with the buttons and the macros behind them?
The macro I have used for the go back button:
(in workbook):
Sub Workbook_SheetDeactivate(ByVal sh As Object)
LastSheet = sh.Name
End Sub
(in module):
Global LastSheet As String
Sub goback()
Sheets(LastSheet).Visible = True
Sheets(LastSheet).Activate
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
With ws
If .Name <> "BM" Then
.Range("H9:R31").Font.Color = RGB(255, 255, 255)
End If
End With
Next ws
Sheets(LastSheet).Visible = False
End Sub
Even though there are no calculations, I did try including ActiveSheet.EnableCalculation but that did nothing.
Any help would be greatly appreciated.
Maybe I am reading this incorrectly but is the issue that the buttons are not dissapearing? if so this may help:
Sub hidebutton()
Dim sh As Shape
For Each sh In Sheet1.Shapes
If sh.Name = "Button 1" Then
sh.Visible = True
End If
Next
End Sub

Excel CustomUI Button Links

Ok, I know this is an ongoing problem, and I have searched seemingly everywhere for an answer. I apologize if I somehow missed an answer on here, so if you know of one, please advise!
I created a Custom Ribbon in Excel using the 'CustomUI Editor". My macros currently only run from buttons directly on the sheet, not on the UI. I read that it is because of the links to the original file. So I created a fresh new workbook, created new macros (not imported), created custom UI buttons, and linked the macros to the new buttons. The buttons still give me the error that the macro cannot be run and may not be available. What could I be missing here? I have also read that its possible to create a macro to update the links when the workbook is opened. Has anyone had success with this? I would love to have buttons on the ribbon and remove them from my sheet! Thanks for any help that you can provide!
You can use the following sub to add custom menu items. Just replace the .OnAction value with the names of the macros you'd like the control to run.
Sub AddCustomMenu()
Dim cbMainMenuBar As CommandBar
Dim iHelpMenu As Integer
Dim cbcCutomMenu As CommandBarControl
'Delete the menu item if it already exists. Use On Error Resume Next in case it doesn't exist.
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("&My Tools").Delete
On Error GoTo 0
Set cbMainMenuBar = Application.CommandBars("Worksheet Menu Bar") 'Set a CommandBar variable to Worksheet menu bar
iHelpMenu = cbMainMenuBar.Controls("Help").Index 'Return the Index number of the Help menu. We can then use this to place a custom menu before.
Set cbcCutomMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup, Before:=iHelpMenu) 'Add a Control to the "Worksheet Menu Bar" before Help.
cbcCutomMenu.Caption = "&My Tools" 'Give the control a caption
'Working with our new Control, add a sub control and give it a Caption and tell it which macro to run (OnAction).
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Run Macro 1"
.OnAction = "Macro1"
End With
With cbcCutomMenu.Controls.Add(Type:=msoControlButton)
.Caption = "Run Macro 2"
.OnAction = "Macro2"
End With
End Sub
If you'd like it to add your custom menu whenever you open the workbook, go to the ThisWorkbook module and add the following sub:
Private Sub Workbook_Open()
AddCustomMenu
End Sub

VBA does not active

I suggest that my workbook contains a VBA code below. But It does not run when I opened my workbook too.
Sub Appl_StatusBar()
Application.StatusBar = "SupportABC"
End Sub
If you put your code in the Workbook Open Event it will do what you need. To do this click the top dropdown where it says "(General)" and hit "Workbook". In the right dropdown select "Open" and save your code there". See below
Private Sub Workbook_Open()
Application.StatusBar = "SupportABC"
End Sub

Excel VBA : WorkSheet_FollowHyperlink is not getting called for Shape Object ( msoPicture )

I need to have a macro executed when someone clicks on the shape in the worksheet which is a picture. The macro intention is to display more information regarding the shape on which the user clicked.
EDIT: I also needed an information display during mouse-rollover, hence used hyperlink method.
I followed the method outlined in the MSDN , but the macro just doesn't seem to run.
My Macro is as follows:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
MsgBox "Source: Target.Range.Address " & Target.Range.Address
MsgBox "Source: Target.Range.Value " & Target.Range(1, 1).Value
' Some more macro stuff here
End Sub
On clicking the picture I get to the sheet where the hyperlink target
is, but the macro doesn't get executed.
I created a dummy hyperlink on one of the cells (in the same sheet), if I click that one the macro is executed.
I used the hyperlink method to have the rollover information on the picture shape.
I am totally out of ideas about how to go about this. Help would be greatly appreciated.
Don't use that method. Use Worksheet_SelectionChange instead of Worksheet_FollowHyperlink.
So change your code to
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'~~> Change A1 to the cell which is behind the shape
If Not Intersect(Target, Range("A1")) Is Nothing Then
'~~> Change this to the relevant sheet
With ThisWorkbook.Sheets("Sheet1")
.Visible = xlSheetVisible
.Activate
End With
'
'~~> Other Code if required
'
End If
End Sub
Next, Right Click on the Shape and click on Hyperlink and hyperlink to a cell behind the shape. You may also display information using the ScreenTip button in the Insert hyperlink dialog box.
We will do the sheet activating part in the above code rather than letting Excel do it.
Hope this solves your issue.

Resources