PDA

View Full Version : [SOLVED:] how to enable macro button for every sheet



KAMRAN AJ
06-12-2023, 01:58 PM
i want to create alteast fifty buttons and assign a macro them i dont know how to enable these macro enabled buttons for every sheet

June7
06-12-2023, 03:50 PM
These are buttons placed on sheets? AFAIK, button can be associated with only one sheet.

A user form might be what you need. Or migrate to Access database.

Paul_Hossler
06-12-2023, 05:40 PM
You could use a macro to add the buttons to each sheet

I created a button on Sheet1 with the size and placement, text, and macro ('Macro To Run')

The AddButtons put a similar button on the other sheets




Option Explicit


Sub AddButtons()
Dim ws As Worksheet
Dim oButton As Shape
Dim T As Double, L As Double, H As Double, W As Double
Dim M As String, C As String

Set oButton = Worksheets("Sheet1").Shapes("Button 1")
With oButton
T = .Top
L = .Left
H = .Height
W = .Width
M = .OnAction
C = .AlternativeText
End With

For Each ws In ThisWorkbook.Worksheets
If Not ws Is Sheet1 Then
ws.Select
ws.Buttons.Add(L, T, W, H).Select
Selection.OnAction = M
Selection.Text = C
End If
Next



End Sub


Sub MacroToRun()


MsgBox ActiveSheet.Name

End Sub