draw more than one plot using vba - excel

I am trying to draw more than one plot using vba in excel I wrote this code which enable me to draw one drawing
Sub trial()
ActiveSheet.Shapes.AddChart2(240, xlXYScatterSmoothNoMarkers).Select
ActiveChart.SeriesCollection.NewSeries
ActiveChart.HasLegend = True
ActiveChart.FullSeriesCollection(1).Name = "=""HBES"""
ActiveChart.FullSeriesCollection(1).XValues = "=EN!$G$253:$G$257"
ActiveChart.FullSeriesCollection(1).Values = "=EN!$dp$253:$dp$257"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(2).Name = "=""NHBES"""
ActiveChart.FullSeriesCollection(2).XValues = "=EN!$G$253:$G$257"
ActiveChart.FullSeriesCollection(2).Values = "='EN1'!$do$253:$do$257"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(3).Name = "=""NHBCS"""
ActiveChart.FullSeriesCollection(3).XValues = "=EN!$G$253:$G$257"
ActiveChart.FullSeriesCollection(3).Values = "=EN1c!$do$253:$do$257"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(4).Name = "=""HBCS"""
ActiveChart.FullSeriesCollection(4).XValues = "=EN!$G$253:$G$257"
ActiveChart.FullSeriesCollection(4).Values = "=ENC!$dp$253:$dp$257"
With ActiveChart
'chart name
.HasTitle = True
.ChartTitle.Characters.Text = "Expected Number of goods for Group Twenty in Condition State Five"
'X axis name
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time (Years)"
'y-axis name
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Expected Number of goods"
End With
End Sub
What I need to do is to make the data range variable I tried to adjust the above code in the following manner
Sub trial()
ActiveSheet.Shapes.AddChart2(240, xlXYScatterSmoothNoMarkers).Select
ActiveChart.SeriesCollection.NewSeries
ActiveChart.HasLegend = True
ActiveChart.FullSeriesCollection(1).Name = "=""HBES"""
ActiveChart.FullSeriesCollection(1).XValues = "=EN!$G$253:$G$257"
ActiveChart.FullSeriesCollection(1).Values = Sheets("Sheets12").Range(Cells(253,32),Cells(302,32))
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(2).Name = "=""NHBES"""
ActiveChart.FullSeriesCollection(2).XValues = "=EN!$G$253:$G$257"
ActiveChart.FullSeriesCollection(2).Values = Sheets("Sheets13").Range(Cells(253,32),Cells(302,32))
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(3).Name = "=""NHBCS"""
ActiveChart.FullSeriesCollection(3).XValues = "=EN!$G$253:$G$257"
ActiveChart.FullSeriesCollection(3).Values = Sheets("Sheets14").Range(Cells(253,32),Cells(302,32))
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(4).Name = "=""HBCS"""
ActiveChart.FullSeriesCollection(4).XValues = "=EN!$G$253:$G$257"
ActiveChart.FullSeriesCollection(4).Values = Sheets("Sheets15").Range(Cells(253,32),Cells(302,32))
With ActiveChart
'chart name
.HasTitle = True
.ChartTitle.Characters.Text = "Expected Number of goods for Group Twenty in Condition State Five"
'X axis name
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time (Years)"
'y-axis name
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Expected Number of goods"
End With
End Sub
but it doesn't work Any suggestion please?
Thanks in advance

Looking at the first series you're trying to add. In the first snippet you're trying to pull the data from the Range DP253:DP257, the columnindex for column DP is 120, not 32 Cells(253, 32) refers to cell AF253 and Cells(302, 32) refers to cell AF302.
For the plot named "HBES"
.Values = Sheets("EN").Range(Cells(253, 120), Cells(257, 120))
Edit
For the plot named "NHBES"
.Values = Sheets("'EN1'").Range(Cells(253, 119), Cells(257, 119))
For the plot named "NHBCS"
.Values = Sheets("EN1c").Range(Cells(253, 119), Cells(257, 119))
For the plot named "HBCS"
.Values = Sheets("ENC").Range(Cells(253, 120), Cells(257, 120))
The code is built up as follows
Sheets("NAME OF THE SHEET AS IT APPEARS ON THE TAB IN YOUR WORKBOOK").Range(Cells(ROWNUMBER of the top (left) cell in the range, COLUMNNumber of the top (left) cell in the range), Cells(ROWNUMBER of the bottom (right) cell in the range, COLUMNUMBER of the bottom right cell in the range))
If you are referring to range A1:B5, you'd type Range(Cells(1, 1), Cells(5, 2)

