Excel VBA TopLeftCell 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23019140/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:44:34  来源:igfitidea点击:

Excel VBA TopLeftCell property

excelvbaexcel-vbapropertiesshape

提问by kwiqry

I want to get .TopLeftCell property of each Shape Object in Selection.ShapeRange, but I got runtime error 438 saying 'this object does not support this property or method' when I run following code.

我想获得 Selection.ShapeRange 中每个 Shape 对象的 .TopLeftCell 属性,但是当我运行以下代码时,我收到运行时错误 438,提示“此对象不支持此属性或方法”。

Sub testTopLeftCell()
    For Each target In Selection.ShapeRange
        MsgBox target.TopLeftCell.Address
    Next
End Sub

However, following code works.

但是,以下代码有效。

Sub testTopLeftCell2()
    For Each target In Selection.ShapeRange
        MsgBox ActiveSheet.Shapes(target.Name).TopLeftCell.Address
    Next
End Sub

Could anyone teach me, what are the problems in the first code and why the second code works. I got really confused with this problem.

任何人都可以教我,第一个代码中的问题是什么以及为什么第二个代码有效。我真的对这个问题感到困惑。

回答by Gary's Student

This is because in these subs targetis a ShapeRange. A ShapeRange does not have a TopLeftCell property. It does have a Name property...........this is why the second sub works.

这是因为在这些 subs 中,目标是一个 ShapeRange。ShapeRange 没有 TopLeftCell 属性。它确实有一个 Name 属性.......这就是第二个子工作的原因。

Here is a way to get individual Shapes from a ShapeRange:

这是从 ShapeRange 中获取单个形状的方法:

Sub durall()
    Dim s As Shape, i As Long
    For i = 1 To Selection.ShapeRange.Count
        Set s = Selection.ShapeRange(i)
        MsgBox s.Name
        MsgBox s.TopLeftCell.Address
    Next i
End Sub

回答by kwiqry

For Eachfor ShapeRange Collection does not offer the access to ShapeRange(i), so targetin the first code is an instance of ShapeRange object(reffering same Object reffered by ShapeRange(i), but is not a Shape object.)

For Each因为 ShapeRange 集合不提供对 的访问ShapeRange(i),所以target在第一个代码中是一个 ShapeRange 对象的实例(引用由 ShapeRange(i) 引用的相同对象,但不是一个 Shape 对象。)

According to reference articles on MSDN, ShapeRange Objectdoes not have the .TopLeftCell property.

根据 MSDN 上的参考文章,ShapeRange 对象没有 .TopLeftCell 属性。

On the other hand, ShapeRange(i)means ShapeRange.Item(i)which returns single Shape Objectwith .TopLeftCell property. Of course, we also can access Shape Object as a member of Shapes Collection by Shapes(j).

另一方面,ShapeRange(i)意味着ShapeRange.Item(i)返回具有 .TopLeftCell 属性的单个形状对象。当然,我们也可以访问 Shape Object 作为 Shapes Collection by 的成员Shapes(j)

So, we can think the ShapeRange Object as a kind of interfaces for the Shapes Object, which offers properties slightly different from those Shape Object has(in this case ShapeRange does not have .TopLeftCell property but Shape has.)

因此,我们可以将 ShapeRange 对象视为 Shapes 对象的一种接口,它提供的属性与 Shape 对象所具有的属性略有不同(在这种情况下,ShapeRange 没有 .TopLeftCell 属性,但 Shape 具有。)

In other cases ShapeRange offers same properties as Shape Object does.

在其他情况下,ShapeRange 提供与 Shape Object 相同的属性。

This is why confusions come up with following code which returns same result 3 times, when single Shape Object is selected. But these result comes up, simply because .Name property and TypeName Function returns same result for ShapeRange Object and Shape Object.

This is why confusions come up with following code which returns same result 3 times, when single Shape Object is selected. 但是出现这些结果,仅仅是因为 .Name 属性和 TypeName 函数为 ShapeRange Object 和 Shape Object 返回相同的结果。

Sub testShapeRange()
    For Each target In Selection.ShapeRange
        MsgBox "target: " + target.Name + "," + TypeName(target)
        MsgBox "Selection.ShapeRange(1): " + _
            Selection.ShapeRange(1).Name + ", " + _
            TypeName(Selection.ShapeRange(1))
        MsgBox "ActiveSheet.Shapes(target.Name): " + _
            ActiveSheet.Shapes(target.Name).Name + "," + _
            TypeName(ActiveSheet.Shapes(target.Name))
    Next
End Sub