You are here:

Excel/form Control Button disappearing!!!!

Advertisement


Question
QUESTION: Good day Bob,
I have created a button via the Form Controls.
Regularly, I loose my button. Or is it relocated???
The button properties are set to "move and size with cells".

Now, I would like to know if there is some code I can use to locate any button on a particular sheet?

Regards,

ANSWER: Sub whereIsTheButton()
msgbox activesheet.Buttons(1).topleftcell.address
End Sub

---------- FOLLOW-UP ----------

QUESTION: Thank you Bob, it has given a good start understanding how to use the Object properties.
I created this small utility sub to locate my "Button".
There are several buttons in one of my sheet but the .Buttons(1) always refer to $A$1 cell.
My questions:
1) How can I modify the code to be more dynamic (not to refer to .Buttons(1))
2) How can I know the Button name without refering to VBA (right click the button and go on Format Control - no sign of name)?


Sub LocateButtonWithMessage()
   Dim wksht As Worksheet, obj As Object
   Dim Counter As Integer    
   Counter = 0
   
   For Each wksht In Worksheets
       For Each obj In wksht.Shapes
         On Error Resume Next
         
         If TypeName(obj.OLEFormat.Object) <> "OLEObject" Then
         Counter = Counter + 1
         MsgBox ("Name: " & obj.Name _
         & vbCrLf & "Type: " & TypeName(obj.OLEFormat.Object) _
         & vbCrLf & "Top Left Location: " & wksht.Buttons(1).TopLeftCell.Address _
         & vbCrLf & "Worksheet: " & wksht.Name)
         End If
       Next
       MsgBox ("There are " & Counter & " Shape(s) in the sheet " & """" & wksht.Name & """")
       Counter = 0
   Next
End Sub

ANSWER: instead of Buttons(1) use Buttons(i) inside a for i=1 to activesheet.buttons.count/next loop
You can get the name of the button by ctrl/clicking the button and looking at the name in the name box - something like "Button 1" (which you can change when you create it)
Does that help?

---------- FOLLOW-UP ----------

QUESTION: Work great for the first part.  Used Select Case instead of For...
As far as how to get the name of the button without VBA, your trick does not work.
I suppose it is probably the same thing when you create/add a new sheet.  Excel give it a unique name (i.e. Sheet1 or Sheet 24...) and even if you change the name on the TAB, you can always refer it a Sheet1.  I guess it is the same for objects!

Confirmed by additionnal code added in previous Sub
'Create Log Report of ALL shape found in the Worksheet
  Sheets("LogShapes").Cells(pt_rd, 1) = obj.Name
  Sheets("LogShapes").Cells(pt_rd, 2) = TypeName(obj.OLEFormat.Object)
  Sheets("LogShapes").Cells(pt_rd, 3) = wksht.Buttons(ButtonCounter).TopLeftCell.Address
  Sheets("LogShapes").Cells(pt_rd, 4) = wksht.Name


Any other ideas how to get the name?

Answer
since it's possible to have more than one shape with the identical name (yes, happens), you can get the name of the shape, but when you access it you may access a different one! Only solution is to run some shape-naming procedure to look through all the shapes and assign it a unique name, like:
Sub shapeNamer()
   For Each x In ActiveSheet.Shapes
       n = n + 1
       x.Name = "Shape" & n
   Next
End Sub

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bob Umlas

Expertise

I`m a Microsoft Excel MVP (Most Valuable Professional) and have been since the inception of the program in 1995. I can answer every kind of Excel question except: API, Importing/exporting to other programs (powerpoint, word,...) Also check out my in-person training link at http://www.thumbtack.com/ny/new-york/excel-training/

Experience

Worked with MS Excel since version 0.99 (on the Mac!). Was contributing editor to Excellence Magazine, having written >300 articles. John Walkenbach said of me "I finally met someone who knows as much about Excel as I do."

Publications
Excellence, The Expert, Microsoft

Education/Credentials
BA in math, Hofstra University, 1965

Awards and Honors
MVP
Led sessions for the Convergence 2004-2006 seminar on Excel tips & tricks

©2016 About.com. All rights reserved.