Comment add a button programmatically in VBA next to some sheet cell data?

Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

Comment add a button programmatically in VBA next to some sheet cell data?

Message par Clippy »

I have a function that generates data for say 100 rows (and 2 columns). For each row (in the 3rd column) I need to add a button which, when clicked, brings up a custom modal dialog box giving the user 4 options/buttons to choose from.

Any idea how to do this?
Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

Re: Comment add a button programmatically in VBA next to some sheet cell data?

Message par Clippy »

I think this is enough to get you on a nice path:

```
Sub a()
Dim btn As Button
Application.ScreenUpdating = False
ActiveSheet.Buttons.Delete
Dim t As Range
For i = 2 To 6 Step 2
Set t = ActiveSheet.Range(Cells(i, 3), Cells(i, 3))
Set btn = ActiveSheet.Buttons.Add(t.Left, t.Top, t.Width, t.Height)
With btn
.OnAction = "btnS"
.Caption = "Btn " & i
.Name = "Btn" & i
End With
Next i
Application.ScreenUpdating = True
End Sub

Sub btnS()
MsgBox Application.Caller
End Sub

```

It creates the buttons and binds them to butnS(). In the btnS() sub, you should show your dialog, etc.
Répondre

Revenir à « Excel & VBA »