Help: Custom Error Bars and Absolute Cell Reference!

FrozenData

New Member
Joined
Feb 13, 2014
Messages
18
Hi Everyone,

I have a line graph with 8 series of data, all of which have custom error bars which I have calculated. I have to create one of these graphs each time I collect data. What I have been doing is copying the graph I made previously and dragging over the series to the new set of data. It takes a few seconds and its great. The issue I have is the error bar ranges do not move accordingly since they have an absolute reference. So I have to manually reassign the ranges for the custom error each time I collect a new set of data (which is tedious). How can I easily assign/reassign the error bar ranges without doing them individually? I've tried removing the absolute references ($) but they get automatically added back in by excel.

Thanks,
FrozenData
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I've decided to include some sample data in the hopes of getting some help. In the attached spreadsheet you'll find 3 days worth of data and then two additional empty days where I've copied a previous day's graph and moved over the series. Note that the error bars did not move along with them. I'm looking for a way to fix this issue or bulk edit the custom error bar feature.

Thanks,
Frozen

Sample Data (Hosted at Dropbox)
 
Upvote 0
But although tedious, once you have set them for all five charts once ever - is there really a problem? Assuming you use that sheet as a template, Save As, and add new data in to it?
 
Upvote 0
I enjoy the occasional challenge. Working with charts is a bit of pain. I couldn't get the triangle colour set correctly via VBA (it seems there are some problems / bugs with this generally in Excel 2010 on PC at least).
Here is a modified version of your file.
https://dl.dropboxusercontent.com/u/87147486/Sample Data.xlsm

It's not the most beautiful code, but if you now run the macro CreateNewChart (don't know how you do this on Mac) it should allow you to choose the column which has a 0 in (e.g. column G or M or S etc.).
Once you have picked the column where your data starts it should generate a new chart with error bars etc. It defaults to red and blue triangles, I couldn't get your green shades to work properly.
This works on PC Excel 2010 at least!
 
Upvote 0
This works perfectly on my home PC (excel 2013). I'll test it out on my mac at work Monday and hopefully it'll be amazing. I'll test my hand at modifying the code to include additional series, but the stuff you wrote is amazing!

I never knew "VBA" code worked like this and I plan on learning it to make my life easier in the future.

I can't thank you enough for you help!

-Frozendata
 
Upvote 0
No problem, I learnt a couple of new things which was useful. I have updated the code to be more general and to use constants where possible rather than hard coding rows in the code. It was quite short with minimal repetition but it can be a bit clearer having all those things declared at the top of the code. I'll leave the file there for a little while. I also added few more in line comments. Stepping through the code in the VBA editor using F8 is a good way to watch it execute line by line.

The full code is here for when the file disappears.

Code:
Option Explicit

