Setting X and Y axis for both limits - vba

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.

Related

Maximum scale of secondary horizontal X-axis VBA

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

mulit plot from different sheets in one chart with VBA

My problem is that I want to make many graph in one chart but the data is from different sheets.
At the moment my code can only take multi data from one sheet, meaning I can plot 2 graph from one sheet.
My code at the moment is:
Sub ChartSheet()
Dim ChartSheet1 As Chart
Set ChartSheet1 = Charts.Add
With ChartSheet1
.SetSourceData Source:=Sheets("Sheet1").Range("E12:E6232, Y12:Y6232")
.ChartType = xlLine
.HasTitle = True
.ChartTitle.Characters.Text = "Test Chart"
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "x"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "y"
End With
End Sub
What I want is to say:
.SetSourceData Source:=Sheets("Sheet1").Range("E12:E6232, Y12:Y6232")
.SetSourceData Source:=Sheets("Sheet2").Range("D12:E23")
.SetSourceData Source:=Sheets("Sheet3").Range("Y12:Y6232, G27:G496, H3:5977")
and so on..
But when I do this my code it only print the last line from .SetSoureData
Hope some of you can help me work around this, Many thank in advarnce :)
Update:
I found abit of a work around by looping but this is not my total answer
But here is my other code:
Sub MultiSheetPlot()
Dim cht As Chart, s As Series, xRng As Range
Dim i As Long, chartName As String
Set cht = Charts.Add
cht.ChartType = xlLine
For i = 1 To 3
chartName = "Sheet" & i
Set xRng = Sheets("Sheet1").Range("A1:A20, C1:C20")
With cht.SeriesCollection.NewSeries()
.Values = xRng
.Name = chartName
End With
Next i
End Sub
the problem in this code is that it ignores the last range I define like the C10:C20
Responding to your latest update, I have a Sub that creates a series each time it's called from MultiSheetPlot Sub.
You can actually add more parameters to this Sub (as long as you remeber to pass them in the Calling).
Option Explicit
Dim cht As Chart
Sub MultiSheetPlot()
Set cht = Charts.Add
cht.ChartType = xlLine
' call the series creation chart function (each time for each series you want to add to the existing chart
Call Create_SeriesChart("Sheet1", Sheets("Sheet1").Range("E12:E6232"), 1, True, msoThemeColorText1)
Call Create_SeriesChart("Sheet1", Sheets("Sheet1").Range("Y12:Y6232"), 1, True, msoThemeColorText1)
End Sub
' ------ this Sub creates a series to the chart, it receives the following parameters: ------
' 1. seriesName - String
' 2. serValues - Range
' 3. lineWeight - Double (the weight of the line)
' 4. lineVis - Boolean (if you want to hide a certail series)
' 5. lineColor - MsoColorType (using the current's PC Theme colors
Sub Create_SeriesChart(seriesName As String, serValues As Range, lineWeight As Double, lineVis As Boolean, lineColor As MsoColorType)
Dim Ser As Series
Set Ser = cht.SeriesCollection.NewSeries
With Ser
.Name = seriesName
.Values = serValues
.Format.Line.Weight = lineWeight
If lineVis = True Then
.Format.Line.Visible = msoTrue
Else
.Format.Line.Visible = msoFalse
End If
.Format.Line.ForeColor.ObjectThemeColor = lineColor ' Line color Black
End With
End Sub
My solution is use Name of plage, you can give a name of your range
Like in my photo :
Everytime when you want to use these datas you just need call them
Like this plage i gived them name Poste_EtatDeLaDemande
When i want to use these elements i just call it like Range("Poste_EtatDeLaDemande") Excel will find it you don't need tell them where it is anymore ; )

Creating a specific chart

I would like to create a chart like this: http://take.ms/BEie1.
The first two columns are the name and serial number.
I have to put these two on the X axis as in the images, and the Y axis is for the percentage usage.
Here is my latest attempt:
Dim xRange As Range
Dim yRange As Range
Dim objCht As ChartObject
Set objCht = detailsh.ChartObjects.Add(Left:=detailsh.Columns("A").Left, Width:=350, Top:=detailsh.Rows(9).Top, Height:=210)
Set xRange = Range("I2:J9")
Set xRange = Range("L2:L9")
With objCht.Chart
Set srs = .SeriesCollection.NewSeries
With srs
.name = detailsh.Range("B1")
.Values = detailsh.Range("I2:I7")
.XValues = detailsh.Range("L2:L7")
End With
End With
I can solve it finally :)
Here is my code, maybe it can help for anyone else.
Dim sourcech As Chart
Set sourcech = Charts.Add
Set sourcech = sourcech.Location(Where:=xlLocationAsObject, name:="Details")
With sourcech
.SetSourceData Source:=Sheets("Details").Range("l2:L9")
.SeriesCollection(1).XValues = Sheets("Details").Range("I2:J9") 'here you can modify the X axes
End With

Get Chart Axis Value

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

Update line graph based on table

I have a table with Max Min for each line graph. I want to build a macro that will update each line graph (40 in total) off of a table. Please Advise.
Here is what I have so far. I'm sure its poorly written as I'm starting to learn how to write VBA.
Sub Update_Slope()
With Chart(2).Axes(xlValue, xlPrimary)
.MaximumScale = ActiveSheet.Range("F58").Value
' Constant value
.MinimumScale = ActiveSheet.Range("F68").Value
' Constant Value
With Chart(4).Axes(xlValue, xlPrimary)
.MaximumScale = ActiveSheet.Range("F59").Value
' Constant value
.MinimumScale = ActiveSheet.Range("F69").Value
' Constant Value
End With
End Sub
UNTESTED
Sub Update_Slope()
ActiveSheet.ChartObjects("Chart(2)").Activate
ActiveChart.Axes(xlCategory).MinimumScale = Range("F68").Value
ActiveChart.Axes(xlCategory).MaximumScale = Range("F58").Value
ActiveSheet.ChartObjects("Chart(4)").Activate
ActiveChart.Axes(xlCategory).MinimumScale = Range("F69").Value
ActiveChart.Axes(xlCategory).MaximumScale = Range("F59").Value
End Sub
Sub Update_All_Slopes()
Dim co, sht as WorkSheet
Set sht = Activesheet
Set co = sht.ChartObjects
'ideally you're looping through your table range to do this, instead
' of hard-coding each line. Need more info on how your sheet is set up
' and how to identify which chart should be updated...
FixYAxis co("Chart2").Chart,sht.Range("F68").Value, sht.Range("F58").Value
FixYAxis co("Chart4").Chart,sht.Range("F69").Value, sht.Range("F59").Value
'...etc
End Sub
Sub FixYAxis(cht as Chart, minVal,maxVal)
With cht.Axes(xlValue, xlPrimary)
.MaximumScale = maxVal
.MinimumScale = minVal
End With
End Sub