Quiero agregar algunas opciones rápidas a los gráficos en una hoja de cálculo, quiero seleccionar un gráfico, seleccionar/deseleccionar una casilla de verificación y el código se ejecuta.
Los códigos de casilla de verificación son;
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
DisplayLabels
Else
HideLabels
End If
End Sub
El código DisplayLabels y HideLabels es
Sub DisplayLabels()
With ActiveChart
ActiveChart.FullSeriesCollection(1).Select
ActiveChart.FullSeriesCollection(1).ApplyDataLabels
End With
End Sub
Sub HideLabels()
With ActiveChart
ActiveSheet.ChartObjects("Basic_Chart").Activate
ActiveChart.FullSeriesCollection(1).Select
ActiveChart.FullSeriesCollection(1).DataLabels.Select
Selection.ShowValue = False
End With
End Sub
El problema es que una vez que marco la casilla, el gráfico ya no está seleccionado/activo y, por lo tanto, el código no puede funcionar. Hay alguna forma de evitar esto. Quiero que este sea un código reutilizable en varias hojas, por lo que no puedo hacer referencia directamente a un gráfico.
Respuesta1
Olvídese de la casilla de verificación, haga que la macro determine si las etiquetas están o no. Lo siguiente comprobará si hay un gráfico seleccionado y, en caso afirmativo, alternará las etiquetas:
If ActiveChart Is Nothing Then
MsgBox "You must select a chart"
Exit Sub
End If
For Each mySeries In ActiveChart.SeriesCollection
On Error Resume Next
mySeries.DataLabels.Select
If (Err.Number = 0) Then
mySeries.DataLabels.Delete
Else
mySeries.ApplyDataLabels
End If
On Error GoTo 0
Next mySeries