VBA Excel:与形状一起使用时,选择集合循环索引引用了不正确的范围

发布于 2024-10-07 07:09:56 字数 685 浏览 6 评论 0原文

考虑一个包含多个形状和一个片段 (1) 的工作表:

'(1)
For i = 1 To Selection.Count
    MsgBox Selection(i).Name
Next

无论选择哪个形状(当然必须大于 1),似乎总是返回前两个形状。
考虑使用 ShapeRange (2):

'(2)
For i = 1 To Selection.Count
    MsgBox Selection.ShapeRange(i).Name
Next

此 (2) 实际上返回所选形状的名称,与 (3) 一样:

'(3)
For i = 1 To Selection.ShapeRange.Count
    MsgBox Selection.ShapeRange(i).Name
Next

如果选择的形状少于 1 个,则 (1) 会产生错误,而 (3) 似乎工作正常 -除非图表是唯一的选择,否则将返回自动化错误:“调用的对象已与客户端断开连接。”

我不明白为什么第一个通常无法返回正确的形状 - 我假设 Selection 集合比看起来更复杂,而且我也不明白为什么 Selection.ShapeRange.Count 在一个 Chart 时失败被选中,但当有一个 Shape 时成功。

非常感谢对此的任何启发

Consider a Worksheet containing a number of Shapes, and a snippet (1):

'(1)
For i = 1 To Selection.Count
    MsgBox Selection(i).Name
Next

Regardless of which Shapes are selected (must be more than 1 of course) the first two Shapes seem to always be returned.
Consider use of ShapeRange (2):

'(2)
For i = 1 To Selection.Count
    MsgBox Selection.ShapeRange(i).Name
Next

This (2) actually returns the name of the selected Shape, as does (3):

'(3)
For i = 1 To Selection.ShapeRange.Count
    MsgBox Selection.ShapeRange(i).Name
Next

While (1) produces an error if less than 1 Shape is selected, (3) appears to work fine - unless a Chart is the sole selection, upon which an Automation Error is returned: "The object invoked has disconnection from its clients."

I don't understand why the first fails to return the correct shape in general - I'm assuming that the Selection collection is more complex than meets the eye, and I also don't understand why Selection.ShapeRange.Count fails when one Chart is selected, but succeeds when one Shape is.

Would greatly appreciate any light shed on this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初吻给了烟 2024-10-14 07:09:56

我在一张纸上有一个图表和五个矩形,并且我使用的是 Excel 2003。

如果我先选择一个矩形,然后选择图表,则 (1) 代码适用于我,不会出现错误。它返回我所期望的两个形状的名称。

如果我先选择图表,然后选择矩形,它会返回图表两次,就好像图表是两个选定的形状一样。我只能假设这是一个错误。但这是一个奇怪的错误,因为如果我在不更改选择的情况下再次运行代码,它会返回与我首先选择矩形相同的结果。奇怪的。

如果选择的形状少于一个,则意味着选择了一个范围。在 Excel 中总是会选择某些内容,因此如果您没有选择任何形状,则您的 Selection 对象可能引用了 Range 对象。您可以使用它

Typename(Selection)

来确定选择的内容。如果您确实选择了范围,并且该范围没有定义名称,则“名称”属性将返回错误。

在 Excel 中选择“图表形状”非常困难(不可能?)。当您单独选择图表时,请检查选择点 - 它们是黑色方块。现在按住控制键并选择另一个形状。图表上的选择点变成白色圆圈。当它被单独选择时,Selection 对象实际上是一个 ChartArea 对象。 Excel“猜测”当您选择图表时,您确实想要选择图表的一个组件(默认情况下为 ChartArea)。由于 ChartArea 没有 ShapeRange 属性,因此您会收到错误。

至于第一个假设的错误,这里有一些代码可以证明这一点。请注意,在第三部分中,我检查了类型名称,但没有对其执行任何操作。这似乎让 Excel 知道实际选择了什么

