excel 在一个或多个选定图表对象之间循环

rbl8hiat  于 2023-02-05  发布在  其他
关注(0)|答案(1)|浏览(83)

我想编写一段代码,无论我在Excel中选择一个还是多个图表,它都能正常工作。
实际上我使用这段代码,但是当只选择一个图表时,它失败了:

Dim obj As Object

'Check if any charts have been selected
If Not ActiveChart Is Nothing Then

    MsgBox ("Select multiple charts")

Else
    For Each obj In Selection

    'If more than one chart selected
    If TypeName(obj) = "ChartObject" Then

        Call AnotherMacro(obj.Chart)

    End If
Next

End If

我正在寻找一个解决方案工作到一个独特的潜艇。
谢谢你的帮忙

xoshrz7s

xoshrz7s1#

请尝试下一种方法:

Sub iterateSelectedCharts()
    Dim obj As Object
    If TypeName(Selection) = "ChartArea" Then 'for the case of only one selected chart:
        'Call AnotherMacro(ActiveChart)
    ElseIf TypeName(Selection) = "DrawingObjects" Then
        For Each obj In Selection
            If TypeName(obj) = "ChartObject" Then
                'Call AnotherMacro(obj.Chart)
            End If
        Next obj
    Else
        MsgBox ("Please, select charts") 'for the case when selection contains
                                         'other type of object(s): cells, rectangles, circles etc.
    End If
End Sub

相关问题