'Using VBA to add only populated ranges to a chart
I am working on a dynamic chart that pulls data from the range B53:K68. I have some cells that will be blank depending on what is selected from a drop-down list in cell B52. I want the chart to only search out and add populated cells rather than having a 0 or gap in the chart as this is being done on a bar graph. I have the code below which does successfully search for blanks and will do nothing if it is blank, but I am struggling to get it to cycle through to the next row in the range. Currently, the range names listed break the code, and I can only get it to work specifying actual cell references (like C53:K53 as an example) rather than any type of dynamic like I am trying for.
Sub Chart()
' Select correct sheet
Application.Goto ActiveWorkbook.Sheets("Graphical").Range("A1")
' Set variable i
Dim i As Integer
' Test if cell i,2 is not blank
For i = 53 To 68
Cells(i, 2).Select
If Cells(i, 2).Value <> "" Then
' If cell i,2 is not blank, add data to chart for cell i,3 through i,11
ActiveSheet.ChartObjects("Chart 5").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Name = "=Graphical!" & Range("B" & i)
ActiveChart.FullSeriesCollection(1).Values = "=Graphical!" & Range("$C" & i)
ActiveChart.FullSeriesCollection(1).XValues = "=Graphical!$C52:$K52"
End If
Next i
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
