get the row number of a button

luolovepi

Board Regular
Joined
Jun 9, 2011
Messages
116
Hi,
I use this line to create buttons:
Code:
ActiveSheet.Buttons.Add ActiveCell.Left, ActiveCell.Top, ActiveCell.Width, ActiveCell.Height

Later I want to find the row number of this button, how could I find it using VBA?

Thanks!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Assuming you stored the name somewhere...

RowNumber = ActiveSheet.Shapes(TheButtonName).TopLeftCell.Row

where TheButtonName would be something like "Button 1".
 
Upvote 0
Assuming you stored the name somewhere...

RowNumber = ActiveSheet.Shapes(TheButtonName).TopLeftCell.Row

where TheButtonName would be something like "Button 1".

Thank you Rick,

I set the button name just after adding the button as below:

Code:
 Button.Name = "Line" & ActiveCell.Row

Is it correct to do so?
if is, I'm very unsure how to get the button name...
 
Upvote 0
And what is "Activesheet.Buttons"? Worksheet object doesn't have Buttons collection. Add button like this:

Code:
Sub AddButton()
    With ActiveCell
        ActiveSheet.OLEObjects.Add _
            ClassType:="Forms.CommandButton.1", _
            DisplayAsIcon:=False, _
            Link:=False, _
            Left:=.Left, _
            Width:=.Width
    End With
End Sub
 
Upvote 0
Dear Sektor,
Sorry for the late reply.

I'm not sure whether Activesheet.Buttons is correct or not. I just copied it from a webpage.

Code:
With ActiveCell
        ActiveSheet.OLEObjects.Add _
            ClassType:="Forms.CommandButton.1", _
            DisplayAsIcon:=False, _
            Link:=False, _
            Left:=.Left, _
            Width:=.Width
    End With
    Button.Caption = ActiveCell.Row
    Button.Name = "line" & ActiveCell.Row

I use your code then. Thanks! and want is "Forms.CommandButton.1"? Does this mean button Name--CommandButton1? If I have other cells where buttons will be created, should I change this classtype? Is it to set "1" as a string variable which will increment automatically?

And is it correct to set caption and name as above? Is it possible to get which row a particular button locates at?

Thank you Sektor!

Best regards,
lolo

And what is "Activesheet.Buttons"? Worksheet object doesn't have Buttons collection. Add button like this:

Code:
Sub AddButton()
    With ActiveCell
        ActiveSheet.OLEObjects.Add _
            ClassType:="Forms.CommandButton.1", _
            DisplayAsIcon:=False, _
            Link:=False, _
            Left:=.Left, _
            Width:=.Width
    End With
End Sub
 
Last edited:
Upvote 0
Hi Sektor,
Your code gives me an error message:"Object required", why?

Best regards,
lolo
And what is "Activesheet.Buttons"? Worksheet object doesn't have Buttons collection. Add button like this:

Code:
Sub AddButton()
    With ActiveCell
        ActiveSheet.OLEObjects.Add _
            ClassType:="Forms.CommandButton.1", _
            DisplayAsIcon:=False, _
            Link:=False, _
            Left:=.Left, _
            Width:=.Width
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,136
Members
452,890
Latest member
Nikhil Ramesh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top