I came up with a VBA solution for this problem, but I'm getting a
runtime error on the very last line of the function. (Object variable
or With block variable not set)
It doesn't like this line; "RemoveZeroSeries = chartable.Address"
Everything else works fine. The message boxes which come up show
exactly what they are supposed to show, so the only error really is on
that one line.
Can anyone see what is wrong with the very last line of my function?
Code follows:
Private Sub RemoveEmptyChartSeries_Click()
Range("IncomeChartNominalNoZeros").Name =
RemoveZeroSeries(Range("IncomeChartNominalData"))
ActiveSheet.ChartObjects("Chart 5").Activate
ActiveChart.SetSourceData Source:=Sheets("Projection
1").Range("IncomeChartNominalNoZeros"), _
PlotBy:=xlRows
ActiveChart.SeriesCollection(1).XValues = Range("ProjectionYears")
End Sub
Private Function RemoveZeroSeries(inputrange As Range) As Range
Dim DownCounter, AcrossCounter, NumberOfRows As Integer
Dim temprange, chartable, upperleft As Range
' delete the next two lines for the function
'Dim inputrange As Range
'Set inputrange = Range("IncomeChartNominal")
MsgBox "There are " & inputrange.Rows.Count & " data series"
NumberOfRows = inputrange.Rows.Count
Set upperleft = inputrange.Resize(1, 1)
MsgBox "The Upper Left Cell is at " & upperleft.Address
' Find the first series which isn't all zeros, and name its range
"chartable"
For DownCounter = 0 To NumberOfRows
For AcrossCounter = 1 To (Range("YearsProjection").Value)
If Not upperleft.Offset(DownCounter, AcrossCounter).Value = 0 Then
Set chartable = Range(upperleft.Offset(DownCounter, 0).Address &
":" & upperleft.Offset(DownCounter,
Range("YearsProjection").Value).Address)
GoTo GotFirstChartRangeSoBreakOutOfLoop
End If
Next AcrossCounter
Next DownCounter
GotFirstChartRangeSoBreakOutOfLoop:
' Now build up the rest of the range by adding additional ranges which
also have non zeros
For DownCounter = 0 To NumberOfRows
For AcrossCounter = 1 To (Range("YearsProjection").Value)
If Not upperleft.Offset(DownCounter, AcrossCounter).Value = 0 Then
Set temprange = Range(upperleft.Offset(DownCounter, 0).Address &
":" & upperleft.Offset(DownCounter,
Range("YearsProjection").Value).Address)
Set chartable = Union(chartable, temprange)
AcrossCounter = 1
GoTo ThisSeriesHasAtLeastOneNonZeroSoMoveOnToTheNextOne
End If
Next AcrossCounter
ThisSeriesHasAtLeastOneNonZeroSoMoveOnToTheNextOne:
Next DownCounter
MsgBox "The range that will be charted is " & chartable.Address
RemoveZeroSeries = chartable.Address
End Function