Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

button

excel

vba

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?

like image 893
tobefound Avatar asked Dec 30 '10 00:12

tobefound


People also ask

How do I add a button in Excel VBA?

Go to the Developer tab and click Insert under the Control section. Click the Insert button in the drop-down list that opens. Position your cursor in the worksheet location where you want the button to be created. A pop-up window will appear.

How do I assign a button to a function in VBA?

Click the worksheet location where you want the upper-left corner of the button to appear. The Assign Macro popup window appears. Note: If you have already inserted a button, you can right-click on it, and select Assign Macro. Assign a macro to the button and click OK.


1 Answers

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.

Mathematica graphics

like image 172
Dr. belisarius Avatar answered Sep 27 '22 18:09

Dr. belisarius