Catch event on chart click

前端 未结 4 1234
谎友^
谎友^ 2020-12-18 10:39

I need to catch event in Excel VBA when I click on the chart.

I want to bring the chart to the front, when it is activated, but I can\'t find an appropriate event.

4条回答
  •  梦毁少年i
    2020-12-18 11:06

    If I understood the question, I faced the same problem. Whenever there are several overlapping charts, their visualization precedence follows the ZOrder.

    In Excel 2003, when one selected a chart, it came to the foreground (at least for visualization, I do not know whether its ZOrder was temporarily changed). When the chart was deselected, its visualization precedence returned to "normal".

    Starting with Excel 2007, charts do not temporarily come to foreground for visualization when selected, so if they are buried behind other charts (or possibly other Shapes), the only option to see them in full is to bring them to front. This has two downsides: 1) more clicks are needed, 2) the (possibly intended) ZOrder is lost.

    Even Jon Peltier, in a post from 5th May 2009, mentioned that there is no workaround for that.

    I have attempted a solution based on:

    1. Detecting the activation of a chart.
    2. Storing its current ZOrder for later use.
    3. Bringing it to front.
    4. After deselecting the chart, restoring its original ZOrder.

    This is the basic idea, and the scheme works fairly well, with a few glitches. I have actually based my code on the page by Jon Peltier quoted here by brettdj. One of the modifications is

    Private Sub EvtChart_Activate()
        Application.EnableEvents = False
        ActivatedChart = EvtChart.name
        If (TypeName(EvtChart.Parent) = "ChartObject") Then
        ' Chart is in a worksheet
          Dim chObj As ChartObject
          Set chObj = EvtChart.Parent
          chObj.BringToFront
        Else
        ' Chart is in its own sheet
        End If
        Application.EnableEvents = True
    End Sub
    

    Use something similar for EvtChart_Deactivate. I hope that the idea is useful.

提交回复
热议问题