Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50112

Add custom error bars from data adjacent to series data using VBA

$
0
0
I have a chart that contains several series of data that are organized under the same columns. For example, Series1 = (C3:C9,G3:G9), Series2 = (C10:C16,G10:G16), etc. I want to use a macro that automatically adds custom error bars whose positive and negative ranges are one column to the right of the series Y-data, e.g. H3:H9 for Series1. Below is my attempt at the code but I have difficulty defining the error bar range (see Line 6). I would like the macro to add error bars to all series in the chart. Anybody have suggestions?

Code:

Sub ErrorBars()
    Dim txt As String, srsYval As String, i As Long, srs As Series, errRng As Range
    For i = 1 To ActiveChart.SeriesCollection.Count
        txt = ActiveChart.SeriesCollection(i).Formula
        srsYval = Split(txt, ",")(2)
        Set errRng = Range(srsYval).Offset(0, 1)
           
        ActiveChart.SeriesCollection(i).Select
        ActiveChart.SeriesCollection(i).HasErrorBars = True
        ActiveChart.SeriesCollection(i).ErrorBars.Select
        ActiveChart.SeriesCollection(i).errorbar Direction:=xlY, Include:=xlBoth, _
                Type:=xlCustom, Amount:=errRng
        ActiveChart.SeriesCollection(i).ErrorBars.Select
    Next

End Sub


Viewing all articles
Browse latest Browse all 50112