Search code examples
vbaexcelexcel-2010excel-2013

Excel CheckBox Select Range of Chart


Option Explicit

Public PlotName As String
Public PlotRange As Range

Sub Tester()
Range("TCKWH.V.1").Select
AddPlot ActiveSheet.Range("C9:C13,D9:O13")
End Sub

Sub AddPlot(rng As Range)
 With ActiveSheet.Shapes.AddChart
    PlotName = .Name
    .Chart.ChartType = xlLineMarkers
    .Chart.SetSourceData Source:=Range(rng.Address())

.Chart.HasTitle = True
.Chart.ChartTitle.Text = Range("C9")

End With
Set PlotRange = rng
Application.EnableEvents = False
rng.Select
Application.EnableEvents = True
End Sub

Sub RemovePlot(rng As Range)
If Not PlotRange Is Nothing Then
    If Application.Intersect(rng, PlotRange) Is Nothing Then
        On Error Resume Next
        rng.Parent.Shapes(PlotName).Delete
        On Error GoTo 0
    End If
End If

End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
RemovePlot Target
End Sub

I have this code that creates a graph, based on the information that was accepted through a check box. But any account you do not select appears as a "blank" legend entry Is there a macro code to delete "blank" legend entries?

enter image description here


Solution

  • Below the table create another table. Then link the checkboxes to the table below the first table. Then after True/False enter =IF($B10=TRUE,C3,"")

    After that replace AddPlot ActiveSheet.Range("C9:C13,D9:O13") with the range of the bottom table.

    enter image description here