I realise you can set the axis of a graph in VBA using
.MaximumScale =
.MinimumScale =
Is there a way to get the axis value?
I ask this because it would make it easier to automate a process that gets the axis of a chart, then adds a month to it (without setting the graph axis to automatic).
P.S.
I recorded a macro of changing the axis dates and it set the date values as a number like 40148 or 41609. What is this?
Try to step through the following snippet of code. It shows how to find Y axis value and how to change it. See some comments inside.
1st attempt for chart embedded in worksheet
Sub test_chart()
'get the chart for activesheet
Dim myCHR As Chart
Set myCHR = ActiveSheet.ChartObjects(1).Chart
'get Y axis of the chart
Dim myYA As Axis
Set myYA = myCHR.Axes(XlAxisType.xlValue)
'get the value
Debug.Print myYA.MaximumScale
Debug.Print myYA.MinimumScale
'the same in almost one line
With ActiveSheet.ChartObjects(1).Chart.Axes(xlValue)
Debug.Print .MaximumScale
Debug.Print .MinimumScale
'change the value
.MaximumScale = 10
End With
End Sub
2nd attempt for chart being separate sheet
Sub test_sheet_chart()
'get the chart for activesheet
Dim myCHR As Chart
Set myCHR = Sheets("All SIN 10 Pubs - Unique Users")
'get Y axis of the chart
Dim myYA As Axis
Set myYA = myCHR.Axes(XlAxisType.xlValue)
'get the value
Debug.Print myYA.MaximumScale
Debug.Print myYA.MinimumScale
'the same in almost one line
With Sheets("All SIN 10 Pubs - Unique Users").Axes(xlValue)
Debug.Print .MaximumScale
Debug.Print .MinimumScale
'change the value
.MaximumScale = 10
End With
End Sub
Related
I have two drop-down menus that list a range of dates. One date identifies the beginning of the range and the other the end. I also created two named ranges as follows:
chart_Dates=INDEX(Dates,MATCH(Home!$A$7,Dates,0)):INDEX(Dates,MATCH(Home!$A$9,Dates,0))
chart_Frequency=INDEX(Frequency,MATCH(Home!$A$7,Dates,0)):INDEX(Frequency,MATCH(Home!$A$9,Dates,0))
Dates, and Frequency from those formulas are named ranges with set boundaries. If I manually enter the y-values as "Home!chart_Frequency" and the horizontal values as Home!chart_Dates the macro works just fine. I can adjust the start and end dates using the drop-down menus and the graph updates accordingly.
However, the macro recorder doesn't translate those action into code. I've attempted the following code without success (there is no error generated but the graph only references the first cell in my range).
ActiveChart.SeriesCollection(1).Values = Range("chart_Frequency")
ActiveChart.SeriesCollection(1).XValues = Range("chart_Dates")
How do I get the code to replicate what I can do manually? Any advice is greatly appreciated!
Try this:
Sub testing()
Dim freqRange As String
Dim datesRange As String
Dim Name As Variant
For Each Name In ThisWorkbook.Names
If Name.Name = "chart_Frequency" Then
freqRange = Name.Value
ElseIf Name.Name = "chart_Dates" Then
datesRange = Name.Value
End If
Next Name
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Name = "=Sheet1!$B$1"
ActiveChart.FullSeriesCollection(1).Values = datesRange
ActiveChart.FullSeriesCollection(1).XValues = freqRange
End Sub
My chart didnt have any series so it adds a new series and it updates the Name, Values and XValues with no issues.
Here is the code I have right now. After it runs when I check the y-values and horizontal values on the graph itself and they are both blank.
'Create chart
Dim rng As Range
Dim cht As ChartObject
'Data range for the chart
Set rng = ActiveSheet.Range("$E$7:$F$501")
'Designates which cell to place the top left corner of the graph in
ActiveSheet.Range("N5").Select
'Create a chart
Set cht = ActiveSheet.ChartObjects.Add( _
Left:=ActiveCell.Left, _
Width:=360, _
Top:=ActiveCell.Top, _
Height:=216)
'Give chart some data
'I was using the line below to set the range but comment blocked it out and entered the suggested code
' cht.Chart.SetSourceData Source:=rng
Dim freqRange As String
Dim datesRange As String
Dim Name As Variant
For Each Name In ThisWorkbook.Names
If Name.Name = "chart_Frequency" Then
freqRange = Name.Value
ElseIf Name.Name = "chart_Dates" Then
datesRange = Name.Value
End If
Next Name
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Name = "=Home!$A$5"
ActiveChart.FullSeriesCollection(1).Values = datesRange
ActiveChart.FullSeriesCollection(1).XValues = freqRange
'Determine the chart type
cht.Chart.ChartType = xlLineMarkersStacked
'Remove Major Gridlines
cht.Chart.Axes(xlValue).MajorGridlines.Delete
'Sets the legend to the bottom of the graph
cht.Chart.SetElement (msoElementLegendBottom)
cht.Name = "Frequency_Chart"
'Add title and reference A5 for name
ActiveSheet.ChartObjects("Frequency_Chart").Activate
ActiveChart.SetElement (msoElementChartTitleAboveChart)
Application.CutCopyMode = False
Selection.Caption = "=Home!R5C1"
I can't seem to get this code to work. I am getting a run-time error 13 'type mismatch'. As the code suggests I would like to set X and Y axis values from the ranges in Z6:Z9.
Sub Axis()
Dim Cht As ChartObject
Set Cht = Worksheets("Data Input & Summary").ChartObjects("Chart 2").Chart
With Cht.Axes(xlCategory)
.MaximumScale = .Range("Z7").Value
.MinimumScale = .Range("Z6").Value
End With
With Cht.Axes(xlValue)
.MaximumScale = .Range("Z9").Value
.MinimumScale = .Range("Z8").Value
End With
End Sub
In general, you have about 3 errors in your code.
This is how it should look like:
Sub AxisSomething()
Dim Cht As ChartObject
Set Cht = Worksheets(1).ChartObjects(1)
With Cht.Chart.Axes(xlCategory)
.MaximumScale = Worksheets(1).Range("Z7").Value
.MinimumScale = Worksheets(1).Range("Z6").Value
End With
With Cht.Chart.Axes(xlValue)
.MaximumScale = Worksheets(1).Range("Z9").Value
.MinimumScale = Worksheets(1).Range("Z8").Value
End With
End Sub
You declare Cht as a ChartObject, but you set it to chart.
If you declare it as a ChartObject, to get the .Axes you should refer to the .Chart of the Chart Object.
Whenever you use ., it refers to the with above. And the .Range should not refer to the Chart.Axes(xlCategory)
Last but not least - when you upload code to StackOverflow, it is a good idea to make it "reproductable". Thus, instad of Worksheets("Data Input & Summary") write Worksheets(1). And then fix the name yourself.
By the following code Max. and Min. scale for primary horizontal X-axis of a chart in vba is being set;
Sub chart_set()
Dim objCht As ChartObject
With ActiveSheet.ChartObjects(1).Chart.Axes(xlValue)
.MinimumScale = 0
.MaximumScale = my_max_var
End With
End Sub
is there any way to set the secondary one's as well?
First, the code below will Set objCht to the ChartObject in ActiveSheet (see here recommendation how to stay away from ActiveSheet How to avoid using Select in Excel VBA macros)
The code below will check if a secondary axis is applied, if not it will add it to the chart. Afterwards, it will modify the MinimumScale and MaximumScale to 20 and 50 (just random numbers I've selected).
Code
Option Explicit
Sub chart_set()
Dim objCht As ChartObject
Dim my_max_var As Long
' just for my tests
my_max_var = 1000
' set chart object
Set objCht = ActiveSheet.ChartObjects(1)
With objCht
With .Chart.Axes(xlValue, xlPrimary)
.MinimumScale = 0
.MaximumScale = my_max_var
End With
' check if Secondary Axis is applied
If .Chart.HasAxis(xlValue, xlSecondary) = False Then
.Chart.HasAxis(xlValue, xlSecondary) = True
End If
' modify minimum and maximum values of seconday axis
With .Chart.Axes(xlValue, xlSecondary)
.MinimumScale = 20
.CrossesAt = 20
.MaximumScale = 50
End With
End With
End Sub
I have following code to copy all charts on one sheet into another.
Problem is that in spite of the fact that my charts have numeric(timeseries) data for horizontal axis Excel treats it as categorical. I can tell that is does that because it tries to display every single date on the axis( in spite of my code swtiching it to once a month), and once select "Format Axis" I only see fields for Categorical data( I am missing fields like Maximim, Major Unit etc.).
This is the code I am using, to be hones I copeid most of it of some forum but I cannot recall where if I do find it again I'll post a link:
Sub CopyCharts(Source As Worksheet, Target As Worksheet)
Dim SChart As ChartObject
Dim NChart As ChartObject
Dim NewSheetNChart As ChartObjects
Dim ChartTop As Single
Dim ChartLeft As Single
Dim ChartIndex As Integer
Dim ChartSeries As Series
Dim XAxis As Axis
For Each SChart In Source.ChartObjects
'Save chart's current position
ChartTop = SChart.Top
ChartLeft = SChart.Left
'Copy and paste the chart
SChart.Copy
Target.Paste
'Find the chart in a new spreadsheet
ChartIndex = ChartIndex + 1
Set NewSheetNChart = Target.ChartObjects
Set NChart = NewSheetNChart(ChartIndex)
'Align the charts
NChart.Top = ChartTop
NChart.Left = ChartLeft
'Delink sereis from source
For Each ChartSeries In NChart.Chart.SeriesCollection
ChartSeries.XValues = ChartSeries.XValues
ChartSeries.Values = ChartSeries.Values
ChartSeries.Name = ChartSeries.Name
Next ChartSeries
'Set the X axis to correct values
Set XAxis = NChart.Chart.Axes(xlCategory, xlPrimary)
With XAxis
.CategoryType = xlTimeScale
.BaseUnit = xlMonths
.MajorUnit = 1
End With
Next SChart
End Sub
I have a code that modifies a chart layout, using a chart number that has to be modified as user input value. As far i have a lot of charts (about 50), i would like to imput not just 1 value per time, but to say to modify charts for example from 2 to 10. Can some one help with it, please?
Sub Grafici()
Dim ws As Worksheet
Dim sUserInput As String
Set ws = ActiveWorkbook.Sheets("Sheet1")
ws.Select
sUserInput = InputBox("Enter Number:", "Collect User Input")
For i = 1 To 7
On Error Resume Next
ActiveSheet.ChartObjects("Chart " & sUserInput).Activate
ActiveChart.SeriesCollection(i).Points(12).MarkerSize = 19
ActiveChart.SetElement (msoElementDataLabelCenter)
ActiveChart.SeriesCollection(i).Points(11).DataLabel.Delete
ActiveChart.SeriesCollection(i).Points(11).MarkerSize = 5
Next
End Sub
Thanks!