Search code examples
excelvbaindexingchartsline

Return index of selected series


is there a simple way to get the index of a selected line in a Excel chart using VBA? I have a chart, where the user selects a series. Then a macro should do some thing. I'm looking for something like idx = Selection.getIndex.

I need this idx to call other functions which do some stuff with the series using the index to select the specific series (e.g. FullSeriesCollection(idx).DataLabels.labelPos=...)


Solution

  • Three ideas for you:

    1st- use object variable instead of index reference:

    Dim SER As Series
    Set SER = Selection
    SER.anyproperty.anymethod... 'do your action here
    

    2nd- use plot order to get...index (but not sure if this always match)?

    Dim inxSer As Integer
    inxSer = Selection.PlotOrder
    

    3rd- read last parameter of series formula

    Dim inxFromFormula As Integer
    Dim tmpSerFormula As String
    tmpSerFormula = Selection.Formula
    
    tmpSerFormula = Mid(tmpSerFormula, InStrRev(tmpSerFormula, ",") + 1)
    inxFromFormula = Left(tmpSerFormula, Len(tmpSerFormula) - 1)