Hi all! This is a question I've asked on another forum but I'm taking the liberty of asking it here aswell since I don't seem to be getting any reply.
I'd like to easily generate a XY scatter chart. Ideally the parameters of the chart would be referenced to cells so that they can easily be changed everytime a chart is generated.
The reason for this is that I'd like this file to serve as a template where I could copy/paste a big range of data and select the series I want (or do not want) charted.
I'm very new to VBA so I don't really know where to start but I found online a macro that'll create a chart (that's a beginning..!^^)
It creates a chart from a selected range though so that's not what I'm looking for but it could be a starting point..
I also found a piece of code to work with the axis:
So I guess I could play with the E2-E3-F2-F3 cells and replace them with my references (B4-B5-B6-B7).. Maybe? But it seems this macro is designed to be ran after the chart has been generated.
Also found this for the title:
But same remark and I have no idea what the "R1C3" could refer to
Thanks in advance for the help
Have a great day,
Simon
I'd like to easily generate a XY scatter chart. Ideally the parameters of the chart would be referenced to cells so that they can easily be changed everytime a chart is generated.
The reason for this is that I'd like this file to serve as a template where I could copy/paste a big range of data and select the series I want (or do not want) charted.
I'm very new to VBA so I don't really know where to start but I found online a macro that'll create a chart (that's a beginning..!^^)
It creates a chart from a selected range though so that's not what I'm looking for but it could be a starting point..
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
With ActiveSheet.ChartObjects("Chart 1").Chart ' Category (X) Axis
With .Axes(xlCategory)
If ActiveSheet.Range("$E$2").Value > .MinimumScale Then
.MaximumScale = ActiveSheet.Range("$E$2").Value
.MinimumScale = ActiveSheet.Range("$E$3").Value
Else
.MinimumScale = ActiveSheet.Range("$E$3").Value
.MaximumScale = ActiveSheet.Range("$E$2").Value
End If
.MajorUnit = ActiveSheet.Range("$E$4").Value
End With ' Value (Y) Axis
With .Axes(xlValue)
If ActiveSheet.Range("$F$2").Value > .MinimumScale Then
.MaximumScale = ActiveSheet.Range("$F$2").Value
.MinimumScale = ActiveSheet.Range("$F$3").Value
Else
.MinimumScale = ActiveSheet.Range("$F$3").Value
.MaximumScale = ActiveSheet.Range("$F$2").Value
End If
.MajorUnit = ActiveSheet.Range("$F$4").Value
End With
End With
End Sub
I also found a piece of code to work with the axis:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
With ActiveSheet.ChartObjects("Chart 1").Chart ' Category (X) Axis
With .Axes(xlCategory)
If ActiveSheet.Range("$E$2").Value > .MinimumScale Then
.MaximumScale = ActiveSheet.Range("$E$2").Value
.MinimumScale = ActiveSheet.Range("$E$3").Value
Else
.MinimumScale = ActiveSheet.Range("$E$3").Value
.MaximumScale = ActiveSheet.Range("$E$2").Value
End If
.MajorUnit = ActiveSheet.Range("$E$4").Value
End With ' Value (Y) Axis
With .Axes(xlValue)
If ActiveSheet.Range("$F$2").Value > .MinimumScale Then
.MaximumScale = ActiveSheet.Range("$F$2").Value
.MinimumScale = ActiveSheet.Range("$F$3").Value
Else
.MinimumScale = ActiveSheet.Range("$F$3").Value
.MaximumScale = ActiveSheet.Range("$F$2").Value
End If
.MajorUnit = ActiveSheet.Range("$F$4").Value
End With
End With
End Sub
So I guess I could play with the E2-E3-F2-F3 cells and replace them with my references (B4-B5-B6-B7).. Maybe? But it seems this macro is designed to be ran after the chart has been generated.
Also found this for the title:
Code:
With Sheet1.ChartObjects(1).Chart
.HasTitle = True
.ChartTitle.Text ="=Sheet1!R1C3"
End With
But same remark and I have no idea what the "R1C3" could refer to
Thanks in advance for the help
Have a great day,
Simon
Attachments
Last edited by a moderator: