Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA TopLeftCell属性_Vba_Excel_Properties_Shape - Fatal编程技术网

Excel VBA TopLeftCell属性

Excel VBA TopLeftCell属性,vba,excel,properties,shape,Vba,Excel,Properties,Shape,我想获取Selection.Shaperage中每个Shape对象的.TopLeftCell属性,但在运行以下代码时出现运行时错误438,其中说“此对象不支持此属性或方法” Sub testTopLeftCell() For Each target In Selection.ShapeRange MsgBox target.TopLeftCell.Address Next End Sub 但是,下面的代码是有效的 Sub testTopLeftCell2()

我想获取Selection.Shaperage中每个Shape对象的.TopLeftCell属性,但在运行以下代码时出现运行时错误438,其中说“此对象不支持此属性或方法”

Sub testTopLeftCell()
    For Each target In Selection.ShapeRange
        MsgBox target.TopLeftCell.Address
    Next
End Sub
但是,下面的代码是有效的

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

有谁能教我,第一个代码有什么问题,第二个代码为什么工作。我真的被这个问题弄糊涂了。

这是因为在这些潜艇中,目标是一个变形器。ShapeRange没有TopLeftCell属性。它确实有一个Name属性…………这就是第二个子项工作的原因

以下是从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

对于每个
的shaperage集合,不提供对
shaperage(i)
的访问,因此第一个代码中的
目标
是shaperage对象的实例(引用shaperage(i)引用的相同对象,但不是shaperage对象。)

根据MSDN上的参考文章,不具有.TopLeftCell属性

另一方面,
shaperage(i)
表示
shaperage.Item(i)
,它返回带有.TopLeftCell属性的single。当然,我们也可以通过
Shapes(j)
访问Shape对象作为Shapes集合的成员

因此,我们可以将ShapeRange对象视为Shapes对象的一种接口,它提供的属性与Shape对象的属性略有不同(在本例中,ShapeRange没有.TopLeftCell属性,但Shape有)

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

这就是为什么当选择单个形状对象时,confusions会产生以下代码,返回相同的结果3次。但出现这些结果的原因很简单,因为.Name属性和TypeName函数为ShapeRange对象和Shape对象返回相同的结果

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

形状(例如一个或多个矩形、图片、椭圆形…)在
选项中。我确信第二个代码可以工作,但当我在选择形状后运行代码时,第一个代码不能工作。有类似的问题,但没有完全回答。仅供参考,我当前的环境是MS Excel 2007。无论如何,谢谢。也许使用option explicit,并将target:Dim target声明为ShapeTanks,您的评论帮助我找到了这个问题的答案。