John asked that I post my button macros, so here they are:
Function CreateButton() As Object
'Creates a Form Button in Selection Cell(s)
' Assigns a universal OnAction
Dim dL As Double _
, dT As Double _
, dW As Double _
, dH As Double
Dim sh As Worksheet
With Selection
dL = .Left
dT = .Top
dW = .Width
dH = .Height
Set sh = .Worksheet
On Error Resume Next
Set CreateButton = sh.Buttons.Add(dL, dT, dW, dH)
If Not CreatButton is Nothing Then
With CreateButton
.Placement = xlMoveAndSize
.OnAction = "ButtonClicked"
End With
End If
End With
End Function
Sub ButtonClicked()
Dim s As String _
, bnButton As Button _
, sCaption As String _
, sName As String
'Determine what button was clicked
On Error Resume Next
s = Application.Caller
If s <> "" Then
Set bnButton = ActiveSheet.Buttons(s)
End If
On Error GoTo 0
If Not bnButton Is Nothing Then
'Get Name and Caption
With bnButton
sCaption = .Caption
sName = .Name
End With
'Examples of Use
' I suggest using sName, so users can change caption
' without any effect on macro
Select Case sCaption
Case "New"
'Call NewData
Case "Edit"
'Call EditData
End Select
'I suggest renaming buttons also
' so you can easily tell what it's supposed to do
Select Case sName
Case "Button 1"
MsgBox "You Clicked 'Button 1'"
Case "Button 2"
MsgBox "You Clicked 'Button 2'"
Case "bnAdd"
MsgBox "You Clicked 'Add'"
End Select
End If
End Sub
\1
Bookmarks