Selecting a shape in Excel with VBA

左心房为你撑大大i 提交于 2020-04-14 07:48:53

问题


I am trying to select a shape by name using VBA. In my sheet, I have 10 shapes named 1 to 10 and want a specific one. For some reason, the shape that excel selects with my code does not seem to be the shape with the name I told it to select. As far as I can tell, the shape that it chooses is random. Here is my code (The x value is supposed to retrieve a number value that I input from 1 to 10):

Sub FindTheShape()

Sheets("Fleet 1").Select
Dim x As Long
x = ActiveSheet.Range("$A$1000").End(xlUp).Value

ActiveSheet.Shapes(x).Select

End Sub

Please help, any ideas are greatly appreciated.


回答1:


Your x gives the index-number not the name, when your shapes are named 1 to 10 make a string from your x:

ActiveSheet.Shapes(CStr(x)).Select



回答2:


As far as I can tell, the shape that it chooses is random.

This happens because when you're using numeric value, you're in fact referring to the position of a shape in Shapes collection. When you use string value, you're referring to shape's name. The same goes for sheets. See my answer.



来源:https://stackoverflow.com/questions/51157421/selecting-a-shape-in-excel-with-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!