Button positions listed out

adeeshc

New Member
Joined
Dec 20, 2011
Messages
24
Hello All,

Been desperately looking for a code snippet which i could use to list the position coordinates of all buttons used in a workbook.

What i want is to list down the Sheet Name - Button Name - Button Height - Button Width - Button Top - Button Left in a new worksheet and append one after the other

Something like a loop to run through each sheet and within that another loop to list the names and other relevant properties of each button in that sheet and close loop, move to the next.

Would be really grateful if someone could help here.

Thank You.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Something like this?

Code:
Public Sub ListAllButtons()

Dim thisSheet As Worksheet
Dim buttonSheet As Worksheet
Dim thisRow As Long
Dim i As Long

Set buttonSheet = ActiveWorkbook.Sheets.Add
buttonSheet.Name = "Button Positions"
buttonSheet.Cells(1, 1).Value = "Sheet Name"
buttonSheet.Cells(1, 2).Value = "Button Name"
buttonSheet.Cells(1, 3).Value = "Button Height"
buttonSheet.Cells(1, 4).Value = "Button Width"
buttonSheet.Cells(1, 5).Value = "Button Top"
buttonSheet.Cells(1, 6).Value = "Button Left"

thisRow = 2
For Each thisSheet In ActiveWorkbook.Sheets
    For i = 1 To thisSheet.Buttons.Count
        With buttonSheet
            .Cells(thisRow, 1) = thisSheet.Name
            .Cells(thisRow, 2) = thisSheet.Buttons(i).Name
            .Cells(thisRow, 3) = thisSheet.Buttons(i).Height
            .Cells(thisRow, 4) = thisSheet.Buttons(i).Width
            .Cells(thisRow, 5) = thisSheet.Buttons(i).Top
            .Cells(thisRow, 6) = thisSheet.Buttons(i).Left
        End With
    Next i
Next
buttonSheet.Columns("A:F").AutoFit

End Sub
 
Upvote 0
Thanks for the quick reply.

I am getting an type mismatch error , which when debugged highlights the 'next' to the initial For loop. Tried it with 'Next thissheet' but still not working. Please help.


-- removed inline image ---
 
Upvote 0
Do you have one or more charts in your workbook perhaps?

Try this instead:

Code:
For Each thisSheet In ActiveWorkbook.Worksheets

WBD
 
Upvote 0
Yes I do have more than one charts in the workbook.

Yes tried it with - For Each thisSheet In ActiveWorkbook.Worksheets but this time no error but simply creates the sheet with the headings and stops.

Also tried - For Each thisSheet In ThisWorkbook.Worksheets but again with same result.

Between could see a line where it gives thisRow = 2 but thisRow is not added afterwards, will this not result in pasting the positions to the same row always.
 
Upvote 0
I think the problem is because the buttons i have in the workbook are all active x control buttons.

I added a form controls button and checked and this is working fine.

Kindly help in tweaking the code to suit active x control buttons.
 
Upvote 0
I'm having a bad day it seems. I tested this but only with a single button on a single sheet do I didn't pick up my mistake. Also, I didn't think about OLEObjects. Give this a whirl:

Code:
Public Sub ListAllButtons()

Dim buttonSheet As Worksheet
Dim thisSheet As Worksheet
Dim thisRow As Long
Dim i As Long

Set buttonSheet = ActiveWorkbook.Sheets.Add
buttonSheet.Name = "Button Positions"
buttonSheet.Cells(1, 1).Value = "Sheet Name"
buttonSheet.Cells(1, 2).Value = "Button Name"
buttonSheet.Cells(1, 3).Value = "Button Height"
buttonSheet.Cells(1, 4).Value = "Button Width"
buttonSheet.Cells(1, 5).Value = "Button Top"
buttonSheet.Cells(1, 6).Value = "Button Left"

thisRow = 2
For Each thisSheet In ActiveWorkbook.Worksheets
    For i = 1 To thisSheet.Buttons.Count
        With buttonSheet
            .Cells(thisRow, 1) = thisSheet.Name
            .Cells(thisRow, 2) = thisSheet.Buttons(i).Name
            .Cells(thisRow, 3) = thisSheet.Buttons(i).Height
            .Cells(thisRow, 4) = thisSheet.Buttons(i).Width
            .Cells(thisRow, 5) = thisSheet.Buttons(i).Top
            .Cells(thisRow, 6) = thisSheet.Buttons(i).Left
        End With
        thisRow = thisRow + 1
    Next i
    
    For i = 1 To thisSheet.OLEObjects.Count
        If thisSheet.OLEObjects(i).progID Like "*.CommandButton.*" Then
            With buttonSheet
                .Cells(thisRow, 1) = thisSheet.Name
                .Cells(thisRow, 2) = thisSheet.OLEObjects(i).Name
                .Cells(thisRow, 3) = thisSheet.OLEObjects(i).Height
                .Cells(thisRow, 4) = thisSheet.OLEObjects(i).Width
                .Cells(thisRow, 5) = thisSheet.OLEObjects(i).Top
                .Cells(thisRow, 6) = thisSheet.OLEObjects(i).Left
            End With
            thisRow = thisRow + 1
        End If
    Next i
Next
buttonSheet.Columns("A:F").AutoFit

End Sub

WBD
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

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