Sub CreateNewChart()

    Dim chtNew As Chart
    Dim rngColumnZero As Range
    Dim lCol As Long
    Dim i As Long
    Const SERIESAROW As Long = 12, SERIESBROW As Long = 13, SERIESWIDTH As Long = 5
    Const SERIESARANGE As String = "$D$12", SERIESBRANGE As String = "$D$13"
    Const XVALUESROW As Long = 2
    Const ERRORAROW As Long = 15, ERRORBROW As Long = 16
    
    'Allow user to choose the column / cell containing the "0" data in, so that the appropriate range can be selected
    'If the user cancels the InputBox then an error will be generated, hence error handling is temporarily disabled
    On Error Resume Next 'Error handling disabled
    Set rngColumnZero = Application.InputBox(Prompt:="Please select column with 0 in", Title:="Graph maker", Type:=8)
    If rngColumnZero Is Nothing Then Exit Sub 'If user presses cancel this will be true, so exit routine
    On Error GoTo 0 'Error handling re-enabled
    lCol = rngColumnZero.Column 'Store the numerical column reference as a variable to use
    
    'Create new chart as object for later manipulation
    Set chtNew = ActiveSheet.Shapes.AddChart(xlLineMarkers).Chart
    With chtNew
        'Delete all auto added series which might get added if Excel tries to 'guess' the layout of your data
        For i = .SeriesCollection.Count To 1 Step -1
            .SeriesCollection(i).Delete
        Next i
        'Add both series
        .SeriesCollection.NewSeries
        .SeriesCollection(1).Name = ActiveSheet.Range(SERIESARANGE)
        .SeriesCollection(1).Values = ActiveSheet.Range(ActiveSheet.Cells(SERIESAROW, lCol), ActiveSheet.Cells(SERIESAROW, lCol + SERIESWIDTH))
        .SeriesCollection(1).XValues = ActiveSheet.Range(ActiveSheet.Cells(XVALUESROW, lCol), ActiveSheet.Cells(XVALUESROW, lCol + SERIESWIDTH))
        .SeriesCollection.NewSeries
        .SeriesCollection(2).Name = ActiveSheet.Range(SERIESBRANGE)
        .SeriesCollection(2).Values = ActiveSheet.Range(ActiveSheet.Cells(SERIESBROW, lCol), ActiveSheet.Cells(SERIESBROW, lCol + SERIESWIDTH))
        'Put legend at top
        .SetElement (msoElementLegendTop)
        'Tidy up gridlines and set y axis spacing
        .Axes(xlValue).MajorGridlines.Delete
        .Axes(xlValue).MinimumScale = 0
        .Axes(xlValue).MaximumScale = 60
        .Axes(xlValue).MajorUnit = 10
        .Axes(xlValue).TickLabels.NumberFormat = "0"
        'Set axis font size
        .Axes(xlCategory).TickLabels.Font.Size = 12
        .Axes(xlValue).TickLabels.Font.Size = 12
        'Resize chart overall
        .Parent.Height = 350
        .Parent.Width = 250
        'Resize plot area within chart
        .PlotArea.Height = 250
        .PlotArea.Top = 47
        .PlotArea.Width = 215
        .PlotArea.Left = 7
        ''''Series markers / lines
        'Triangle style (3) marker size 7
        .SeriesCollection(1).MarkerStyle = 3
        .SeriesCollection(1).MarkerSize = 7
        .SeriesCollection(2).MarkerStyle = 3
        .SeriesCollection(2).MarkerSize = 7
    End With
    
    'Format line as black for each series
    For i = 1 To 2
        With chtNew.SeriesCollection(i).Format.Line
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorText1
            .ForeColor.TintAndShade = 0
            .ForeColor.Brightness = 0
        End With
    Next i
    
    With chtNew
        'Legend text size
        .Legend.Format.TextFrame2.TextRange.Font.Size = 10.9
        'Move legend
        .Legend.Left = 58
        .Legend.Top = 47
        .Legend.Width = 144
        'Error bars pass as R1C1 references! Doesn't seem to like standard range addresses. See http://peltiertech.com/WordPress/custom-error-bars-in-excel-charts/#more-3221 for details
        .SeriesCollection(1).ErrorBar Direction:=xlY, Include:=xlBoth, Type:=xlCustom, _
        Amount:=ActiveSheet.Name & "!R" & ERRORAROW & "C" & lCol & ":R" & ERRORAROW & "C" & lCol + SERIESWIDTH, _
        MinusValues:=ActiveSheet.Name & "!R" & ERRORAROW & "C" & lCol & ":R" & ERRORAROW & "C" & lCol + SERIESWIDTH
        'Second series
        .SeriesCollection(2).ErrorBar Direction:=xlY, Include:=xlBoth, Type:=xlCustom, _
        Amount:=ActiveSheet.Name & "!R" & ERRORBROW & "C" & lCol & ":R" & ERRORBROW & "C" & lCol + SERIESWIDTH, _
        MinusValues:=ActiveSheet.Name & "!R" & ERRORBROW & "C" & lCol & ":R" & ERRORBROW & "C" & lCol + SERIESWIDTH
    End With
    
End Sub
 
Upvote 0
Hey, I'm at working now and trying to use the macro on my Mac excel 2011. There seems to be a error with the error bar part of the code with the mac (works fine on my PC at home). I did some searching of mac vba code and tried to change Type:=xlCustom to Type:=xlErrorBarTypeCustom as well as Include:=xlBoth to Include:=xlErrorBarIncludeMinusValues it did not help.

Code:
.SeriesCollection(1).ErrorBar Direction:=xlY, Include:=xlErrorBarIncludeMinusValues, Type:=xlErrorBarTypeCustom, _
 Amount:=ActiveSheet.Name & "!R" & ERRORAROW & "C" & lCol & ":R" & ERRORAROW & "C" & lCol + SERIESWIDTH, _
 MinusValues:=ActiveSheet.Name & "!R" & ERRORAROW & "C" & lCol & ":R" & ERRORAROW & "C" & lCol + SERIESWIDTH

Any idea on how to fix it?
The arrow is pointing to the third line.
 
Upvote 0
No Mac experience, sorry. If you record a macro on the Mac (assuming you can) and carry out the manual steps of adding error bars to a chart, what does the recorded code look like? That might give you some clues if there are any differences.
 
Upvote 0

Forum statistics

Threads
1,215,744
Messages
6,126,629
Members
449,323
Latest member
Smarti1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top