tags:

views:

46

answers:

2

Hi,

I have assigned macro to few buttons.

How can I find out inside macro which button was clicked?

I am doing as user form, where he can input peoples from family:

name1:
surname1:

name2:
surname2:
|add next member|

I wish button to appear always in last row of the last added person. For simplicity I think it is better to have like 100 empty forms in the sheet but all invisible at the begining.
Then when user clicks add next member I simply make next rows visible, and move button to next person. But to do that I need to know my current position.

Similar with deletion I would make rows invisible when remove button is clicked.

name1:
surname1:
[remove]

name2:
surname2:
[remove]

name3:
surname3:
|add next member|

I need to know which remove button was clicked.

EDIT: Found in web - what do you think, seems to be best /way

Dim r As Range
Set r = ActiveSheet.Buttons(Application.Caller).TopLeftCell
Range(Cells(r.Row, r.Column), Cells(r.Row, r.Column)).Select
+1  A: 

I always write wrappers for each button that then call the macro in question.

Like so:

Public Sub StoreButton_Click()

''// Store values for transaction sheet 3/27/09 ljr

  Call StoreTransValues(ActiveSheet)

End Sub

If you have only one button for any one page, you can just get the ActiveSheet property, and it will be the button on that page.


Edit:

Here's the code to get and use the name of the calling button:

Dim ButtonText As String

ButtonText = Application.Caller

ActiveSheet.Shapes(ButtonText).Delete

You would use the .Move method to move the button.

Lance Roberts
I can not simply adapt your proposal for my case. I still need to find out what is position of clicked button, then find which row it is, and finally remove all e.g. 2 rows above.
Gadolin
A: 

Since you have a macro wired to your button(s), I assume you know which button it is that was clicked. To get the location of the button, use this:

ActiveSheet.Shapes("ButtonName").TopLeftCell.Address

To move a button to a new location, use this:

Dim NewAddress as Range
NewAddress = ActiveSheet.Cells(5, 5) 'Or where ever you need it to go
ActiveSheet.Shapes("ButtonName").Left = NewAddress.Left
ActiveSheet.Shapes("ButtonName").Top = NewAddress.Top
Nick