Yes, all possible. Paste the following subs in a module (in my case Module1). I assumed ActiveSheet.
With the below subs you can create shapes in 2 ways:
1. Use the run macro list (ALT + F8) to run "Create Buttons". Any selected cell will be populated with shapes.
2. Call the "Create_Buttons_From_Range" macro with
Call Module1.Create_Buttons_From_Range(Your_Range_Here)
in your code.
Range is optional, which in that case will be the current selection.
Either case: Modify
shp.OnAction = "Module1.Button_Test"
Change "Module1" to your module name where Button_Test is located.
Change "Button_Test" accordingly (post #7)
Note: Using
Option Private Module
at the top of the module would hide Public Subs from the run macro list, they would still be accessible by "Module_Name.Sub_Name".
Jaafar Tribak's solutions (post #6) may look intimidating but they are extremely nice. Definitely take a look at that if somehow this won't suit you.
VBA Code:
Public Sub Create_Buttons()
Call Create_Buttons_From_Range(Selection)
End Sub
Public Sub Create_Buttons_From_Range(Optional ByVal rng As Range)
Dim cCell As Range
Dim shp As Shape
If rng Is Nothing Then Set rng = Selection
For Each cCell In rng
On Error Resume Next
Set shp = ActiveSheet.Shapes("Cell_" & Replace(cCell.Address, "$", ""))
On Error GoTo 0
If shp Is Nothing Then
Set shp = ActiveSheet.Shapes.AddShape(msoShapeRectangle, cCell.Left, cCell.Top, cCell.Width, cCell.Height)
shp.Name = "Cell_" & Replace(cCell.Address, "$", "")
shp.OnAction = "Module1.Button_Test"
shp.Fill.Transparency = 1
shp.Line.Visible = msoFalse
shp.Placement = xlMoveAndSize
Set shp = Nothing
End If
Next cCell
End Sub