Sub testshapes()

    Dim i As Long
    Dim sType As String

    Sheet1.Shapes.Range(Array("Rectangle 5", "Chart 6")).Select

    For i = 1 To Selection.Count
        Debug.Print Selection(i).Name
    Next i

    Sheet1.Shapes.Range(Array("Chart 6", "Rectangle 5")).Select

    For i = 1 To Selection.Count
        Debug.Print Selection(i).Name
    Next i

    Sheet1.Shapes.Range(Array("Chart 6", "Rectangle 5")).Select

    sType = TypeName(Selection(1)) 'avoids chart selected first bug
    For i = 1 To Selection.Count
        Debug.Print Selection(i).Name
    Next i

End Sub

。在第一部分中,我得到了正确的答案。在第二部分中,我得到了两个 debug.print 语句的“图表 6”。在第三部分,我得到了正确的答案。

我不确定这是否完全回答了您的问题,但我希望它能让您更接近。

I have one chart and five rectangles on a sheet and I'm using Excel 2003.

If I select a rectangle first, then the chart, the (1) code works for me without error. It returns the names of the two shapes I selected as I would expect.

If I select the chart first, then a rectangle, it returns the chart twice as if the chart is both of the selected shapes. I can only assume this is a bug. But a strange bug because if I run the code again without changing the selection, it returns the same as if I had selected the rectangle first. Odd.

If less than one shape is selected, that means a range is selected. Something is always selected in Excel, so if you don't have any shapes selected, it's likely your Selection object refers to a Range object. You can use

Typename(Selection)

to determine what is selected. If you do have a Range selected, and that range doesn't have defined name, the Name property will return an error.

It is very difficult (impossible?) to select a "chart shape" in Excel. Check out the selection points when you select a chart by itself - they're black squares. Now hold down the control key and select another shape. The selection points on the chart turn to white circles. When it's selected by itself, the Selection object is actually a ChartArea object. Excel "guesses" that when you select a chart you really want to select a component of a chart (the ChartArea by default). Because the ChartArea doesn't have a ShapeRange property, you get an error.

As to the first assumed bug, here is some code that proves it out. Note in the third section, I check the Typename, but don't do anything with it. This seems to jolt Excel into knowing what is actually selected

Sub testshapes()

    Dim i As Long
    Dim sType As String

    Sheet1.Shapes.Range(Array("Rectangle 5", "Chart 6")).Select

    For i = 1 To Selection.Count
        Debug.Print Selection(i).Name
    Next i

    Sheet1.Shapes.Range(Array("Chart 6", "Rectangle 5")).Select

    For i = 1 To Selection.Count
        Debug.Print Selection(i).Name
    Next i

    Sheet1.Shapes.Range(Array("Chart 6", "Rectangle 5")).Select

    sType = TypeName(Selection(1)) 'avoids chart selected first bug
    For i = 1 To Selection.Count
        Debug.Print Selection(i).Name
    Next i

End Sub

In the first section, I get the right answer. In the second section, I get "Chart 6" for both debug.print statements. In the third section, I get the right answer.

I'm not sure if this totally answers your question, but I hope it gets you closer.

黯然#的苍凉 2024-10-14 07:09:56

选择可以包含形状以外的对象。并非选择中的所有对象都具有名称属性。这可能就是您选择多个形状时出现错误的原因 - 因为您选择的不仅仅是形状。扫描所有选定形状的正确方法是#3,如讨论的在这里

“对象调用”错误听起来像是 Excel 错误。我会简单地捕获错误,处理它,然后查找 MS 知识库以获取有关如何解决该错误和/或最大程度地减少其发生的指导。

The Selection can contain objects other than shapes. Not all of the objects in the selection will have a name property. That's probably why you're getting errors with more than one shape selected - because you're selecting more than just shapes. The proper way to scan all selected shapes is #3 as discussed here.

The "object invoked" error sounds like an Excel bug. I would simply catch the error, handle it, and look to the MS knowledge base for guidance on how to work around it and/or minimize its occurrence.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文