Related

Generating second axis in a chart vba

I wanted to create a second axis in my chart.
There is no error when i run ".SeriesCollection(1).AxisGroup=2" but it does not create the second axis.
I want to plot temperature at the LHS of the y-axis and power at RHS of the y-axis.
The x-axis is time.
Outcome: I want to see 2 graphs. A graph generated by Col H and Col F and another will be generated by Col R and Col F.
Sub plotGraph()
Dim LastRowOfH As Long, LastRowOfF As Long, LastRowOfR As Long
Dim ColumnHRngData As Range, ColumnFRngData As Range, ColumnRRngData As Range
Dim xAxes As Axis, yAxes As Axis, y2Axes As Axis
Dim srsNew As Series
LastRowOfH = ActiveSheet.Cells(Rows.Count, 8).End(xlUp).Row
LastRowOfF = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
LastRowOfR = ActiveSheet.Cells(Rows.Count, 18).End(xlUp).Row
Set ColumnHRngData = ActiveSheet.Range("H31:H" & LastRowOfH)
Set ColumnFRngData = ActiveSheet.Range("F31:F" & LastRowOfF)
Set ColumnRRngData = ActiveSheet.Range("R31:R" & LastRowOfR)
Dim cht As Shape
Set cht = ActiveSheet.Shapes.AddChart(Left:=Range("D1:S31").Left, Top:=Range("D1:S31").Top, Width:=Range("D1:S31").Width, Height:=Range("D1:S31").Height)
With cht.Chart
.HasTitle = True
.ChartTitle.Text = "Temperature VS Time"
.ChartType = xlXYScatterLinesNoMarkers
Do Until .SeriesCollection.Count = 0
.SeriesCollection(1).Delete
Loop
Set srsNew = .SeriesCollection.NewSeries
.SeriesCollection(1).Name = "Power/W"
.SeriesCollection(1).Format.Line.ForeColor.RGB = RGB(0, 0, 0) 'change the color of the chart line
.SeriesCollection(1).AxisGroup = 2
With srsNew
.XValues = ColumnFRngData 'xaxis of the graph refers to time in hr
.Values = ColumnHRngData
End With
With srsNew
.XValues = ColumnFRngData
.Values = ColumnRRngData
End With
Set xAxes = .Axes(xlCategory, xlPrimary)
With xAxes
.HasTitle = True
.AxisTitle.Text = "TIME (Hr)"
End With
Set yAxes = .Axes(xlValue, xlPrimary)
With yAxes
.HasTitle = True
.AxisTitle.Text = "Temp (DegCel)"
End With
Set y2Axes = .Axes(xlValue, xlSecondary)
With y2Axes
.HasTitle = True
.AxisTitle.Text = "Power (W)"
End With
End With
You're only plotting one series.
Try this
With cht.Chart
.HasTitle = True
.ChartTitle.Text = "Temperature VS Time"
.ChartType = xlXYScatterLinesNoMarkers
Do While .SeriesCollection.Count > 0
.SeriesCollection(1).Delete 'remove any existing series
Loop
With .SeriesCollection.NewSeries
.Name = "Power/W"
.Format.Line.ForeColor.RGB = RGB(0, 0, 0) 'change the color of the chart line
.AxisGroup = 2
.XValues = ColumnFRngData 'xaxis of the graph refers to time in hr
.Values = ColumnHRngData
End With
With .SeriesCollection.NewSeries
.Name = "Temperature"
.Format.Line.ForeColor.RGB = RGB(255, 0, 0) 'change the color of the chart line
.AxisGroup = 1
.XValues = ColumnFRngData 'xaxis of the graph refers to time in hr
.Values = ColumnRRngData
End With
With .Axes(xlCategory, xlPrimary)
.HasTitle = True
.AxisTitle.Text = "TIME (Hr)"
End With
With .Axes(xlValue, xlPrimary)
.HasTitle = True
.AxisTitle.Text = "Temp (DegCel)"
End With
With .Axes(xlValue, xlSecondary)
.HasTitle = True
.AxisTitle.Text = "Power (W)"
End With
End With

