Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88111

Deselecting a chart in Excel VBA

$
0
0

In reference to the following file.xlsm:

first image

where Sheet1 (1) is a Sheet, Plot1 (2) is a Graphic-Sheet and the button (3) refers to the following Macro:

Sub plot()

ReDim blue(1 To 5, 1 To 2)
ReDim red(1 To 5, 1 To 2)
For i = 1 To 5
    blue(i, 1) = i
    blue(i, 2) = i ^ 2
    red(i, 1) = i + 4
    red(i, 2) = (i + 4) ^ 2
Next i

Sheets("Plot1").Select
ActiveChart.ChartArea.ClearContents
ActiveChart.ChartArea.Select
ActiveChart.ChartType = xlXYScatterSmoothNoMarkers

ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).XValues = Application.Index(blue, , 1)
ActiveChart.FullSeriesCollection(1).Values = Application.Index(blue, , 2)
ActiveChart.FullSeriesCollection(1).Select
Selection.Format.Line.ForeColor.RGB = RGB(0, 0, 255)

ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(2).XValues = Application.Index(red, , 1)
ActiveChart.FullSeriesCollection(2).Values = Application.Index(red, , 2)
ActiveChart.FullSeriesCollection(2).Select
Selection.Format.Line.ForeColor.RGB = RGB(255, 0, 0)

Sheets("Plot1").Protect DrawingObjects:=True, Contents:=False
Sheets("Plot1").Unprotect
Sheets("Sheet1").Select

End Sub
  • after opening file.xlsm, clicking (3) and (2) I get:

second image

  • after opening file.xlsm, clicking (2), (1), (3) and (2) I get:

third image

How can I change the Macro so that I always get the third image regardless of the click sequence?


Viewing all articles
Browse latest Browse all 88111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>