Question

I'm graphing a set of data that has blanks in some cells. In the blank cells I have formulas and I have to keep the formulas. When I graph the data, the blank cells are graphed as zeros. I'd like to put gaps instead of zeros in the graph. I tried right click on the graph > Select Data > Hidden and Empty Cells Settings > Show empty cells as Gaps. But this did not help!

Was it helpful?

Solution

Instead of putting zeros or empty strings try to put #N/A.

You can do it with a formula like =IF([test],[value],NA()).

This will allow the graph not to show the missing values as zeros, but if I understand your question, it is still not what you want, because you want the missing values to be represented as gaps, not as missing values.

The only way that I know of to see the gaps is to use a scattered graph.

As far as I know, all the graphs that make a line to join two points, do join two points, and don't have the concept of missing point. They just join the two closest points.

A solution could be to make a VBA macro that goes inside the graph and changes the color of each graph line when the data is missing.

OTHER TIPS

A solution could be to make a VBA macro that goes inside the graph and changes the color of each graph line when the data is missing.

I have code, that modifies charts. It works for cells with #N/A, also na() function. Like old excel did. First, you need a module with public sub:

Public Sub FormatNA()
Dim myChart As ChartObject
Dim series_i As Integer, series_count As Integer
Dim values_i As Integer, values_count As Integer
Dim rows As Integer, r As Integer
Dim mySeries As Object
Dim myValues As Variant
Dim myPoint As Object
Application.ScreenUpdating = False
If ActiveSheet.ChartObjects.Count = 0 Then Exit Sub
' for each chart in active sheet
For Each myChart In ActiveSheet.ChartObjects
    ' Determine Chart Type
    Select Case myChart.Chart.ChartType
        Case xlLine, _
            xlLineMarkers, _
            xlLineMarkersStacked, _
            xlLineMarkersStacked100, _
            xlLineStacked, _
            xlLineStacked100, _
            xlXYScatter, _
            xlXYScatterLines, _
            xlXYScatterLinesNoMarkers, _
            xlXYScatterSmooth, _
            xlXYScatterSmoothNoMarkers
                ' for each series
                series_count = myChart.Chart.SeriesCollection.Count
                For series_i = 1 To series_count
                    ' for each data
                    Set mySeries = myChart.Chart.SeriesCollection(series_i)
                    Set myPoint = mySeries.Points(1)
                    myValues = mySeries.Values
                    values_count = UBound(myValues)
                    ' global formatting:
                    Select Case mySeries.ChartType
                        ' MARKERS:
                        Case xlLineMarkers, _
                            xlLineMarkersStacked, _
                            xlLineMarkersStacked100, _
                            xlXYScatter, _
                            xlXYScatterLines, _
                            xlXYScatterSmooth
                                With mySeries
                                    .MarkerForegroundColorIndex = myPoint.MarkerForegroundColorIndex
                                    .MarkerForegroundColor = myPoint.MarkerForegroundColor
                                    .MarkerBackgroundColorIndex = myPoint.MarkerBackgroundColorIndex
                                    .MarkerBackgroundColor = myPoint.MarkerBackgroundColor
                                    .MarkerForegroundColor = myPoint.MarkerForegroundColor
                                    .MarkerSize = myPoint.MarkerSize
                                    .MarkerStyle = myPoint.MarkerStyle
                                End With
                        ' NO MARKERS, JUST LINE:
                        Case Else
                    End Select
                    With mySeries
                        .Border.Color = myPoint.Border.Color
                        .Border.Weight = myPoint.Border.Weight
                        With .Format.Line
                            .ForeColor.RGB = myPoint.Format.Line.ForeColor.RGB
                            .BackColor.RGB = myPoint.Format.Line.BackColor.RGB
                            .Weight = myPoint.Format.Line.Weight
                            .Visible = msoTrue
                        End With
                    End With
                    For values_i = 2 To values_count
                        ' set line invisible if #NA
                        If IsEmpty(myValues(values_i - 1)) And Not IsEmpty(myValues(values_i)) Then
                            mySeries.Points(values_i).Format.Line.Visible = msoFalse
                            'mySeries.Points(values_i).Border.Color = RGB(255, 255, 255) ' for debugging
                            'mySeries.Points(values_i).Border.Weight = 1
                        End If
                    Next values_i
                Next series_i
        Case Else
            ' different chart type
    End Select
Next
Application.ScreenUpdating = True
End Sub

Then, you'll have to trigger this sub everytime you calculate worksheet: In ThisWorkbook define sub:

Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
Static Calculated As Boolean
If Not Calculated Then
    Call FormatNA
    Calculated = True
Else
    Calculated = False
End If
End Sub

Maybe it's not perfect, but it works for me. Sample of manipulated chart

May be it might be Usefull any one how has this problem,

Step1: First get Chartpage access and use Display blank as

   Excel.Chart chartPage = myChart.Chart;

   chartPage.DisplayBlanksAs = Excel.XlDisplayBlanksAs.xlInterpolated;

Happy Coding.

As stenci said, it's difficult to create a gap without VBA due to the presence of formulas in the cells. A time consuming solution is to delete the formulas, which provided blank cells, one by one so that they will then graph as gaps. For a large dataset that might be too time consuming.

There's a workaround if you're willing to open and close the file:

  1. Set the blank cell to appear empty. For example: =IF(COUNT(A1)>0,A1,"");
  2. Save a copy of your workbook in your preferred format because the next step will eliminate the formulas;
  3. Save the workbook as a .CSV file with a different file name;
  4. Close the file. Then reopen the file;
  5. Now a line graph will provide gaps for the empty cells.

Note that both sides of the gap need to have a line segment, i.e. at least two data cells on both sides of the gap. Specifically, this will graph a gap: A1=1, A2=2, A3=(blank), A4=4, A5=5.

And this will not graph a gap: A1=1, A2=(blank), A3=3, A4=4.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top