Chart legend entry width

How do I get the correct width of each legend entry in a chart? I have used the width property of the LegendEntry but this doesn't give the correct value.
For example, using the below legend:
if I check each legend entry's width using LegendEntry.Width I get the same width for each entry
A = 67, word = 67, Longer sentence = 67,
Which is obviously incorrect, it's probably assigned the longest width to all entries. So how do i get the actual width of each entry?
I know that the chart's legend entry is automatically resized according to the number of letters.
So it is an anomaly, but it seems to be necessary to adjust the number of characters to be blank. It is not an exact match, but space * 2 seems similar.
Sub setCharts(Target As Range, Cht As Chart)
Dim Srs As Series
Dim vColor, vName
Dim i As Integer, Ln As Integer, k As Integer
vColor = Array(RGB(246, 246, 246), RGB(255, 224, 140), RGB(47, 157, 39), RGB(0, 0, 0))
vName = Array("A", "Word", "Longer sentence", "stack")
Ln = Len(vName(2)) '<~~~ "Longer sentence" 's length --> Collection name
'****** The Loop statement below makes the series names the same length ******
' For i = 0 To UBound(vName)
' k = Ln - Len(vName(i))
' vName(i) = vName(i) & Space(k * 2)
' M = Len(vName(i))
' Next i
With Cht
.ChartType = xlColumnStacked
.HasLegend = True
.Legend.Position = xlLegendPositionBottom
.HasTitle = True
.ChartTitle.Text = Target.Value
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "OCF Percentiles"
.Axes(xlValue).MajorUnit = 50
For Each Srs In .SeriesCollection
Srs.Delete
Next Srs
For i = 0 To 2
Set Srs = .SeriesCollection.NewSeries
With Srs
.Name = vName(i) '<~~ Collection name
.Values = Target.Offset(0, 1).Resize(3).Offset(0, i)
.XValues = Array("A", "D", "I")
.Format.Fill.ForeColor.RGB = vColor(i)
If i = 0 Then
.Format.Fill.Transparency = 0.5 '<~~~~~ Transparency was adjusted
End If
End With
Next i
Set Srs = .SeriesCollection.NewSeries
With Srs
.Name = vName(3) '<~~ Collection name
.ChartType = xlXYScatter
.Values = Target.Offset(0, 4).Resize(1, 3)
.MarkerStyle = xlMarkerStyleSquare
.MarkerBackgroundColor = vColor(3) 'vbBlack
End With
End With
End Sub
Before
After

Excel - generate multiple series line chart using same column

