Hi all. I would like two change the axis scale of a chart by writing the values into a cell. How could I manage with that? Should I use VBA? I don't have any experience on that but I could try with your help.
Thank you.
Hi all. I would like two change the axis scale of a chart by writing the values into a cell. How could I manage with that? Should I use VBA? I don't have any experience on that but I could try with your help.
Thank you.
VBA is the only way as you can not directly link scale values to cells.
Something like this will do it for the Value axis
![]()
With ActiveChart.Axes(xlValue) .MinimumScale = Range("A1") .MaximumScale = Range("A2") End With
Thx Andy. I'm trying to use this code for the file I sent you a couple of days ago. I haven't used before this VBA and I'm trying to find out the way it works. I do the following steps:
1) Open the visual basic editor.
2) Insert new module
3) Copy and paste this code there:
With ActiveChart.Axes(xlValue)
.MinimumScale = Range("A1")
.MaximumScale = Range("A2")
End With
4) write in the corresponding values so, A1=1000 A2=2000
is that all? Does the chart change automatically??any other modifications?
I would appreciate also if you could tell me where to find a link with some information about VBA for beginners.
Thx
There is a little more to it than that.
You would need to wrap the code in a procedure.
Then select the chart and run the macro
![]()
Sub ChangeAxis() With ActiveChart.Axes(xlValue) .MinimumScale = Range("A1") .MaximumScale = Range("A2") End With End sub
Or you could add code to the worksheet change event so the code is run when the cells containing the information are altered.
See attached.
Included in this is a change to the procedure so the chart to be altered is passed as an argument rather than relying on the active chart object
Is there any change when having the chart in different sheet?
Yes. The routine would need to know which sheet to check.
So I have added the min and max values to the argument list.
And the sheet level code can pass the relevant information![]()
Sub AxisChange(Cht As Chart, MinValue, MaxValue) With Cht.Axes(xlValue) .MinimumScale = MinValue .MaximumScale = MaxValue End With End Sub
![]()
Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Range("A1:A2"), Target) Is Nothing Then AxisChange ActiveSheet.ChartObjects(1).Chart, Range("A1").Value, Range("A2").Value End If End Sub
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks