vba 在excel表格中定义对象的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22730709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Define the position of object in excel sheet
提问by Aleksey Potapov
Issue: Put the object's (here is the circle) contents into the corresponding cell. (On the picture - put "5" into E3 cell, and also put second "5" into F3)
问题:将对象(这里是圆圈)的内容放入相应的单元格中。(在图片上-将“5”放入E3单元格,并将第二个“5”放入F3)
I have some experience in VBA-programming (for excel). And all I know - this is impossible to do. We can iterate through all the objects on the sheet, get it's content, but we still can not define it's (the objects') coordinate. So this issue is unsolvable.
我在 VBA 编程方面有一些经验(对于 excel)。而我所知道的——这是不可能做到的。我们可以遍历工作表上的所有对象,获取它的内容,但我们仍然无法定义它的(对象的)坐标。所以这个问题无解。
But I decided to ask the SO community, because I may be wrong. If you give me some info - I'll appreciate that.
但我决定问 SO 社区,因为我可能是错的。如果你给我一些信息 - 我会很感激的。
NB.I'm not asking to solve my "university's problem". I just want a hint, where I should look or something.
注意。我不是要解决我的“大学问题”。我只是想要一个提示,我应该在哪里看什么的。
回答by Portland Runner
If I understand you correctly, you want to return the cell location that the shape is positioned at? You can display the cell address of the top left corner of the shape like this:
如果我理解正确,您想返回形状所在的单元格位置吗?您可以像这样显示形状左上角的单元格地址:
Sub getLocation()
Dim wks As Worksheet
Set wks = Sheets("Sheet1")
For Each sShapes In wks.Shapes
MsgBox (sShapes.TopLeftCell.Address)
Next
End Sub
You can also move the shape to a specific cell like this:
您还可以将形状移动到特定的单元格,如下所示:
Sub getLocation()
Dim wks As Worksheet
Set wks = Sheets("Sheet1")
'move shape 'Oval 1' to cell H3
wks.Shapes("Oval 1").Left = wks.Cells(3, 8).Left
wks.Shapes("Oval 1").Top = wks.Cells(3, 8).Top
End Sub