I have a VBA script that I use to generate multiple line charts in Excel. It used to include 2 series collections per chart (reading from 2 columns) but I since modified it for only one. However now I want it to do 2 series' again but want it to read both collections from the same column. Is this possible?
I've tried modifying the .SeriesCollection(2) to go to the next range further down the column. However this just returns an error 4001.
Sub CreateCharts()
Dim ws As Worksheet
Dim ch As Chart
Dim NumCharts As Integer, ChartName As String, ChartTitle As String, i As Integer
Set ws = Sheets("Charts")
NumCharts = WorksheetFunction.CountA(ws.Rows(2))
For i = 2 To NumCharts Step 1 '1 column of data per chart
ChartName = ws.Cells(2, i) '"chrt" & Range(Col1 & 2)
ChartTitle = ws.Cells(2, i) 'Range(Col1 & 2)
Set ch = Charts.Add
With ch
.ChartType = xlLine
.SetSourceData Source:=ws.Range(ws.Cells(3, i), ws.Cells(20, i)), _
PlotBy:=xlColumns 'range of data for each chart
.SeriesCollection(1).XValues = ws.Range("A3:A20") 'data range of line 1 (test data)
.SeriesCollection(2).XValues = ws.Range("A21:A38") 'data range of line 2 (Rw curve)
.Name = ChartName
.HasTitle = True
.ChartTitle.Characters.text = "#" & ws.Cells(2, i) '& " " & ws.Cells(1, i) 'remove title 'change to "ws.Cells(2, i)" to see titles
.ChartTitle.Left = 600
'HORiZONTAL X AXiS
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.text = "Frequency (Hz)"
.Axes(xlCategory).MajorTickMark = xlNone
.Axes(xlCategory).AxisBetweenCategories = False
.Axes(xlCategory).Border.LineStyle = None
'VERTiCAL Y AXiS
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.text = "Sound Reduction Index (dB)"
.Axes(xlValue).TickLabels.NumberFormat = "0"
.Axes(xlValue).MajorTickMark = xlNone
.Axes(xlValue).HasMajorGridlines = False
.Axes(xlValue).MinimumScale = 10 'minimum value on y
.Axes(xlValue).MaximumScale = 80 'maximum value on y
.Axes(xlValue).Border.LineStyle = None
'LEGEND
.HasLegend = False
'FONT SPECiFiCATiONS
.ChartArea.Format.TextFrame2.TextRange.Font.Size = 14
.ChartArea.Format.TextFrame2.TextRange.Font.Name = "Myriad Pro"
.ChartArea.Border.LineStyle = xlNone
'CHART POSiTiON, SiZE & COLOUR
.PlotArea.Format.Fill.ForeColor.RGB = RGB(242, 242, 242) 'grey background
.PlotArea.Top = 0
.PlotArea.Left = 20
.PlotArea.Height = 440
.PlotArea.Width = 420
'CHART LiNE COLOURS
.SeriesCollection(1).Border.Color = RGB(27, 117, 188) 'first line colour
'.SeriesCollection(2).Border.Color = RGB(0, 0, 0) 'second line colour
'.SeriesCollection(2).LineStyle = xlDashDot
End With
Next i
End Sub
Here is an image example of what I'm wanting to achieve.
Code is slightly modified and tested to work as far my understanding of the objective (to create one 2 series charts per column. 1st series Row 3-20 and 2nd series 21 to 38). Only issue with code was absence of SeriesCollection(2). It is modified to add necessary SeriesCollection and to delete if any automatically added series collection exist.
For i = 2 To NumCharts Step 1 '1 column of data per chart
ChartName = ws.Cells(2, i) '"chrt" & Range(Col1 & 2)
ChartTitle = ws.Cells(2, i) 'Range(Col1 & 2)
Set ch = Charts.Add
'Delete if any automatically added series exist
For x = ch.SeriesCollection.Count To 1 Step -1
ch.SeriesCollection(x).Delete
Next
With ch
.ChartType = xlLine
.SeriesCollection.Add ws.Range(ws.Cells(3, i), ws.Cells(20, i))
.SeriesCollection.Add ws.Range(ws.Cells(21, i), ws.Cells(38, i))
.SeriesCollection(1).XValues = ws.Range(ws.Cells(3, 1), ws.Cells(20, 1))
.SeriesCollection(2).XValues = ws.Range(ws.Cells(21, 1), ws.Cells(38, 1))
.Name = ChartName

Multiple Chart objects on one sheet

