Creating a specific chart - vba

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

Related

Setting X and Y axis for both limits

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.

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 ; )

excel VBA Chart Formatting - need to change the width of a line

I am trying to write code that will format a chart (myChart). Series line 2 and 4 are the 2 that I need to format so their weight = 1. I have tried following with no luck.
myChart.fullseriescollection(2).format.line.weigth = 1
I am pretty new to VBA, and I have never tried to reference chart object before, so I am unfamiliar with the proper syntax.
Thanks! See my code below.
Sub myChart()
Dim myChart As Chart, cht As ChartObject
Dim rngChart As Range, destSht As String
Dim rngData As Range
destSht = ActiveSheet.Name
Set myChart = Charts.Add
Set myChart = myChart.Location(where:=xlLocationAsObject, Name:=destSht)
With ActiveSheet.ListObjects("Table1").DataBodyRange
Set rngData = Union(.Columns(2), .Columns(9), .Columns(10), .Columns(11), .Columns(12))
End With
With myChart
.SetSourceData Source:=rngData, PlotBy:=xlColumns
.ChartType = xlColumnClustered
.ChartStyle = 209
*****Below is the first series line that I want to format*****
With .FullSeriesCollection(2)
.ChartType = xlLine
.AxisGroup = 1
End With
With .FullSeriesCollection(3)
.ChartType = xlLineMarkers
.AxisGroup = 2
End With
*****2nd line I want to format*****
With .FullSeriesCollection(4)
.ChartType = xlLine
.AxisGroup = 2
End With
End With
ActiveSheet.ChartObjects(1).Activate
Set cht = ActiveChart.Parent
Set rngChart = Range("A1100:K1115")
cht.Left = rngChart.Left
cht.Top = rngChart.Top
cht.Width = rngChart.Width
cht.Height = rngChart.Height
Range("A2").Select
End Sub
With .FullSeriesCollection(2)
.ChartType = xlLine
.AxisGroup = 1
.Format.Line.Weight = 5
End With
Worked fine for me in Office 2016.
I would have commented, but I do not have a high enough reputation yet

Error at creating simple x y smooth chart

I am trying to create a dynamic chart that will change the data set with time, but I am stuck at the first step which is creating a simple chart with a certain limited number of points.
My x values and Y values are at rows 3 and 5.
The code I am trying to run is to create the chart in the same worksheet I am using:
Sub UpdateChart()
Dim ChtObj As ChartObject
Set ChtObj = ActiveSheet.ChartObjects("Bending Moment along " & ActiveSheet.Name) 'Adjust chart name to your chart
With ChtObj.Chart
.ChartType = x1XYScatterSmooth
.SetElement msoElementLegendNone
.Axes(xlValue).MinimumScale = 0
.Axes(xlValue).MaximumScale = 5
.SeriesCollection.NewSeries
.SeriesCollection(1).Name = "Bending moment"
.SeriesCollection(1).Values = Range("D3:H3")
.SeriesCollection(1).XValues = Range("D5:H5")
End With
End Sub
I am getting error run-time 5, invalid procedure call or argument? This happens at the line of: With ChtObj.Chart
You have a typo in the first line of the With statement. Hard to see, but there is a 1 instead of an l.It should be
.ChartType = xlXYScatterSmooth

Excel failling to recognize type of chart

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