I'm trying to place two chart objects on a single sheet and encountering difficulties with Excel 2010.
My code was working fine with a single chart object but when I added an additional chart: the chart type, title and other attributes are not registering up on the second chart.
The two charts should have the same structure but reference a different column on the sheet. I've looked around but couldn’t find a solution. Please suggest how to fix this problem. I’m posting partial code only but can post the rest of the code if it's helpful. Sorry if the code is too long...
I really appreciate your help.
Function GraphMFI(Arr() As Variant, Arr2() As Variant, ChartName As String, ChartName2 As String)
Dim i As Long, l As Long
Dim rng As Range, aCell As Range
Dim MyArY() As Variant, MyArX() As Variant
Dim LastRow As Long, iVal As Long
Dim Count As Long, SumArr As Long, AvgC As Long
Application.EnableEvents = False
'***********************************************************************
'Code that calculates x and y values not shown
'**************************************************************************
On Error Resume Next
'~~~~~~~~~chart code begins
Call DeleteallCharts 'delete all existing charts from active sheet
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~add both charts~~~~~~
Set objChart = ActiveSheet.ChartObjects.Add _
(Left:=410, Width:=500, Top:=15, Height:=250)
objChart.Chart.ChartType = xlXYScatterLines
Set objChart2 = ActiveSheet.ChartObjects.Add _
(Left:=410, Width:=500, Top:=300, Height:=250)
objChart.Chart.ChartType = xlXYScatterLines
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~add both charts~~~~~~
Dim objChartSeriesColl As SeriesCollection
Dim objChartSeriesColl2 As SeriesCollection
Set objChartSeriesColl = objChart.Chart.SeriesCollection
Set objChartSeriesColl2 = objChart2.Chart.SeriesCollection
'delete all chart series
'~~~~~~~~~~~first chart
With objChartSeriesColl.NewSeries '~~~raw data
.Name = "Inner Run Variability"
.Values = Arr
.XValues = rng
.MarkerSize = 10
.
'code not shown
End With
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~First Chart
With objChartSeriesColl.NewSeries '~~~average series one
Dim nPts As Long
.Name = "Mean"
.Values = AvgArr '~~~~average of Negative control
.XValues = rng '~~~dates
'.AxisGroup = xlSecondary
.ChartType = xlXYScatterLinesNoMarkers
'With mySrs
nPts = .Points.Count
.Points(nPts).ApplyDataLabels _
Type:=xlDataLabelsShowValue, _
AutoText:=True, LegendKey:=False
.Points(nPts).DataLabel.Text = .Name
.Points(nPts).ApplyDataLabels Type:=xlDataLabelsShowValue, _
AutoText:=True, LegendKey:=False
With .DataLabels
.AutoScaleFont = False
.Font.Size = 10
.Font.ColorIndex = 3
.Position = xlLabelPositionAbove
.HorizontalAlignment = xlLeft
.VerticalAlignment = xlCenter
.Orientation = xlHorizontal
End With
'~~~~~~~~~~~~~~~~~~
End With
With objChartSeriesColl.NewSeries '~~plus two stdev series two
.Name = "plus 2 stdev"
.Values = TwoPlusSdtDevArr
.XValues = rng '~~~dates
End With
With objChartSeriesColl.NewSeries 'minus three stdev series three
.Name = "minus 2 stdev"
.Values = TwiceStdDevArr
.XValues = rng
.ChartType = xlXYScatterLinesNoMarkers
End With
'~~~~~~~~~~~Second chart
With objChartSeriesColl2.NewSeries '~~~raw data
.Name = "Inner Run Variability"
.Values = Arr2
.XValues = rng
.MarkerSize = 10
End With
'~~~~~adding series to the second chart
With objChartSeriesColl2.NewSeries '~~~average
Dim nPts2 As Long
.Name = "Mean"
.Values = AvgArr
.XValues = rng '~~~dates
.ChartType = xlXYScatterLinesNoMarkers
End With
'....more series not shown here
With objChart
.Axes(xlCategory).TickLabels.NumberFormat = "m/d/yyyy" 'changes Xaxis text format
.Axes(xlValue).TickLabels.NumberFormat = "General" 'changes Yaxis Text Format
.SetElement (msoElementChartTitleAboveChart) 'adds chart title above chart
.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis) 'adds Xaxis title
.SetElement (msoElementPrimaryValueAxisTitleRotated) 'adds rotated Yaxis Title
.ChartTitle.Text = ChartName 'adds chart title above chart
.SetElement (msoElementLegendNone)
'~~~~~~~~~~~~set plot area
With .PlotArea
.Width = .Width / 2
.Height = .Height / 2
.Left = 16
.Top = 16
.Width = 450
End With
'~~~~~~~~~~~~~~~~
With .Axes(xlCategory, xlPrimary)
.AxisTitle.Text = "Run Dates" 'renames Xaxis title to "X Title"
.AxisTitle.Font.Bold = True
End With
With .Axes(xlValue, xlPrimary)
.AxisTitle.Text = "Sample Dates" 'renames Xaxis title to "X Title"
.AxisTitle.Text = "MFI Values" 'renames Yaxis title to "Y Title"
End With
.Axes(xlCategory).MinimumScale = ChartMin '~~adds min
.Axes(xlCategory).MaximumScale = ChartMax '~~ adds max
.Parent.Placement = xlFreeFloating
With .ChartArea.Format.Line
.Visible = msoCTrue
.Style = msoLineSingle
.Weight = 1
.ForeColor.RGB = RGB(255, 255, 255)
End With
End With
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'more code
End With
''''~~~~~~~~~~~~~Second Chart begins here
With objChart2
'..........
'code almost the same as 'with objChart'
Application.EnableEvents = True
End With
End Function

Put in X axis counter timer from integer in Excel Chart

In my Excel chart, the Xaxis shows sequential integers representing seconds in multiples of 10:
10 20 30 40 50 60 70 80 90 100 and so on
How to show in this Xaxis minutes and seconds like this:
00:00 00:10 00:20 00:30 00:40 00:50 01:00 01:10 01:20 01:30 and so on
I use the VBA below to generate the chart.
Sub NewChart()
'Create a chart
Sheets(1).Select 'Select the active Sheet
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatter
LastLine = Sheets(2).Range("A" & Rows.Count).End(xlUp).Row
Dim MaxScale As Integer
Dim MTotal As Integer
Dim Aprox As Integer
Dim vName As String
vName = Sheets(2).Range("B3")
Dim Qx As Integer
Qx = LastLine - 1
Dim Rangg As Integer
Rangg = (LastLine * 10) - 60
MTotal = Rangg
MaxScale = Rangg + 20
With ActiveChart
.ChartType = xlXYScatter
'Set data source range.
.SetSourceData Source:=Sheets(2).Range("A5:A" & Qx & ",B5:B" & Qx & ",E5:E" & Qx & ",H5:H" & Qx & ", K5:K" & Qx & ", N5:N" & Qx & ", Q5:Q" & Qx) ', 'PlotBy:=xlRows
.HasTitle = True
.ChartTitle.Text = vName '"2-7μm"
'The Parent property is used to set properties of the Chart.
With .Parent
.Top = 2
.Left = 2
.Width = 540
.Height = 252
.Name = "2micron"
End With
ActiveChart.Legend.Select
With Selection.Format.TextFrame2.TextRange.Font
.NameComplexScript = "Tahoma"
.NameFarEast = "Tahoma"
.Name = "Tahoma"
End With
.Axes(xlCategory).MajorTickMark = xlInside
.Axes(xlCategory).MinorTickMark = xlInside
.Axes(xlCategory, xlPrimary).Select
.Axes(xlCategory, xlPrimary).TickLabels.Font.Size = 5
.Axes(xlCategory, xlPrimary).TickLabels.Font.Name = "Tahoma"
.Axes(xlCategory, xlPrimary).TickLabels.Font.Bold = msoTrue
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Time (seconds)"
.Axes(xlCategory, xlPrimary).AxisTitle.Font.Size = 11
.Axes(xlCategory, xlPrimary).AxisTitle.Font.Bold = msoTrue
.Axes(xlCategory, xlPrimary).AxisTitle.Font.Name = "Tahoma"
.Axes(xlCategory, xlPrimary).MinorUnitIsAuto = False
.Axes(xlCategory, xlPrimary).MajorUnit = 300
.Axes(xlCategory, xlPrimary).MinorUnit = 60
.Axes(xlCategory, xlPrimary).MaximumScale = MaxScale
.Axes(xlCategory, xlPrimary).MinimumScale = 0
.Axes(xlCategory, xlPrimary).HasMajorGridlines = True
.Axes(xlCategory, xlPrimary).HasMinorGridlines = True
.Axes(xlCategory).Format.Line.ForeColor.RGB = RGB(0, 0, 0)
.Axes(xlValue).MajorTickMark = xlInside
.Axes(xlValue).MinorTickMark = xlInside
.Axes(xlValue, xlPrimary).Select
.Axes(xlValue, xlPrimary).HasMajorGridlines = True
.Axes(xlValue, xlPrimary).HasMinorGridlines = True
.Axes(xlValue, xlPrimary).TickLabels.Font.Size = 11
.Axes(xlValue, xlPrimary).TickLabels.Font.Name = "Tahoma"
.Axes(xlValue, xlPrimary).TickLabels.Font.Bold = msoTrue
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Y axis Legend"
.Axes(xlValue, xlPrimary).AxisTitle.Font.Size = 11
.Axes(xlValue, xlPrimary).AxisTitle.Font.Name = "Tahoma"
.Axes(xlValue, xlPrimary).AxisTitle.Font.Bold = msoTrue
.Axes(xlValue).Format.Line.ForeColor.RGB = RGB(0, 0, 0)
.Legend.IncludeInLayout = False
.Legend.Select
Selection.Position = xlTop
Selection.Font.Size = 11
Selection.Font.Name = "Tahoma"
Selection.Font.Bold = msoTrue
ActiveSheet.Shapes("2micron").ScaleWidth 1, msoFalse, _
msoScaleFromTopLeft
ActiveChart.SetElement (msoElementChartTitleAboveChart)
ActiveChart.ChartTitle.Select
Selection.Left = 2
Selection.Top = 2
Selection.Format.TextFrame2.TextRange.Font.Size = 13.2
Selection.Format.TextFrame2.TextRange.Font.Name = "Tahoma"
Selection.Format.TextFrame2.TextRange.Font.Bold = msoTrue
ActiveChart.Legend.Select
Selection.Left = 180
Selection.Top = 2
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
End With
ActiveChart.PlotArea.Select
Selection.Top = 22
Selection.Left = 20
Selection.Height = 207
Selection.Width = 540
Selection.Border.LineStyle = xlSolid
Selection.Border.Color = vbBlack
Selection.Interior.Color = vbWhite
End With
Call f_l2m1(1, 8, RGB(0, 176, 240))
Call f_l2m1(2, 3, RGB(255, 0, 0))
Call f_l2m1(3, 1, RGB(255, 0, 255))
Call f_l2m1(4, 2, RGB(153, 0, 255))
Call f_l2m1(5, 4, RGB(153, 0, 255))
Call f_l2m1(6, 9, RGB(146, 208, 80))
Range("a22").Select
End Sub
Sub f_l2m1(LineNo, MStyle, vRGB)
With ActiveSheet.ChartObjects("2micron").Chart
ActiveChart.SeriesCollection(LineNo).Select
With Selection
.MarkerStyle = MStyle
.MarkerSize = 7
.MarkerForegroundColor = vRGB
End With
Selection.Format.Fill.Visible = msoFalse
Selection.Format.Line.Visible = msoFalse
Selection.Format.Line.ForeColor.RGB = vRGB
End With
End Sub
Convert your data from whole number seconds to actual times:
Divide by 86400 (60sec/min*60min/hr*24hr/day)
Use number format MM:SS
Make sure chart is an XY chart. X axis should pick up MM:SS format from data, but if not you can format it by formatting the axis.

Resources