Macro to format charts - excel

This is an excerpt of the code from the full macro. It operates correctly in most cases, however the color formatting is not applied when there is only one data series.
Sub fullPageLine()
Dim rng As Range
Dim cht As Object
Dim chart As chart
'Data range for the chart
'Set rng = Selection
ActiveSheet.Shapes.AddChart2(227, xlLine).Select
'Create a chart
Set cht = Selection
'Format x axis
ActiveChart.ChartArea.Select
With Selection
.Format.TextFrame2.TextRange.Font.Name = "Arial"
.Format.TextFrame2.TextRange.Font.Size = 7
End With
'Format title
ActiveChart.ChartTitle.Font.Size = 8.4
ActiveChart.ChartTitle.Left = 0
ActiveChart.ChartTitle.Top = 2
ActiveChart.ChartTitle.Select
With Selection.Format.TextFrame2.TextRange.Characters.Font
.BaselineOffset = 0
.Bold = msoTrue
.Size = 8.4
.Name = "Arial"
.Caps = msoAllCaps
End With
'Format legend
ActiveChart.Legend.Select
With Selection.Format.TextFrame2.TextRange.Font
.NameComplexScript = "Arial"
.NameFarEast = "Arial"
.Name = "Arial"
End With
Selection.Format.TextFrame2.TextRange.Font.Size = 7
'Change chart series fill color
With ActiveChart.FullSeriesCollection(1).Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent2
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
End With
With ActiveChart.FullSeriesCollection(2).Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorText1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
End With
With ActiveChart.FullSeriesCollection(3).Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.5
.Transparency = 0
End With
The first data series should be in orange, and is whenever there are 2 or more lines on the chart. However, if it is a single line chart, it shows up in the default blue rather than orange. I am new to vba, and am aware that I need to get rid of select and activate, but am trying to get the base code to work first.

Your macro fails before assigning colors when there is only one series, because a chart with one series, by default, does not have a Legend.
Ensure that you have a legend in the chart (alternatively, use conditional logic to check whether Legend exists) before attempting to format the Legend.
Cleaned up to use object variables appropriately, and do your series formatting in a loop.
Option Explicit
Sub fullPageLine()
Dim rng As Range
Dim cht As chart
Dim i As Long, color As Long, bright As Double
Dim srs As Series
'Data range for the chart
'Set rng = Selection
Set cht = ActiveSheet.Shapes.AddChart2(227, xlLine).chart
'Format x axis
With cht.ChartArea.Format.TextFrame2.TextRange.Font
.Name = "Arial"
.Size = 7
End With
'Format title
With cht.ChartTitle
.Font.Size = 8.4
.Left = 0
.Top = 2
With .Format.TextFrame2.TextRange.Characters.Font
.BaselineOffset = 0
.Bold = msoTrue
.Size = 8.4
.Name = "Arial"
.Caps = msoAllCaps
End With
End With
'Format legend
cht.HasLegend = True
With cht.Legend.Format.TextFrame2.TextRange.Font
.NameComplexScript = "Arial"
.NameFarEast = "Arial"
.Name = "Arial"
.Size = 7
End With
' ALTERNATELY, instead of forcing the legend as per above:
'If cht.HasLegend Then
' With cht.Legend.Format.TextFrame2.TextRange.Font
' .NameComplexScript = "Arial"
' .NameFarEast = "Arial"
' .Name = "Arial"
' .Size = 7
' End With
'End If
'Change chart series fill color
For i = 1 To cht.FullSeriesCollection.Count
' Get the color based on series index
Select Case i
Case 1
color = msoThemeColorAccent2
Case 2
color = msoThemeColorText1
Case 3
color = msoThemeColorBackground1
bright = -0.5
End Select
' Assign series color formats
'## NOTE: This only works for the cases defined in the above Select statement.
Set srs = cht.FullSeriesCollection(i)
With srs.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = color
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = bright
.Transparency = 0
End With
Next
End Sub

Related

Suggestions for codes format to set the chart coordinates,title and added text in excel macro

Wrote a macro to draw line with markers plot with excel, it works well in a single macro xlsm file. But when I tried to convert it to a excel addin (xlam file) , it got a lot of bugs. All the bugs are related to the format of both X and Y coordinates, position, font type and size of chart title, and position, font type and sizeof added text. Not sure what is the reason, need to know the correct format of them. Any debug suggestions or help, really appreciated. Please see the error message and my full macro codes as the following. Thanks.
The error message is run time error '-21474627161 (800004003)': the object is no longer valid.
After you clicked the debug, the code " .left=358" was highlighted with yellow.
But you checked with excel, the plot was drawn without chart title and the add text (that I want) and the format of coordinate was not that I tried to set. Again all these errors only happen with the xlam file, the macro works well with xlms fie.
Sub strain_plot()
sh_rows = ActiveWorkbook.ActiveSheet.Range("B65535").End(xlUp).Row
For i = 1 To sh_rows
If ActiveSheet.Cells(i, 1).Value < 0.000001 Then
ActiveSheet.Cells(i, 1).Value = 1000000000# * ActiveSheet.Cells(i, 1).Value
End If
Next i
ii = sh_rows
c_name = "chart1"
On Error GoTo err:
ActiveWorkbook.ActiveSheet.ChartObjects(c_name).Delete
err:
Set ch = ActiveWorkbook.ActiveSheet.ChartObjects.Add(330, 120, 480, 270) 'set graph position and size
ch.Name = c_name
With ch.Chart
For iii = 1 To 2
.SeriesCollection.NewSeries
.SeriesCollection(iii).Values = Range(ActiveWorkbook.ActiveSheet.Cells(1, iii + 1), ActiveWorkbook.ActiveSheet.Cells(ii, iii + 1))
.SeriesCollection(iii).XValues = Range(ActiveWorkbook.ActiveSheet.Cells(1, 1), ActiveWorkbook.ActiveSheet.Cells(ii, 1))
.SeriesCollection(iii).ChartType = xlLineMarkers
Next iii
.SeriesCollection(1).Name = "[110]"
.SeriesCollection(1).MarkerStyle = 2
.SeriesCollection(1).MarkerSize = 12
.SeriesCollection(1).MarkerForegroundColor = RGB(255, 0, 0)
.SeriesCollection(1).MarkerBackgroundColor = RGB(255, 0, 0)
.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(255, 0, 0)
.SeriesCollection(2).Name = "[001]"
.SeriesCollection(2).MarkerStyle = 2
.SeriesCollection(2).MarkerSize = 12
.SeriesCollection(2).MarkerForegroundColor = RGB(96, 96, 96)
.SeriesCollection(2).MarkerBackgroundColor = RGB(96, 96, 96)
.SeriesCollection(2).Format.Fill.ForeColor.RGB = RGB(96, 96, 96)
With .Legend
.IncludeInLayout = False
.Position = xlLegendPositionRight
.AutoScaleFont = False
.Font.Size = 14
.Top = 25
.Left = 392
.Width = 72
.Height = 40
End With
With .ChartArea.Fill
.Visible = msoTrue
.ForeColor.SchemeColor = 33
.Solid
End With
With .SeriesCollection(1).Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 0, 0) 'red
.Transparency = 0
End With
With .SeriesCollection(2).Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(96, 96, 96) 'grey
.Transparency = 0
End With
.HasTitle = True
With .ChartTitle
.Text = ActiveWorkbook.ActiveSheet.Cells(5, 8)
.Left = 358
.Top = 236
With .Font
.Name = "Tahoma"
.Size = 10
End With
End With
With .Axes(xlCategory)
.HasTitle = True
.AxisTitle.Text = "Position(nm)" 'X-axis title
.TickLabels.Font.Size = 10 'X-axis coordinate number size
.AxisTitle.Font.Size = 14 'X-axis title word font size
.TickMarkSpacing = 3
.TickLabelSpacing = 5
.TickLabels.NumberFormatLocal = "#,##0._);[red](#,##0.)"
.TickLabels.NumberFormatLocal = "#,##0_);[red](#,##0)"
.TickLabels.NumberFormatLocal = "0_);[red](0.)"
End With
With .Axes(xlValue)
.HasTitle = True
.AxisTitle.Text = "Strain" 'Y-aixs title
.AxisTitle.Font.Size = 14 'y-axis title word font size
'Minimum value of Y axis
.Axes(xlValue).MinimumScale = -0.005
.Axes(xlValue).TickLabels.NumberFormatLocal = "0.0%"
End With
End With
Dim thechartobj As ChartObject
Set thechartobj = ActiveWorkbook.ActiveSheet.ChartObjects(ch.Name)
Dim thechart As Chart
Set thechart = thechartobj.Chart
Dim thetextbox As Shape
Set thetextbox = thechart.Shapes.AddTextbox(msoTextOrientationHorizontal, 688, 372, 122, 20)
With thetextbox.TextFrame.Characters
.Text = ActiveSheet.Cells(6, 8)
With .Font
.Name = "tahoma"
.Size = 10
.Bold = msoTrue
End With
End With
End Sub

Formatting Chart Legends Using With (Syntax)

I have a macro which adds and removes (filterseries) set data series of a chart (in its own sheet) by looping through checkboxes in a separate sheet. When I add and remove them, I want to cycle the legend off then back on so that it resizes itself automatically.
I think this is just a syntax error of how I'm using the with statement.
I have a separate macro which does this for a different purpose, but it loops through chart-sheets and treats them as variables and, for some reason, it works there.
Sub ISurfSeries1Checklist()
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim i As Integer
Dim c As Integer
For i = 1 To 56
If ActiveWorkbook.Sheets("Range").Cells(3 + i, 12).Value = True Then
ActiveWorkbook.Charts("I. Surf (1)").FullSeriesCollection(i).IsFiltered = False
Else 'ActiveWorkbook.Sheets("Range").Cells(3 + i, 12) = False Then
ActiveWorkbook.Charts("I. Surf (1)").FullSeriesCollection(i).IsFiltered = True
End If
Next i
For c = 51 To 56 'ActiveWorkbook.Charts("I. Surf (1)").FullSeriesCollection.Count
ActiveWorkbook.Charts("I. Surf (1)").FullSeriesCollection(c).Format.Line.Visible = msoTrue
ActiveWorkbook.Charts("I. Surf (1)").FullSeriesCollection(c).Format.Line.ForeColor.RGB = RGB(255, 0, 0)
ActiveWorkbook.Charts("I. Surf (1)").FullSeriesCollection(c).Format.Line.Transparency = 0
Next c
ActiveWorkbook.Charts("I. Surf (1)").HasLegend = False
ActiveWorkbook.Charts("I. Surf (1)").HasLegend = True
'***Below is where it stops working.***
With ActiveWorkbook.Charts("I.Surf (1)").Legend
.Font.Size = 8
.Border.Weight = xlHairline
.Border.Color = RGB(89, 89, 89)
.Interior.Color = RGB(255, 255, 255)
.Left = Cht_Sht.PlotArea.InsideLeft - Cht_Sht.Axes(xlValue).Format.Line.Weight
.Top = Cht_Sht.PlotArea.InsideTop
End With
Runtime Error '9'. Subscript out of range
on the With statement: With ActiveWorkbook.Charts("I.Surf (1)").Legend
That means a chart named "I.Surf (1)" does not exist you are probably missing the space between the dot and Surf. It should be "I. Surf (1)".
I recommend to reference the chart by a variable so you only have to use its name once. Coding rule number 1: Don't repeat yourself.
Dim ActChart As Chart
Set ActChart = ActiveWorkbook.Charts("I. Surf (1)")
This prevents typos and if you have to change it you only need to change it in one position:
Sub ISurfSeries1Checklist()
Application.ScreenUpdating = False
Application.EnableEvents = False
Dim ActChart As Chart
Set ActChart = ActiveWorkbook.Charts("I. Surf (1)")
Dim i As Long
For i = 1 To 56
'Note that you can shorten this to:
ActChart.FullSeriesCollection(i).IsFiltered = Not (ActiveWorkbook.Sheets("Range").Cells(3 + i, 12).Value = True)
Next i
Dim c As Long
For c = 51 To 56 'ActiveWorkbook.Charts("I. Surf (1)").FullSeriesCollection.Count
With ActChart.FullSeriesCollection(c).Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(255, 0, 0)
.Transparency = 0
End With
Next c
ActChart.HasLegend = False
ActChart.HasLegend = True
With ActChart.Legend
.Font.Size = 8
.Border.Weight = xlHairline
.Border.Color = RGB(89, 89, 89)
.Interior.Color = RGB(255, 255, 255)
.Left = Cht_Sht.PlotArea.InsideLeft - Cht_Sht.Axes(xlValue).Format.Line.Weight
.Top = Cht_Sht.PlotArea.InsideTop
End With
End Sub

How can I visualize specific columns using Excel VBA

I built the following VBA code that automatically creates the PIVOT table and the charts on a new worksheet.
The Pivot table is fine. The Bar chart is fine too. However, the Pie Chart (chart 2) is picking the "closed" column data (Column 1 in the Pivot table) from the Pivot table. I want it to pick the Grand total column, which is column 3 in the PT.
I tried to change the range on the top of the code, but it didn't work. I also removed the bar chart and executed with the PT and the Pie chart only and still same problem (the data is reading from the "Closed" column only).
Attachment: Contains screenshot of the output.
Here is my code below:
Sub PT_C_RiskType_Status()
'Defining the variables
Dim pc As PivotCache
Dim ws As Worksheet
Dim pt As PivotTable
Dim sh As Shape
Dim sh1 As Shape
Dim ch As Chart
Dim ch1 As Chart
Set pc = ThisWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:="DataSource", _
Version:=xlPivotTableVersion15)
Set ws = Sheets.Add
ws.Select
Cells(3, 1).Select
Range("A3").Select
'Create the pivot table
Set pt = pc.CreatePivotTable(TableDestination:=ws.Name & "!R3C1", TableName:="Pivottable")
'Adding the data fields
pt.AddDataField _
Field:=pt.PivotFields("Status"), _
Function:=XlConsolidationFunction.xlCount
'Adding the fields
pt.AddFields _
RowFields:="Risk Type", _
ColumnFields:="Status", _
PageFields:=Array("Date Received", "Risk Level")
'Adding formatting options
pt.DataFields(1).NumberFormat = "0"
'Adding Filters for the pivot
With ActiveSheet.PivotTables("PivotTable").PivotFields("Date Received")
.PivotItems("(blank)").Visible = False
End With
ActiveSheet.PivotTables("PivotTable").PivotFields("Date Received"). _
EnableMultiplePageItems = True
With ActiveSheet.PivotTables("PivotTable").PivotFields("Risk Level")
.PivotItems("N/A").Visible = False
End With
ActiveSheet.PivotTables("PivotTable").PivotFields("Risk Level"). _
EnableMultiplePageItems = True
With ActiveSheet.PivotTables("PivotTable").PivotFields("Risk Type")
.PivotItems("(blank)").Visible = False
End With
With ActiveSheet.PivotTables("PivotTable").PivotFields("Status")
.PivotItems("(blank)").Visible = False
End With
'Adding chart
Set sh = ws.Shapes.AddChart2( _
XlChartType:=XlChartType.xlColumnStacked, _
Width:=350, Height:=300)
Set sh1 = ws.Shapes.AddChart2( _
XlChartType:=XlChartType.xlPie, _
Width:=200, Height:=200)
Set ch1 = sh1.Chart
Set ch = sh.Chart
'The position of the chart (the Column Stacked Chart)
ch.SetSourceData pt.TableRange1
sh.Top = pt.TableRange1.Top
sh.Left = pt.TableRange1.Left + pt.TableRange1.Width + 10
'The position of the chart1 (The Pie Chart)
sh1.Left = sh.Left - sh.Width
'Formatting for chart and chart1
'Colors the area for "Opened" status
ch.FullSeriesCollection(2).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent4
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0.400000006
.Transparency = 0
.Solid
End With
'Colors the area for "Closed" status
ch.FullSeriesCollection(1).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent3
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.25
.Transparency = 0
.Solid
End With
'Formatting the labels inside the bars
'Adding data labels to the bars
ch.ApplyDataLabels (xlDataLabelsShowValue)
'Formatting Chart1 (Pie Chart)
ch1.ChartTitle.Select
Selection.Delete 'Delete the title
'Changing the color of the second portion of the chart
ch1.FullSeriesCollection(1).Select
ch1.FullSeriesCollection(1).Points(2).Select
With Selection.Format.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent4
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0.400000006
.Transparency = 0
.Solid
End With
' Moving the chart to the bottom of the pivot table
ActiveSheet.Shapes("Chart 2").IncrementLeft 2.5
ActiveSheet.Shapes("Chart 2").IncrementTop 97.5
End Sub
Please advise.
Thank you.

How do I change the background image of a chart in vba?

I need to change the background image of a chart I created using vba. I tried using the .Fill command but I can't make it work. How do I do it?
This is the code I used to create the chart and it is working fine:
With myChart
.ChartStyle = 245
.SeriesCollection.NewSeries
.SeriesCollection(1).Name = ""
.HasLegend = False
.Axes(xlCategory).HasTitle = True
.Axes(xlCategory).AxisTitle.Text = "Punteggio Tecnico"
.Axes(xlValue).HasTitle = True
.Axes(xlValue).AxisTitle.Characters.Text = "Punteggio Economico"
.Axes(xlCategory).MinimumScale = 0
.Axes(xlCategory).MaximumScale = 1
.Axes(xlValue).MinimumScale = 0
.Axes(xlValue).MaximumScale = 1
.SeriesCollection(1).XValues = "=Dati2!B3:B270"
.SeriesCollection(1).Values = "=Dati2!C3:C270"
End With
If myChart is Chart variable then you want the myChart.PlotArea.Format.Fill for the area of the chart that contains the actual chart, or myChart.ChartArea.Format.Fill for the whole chart area.
The code below shows how to use it. I've commented out the colouring code that a macro recorder would supply and replaced with a basic RGB value.
Sub Test()
Dim myChart As Chart
Set myChart = Sheet1.ChartObjects("Chart 2").Chart
With myChart
With .PlotArea.Format.Fill
.ForeColor.RGB = RGB(255, 0, 0)
' .Visible = msoTrue
' .ForeColor.ObjectThemeColor = msoThemeColorAccent6
' .ForeColor.TintAndShade = 0
' .ForeColor.Brightness = 0.400000006
' .Solid
End With
With .ChartArea.Format.Fill
.ForeColor.RGB = RGB(0, 255, 0)
' .Visible = msoTrue
' .ForeColor.ObjectThemeColor = msoThemeColorAccent6
' .ForeColor.TintAndShade = 0
' .ForeColor.Brightness = 0.400000006
' .Transparency = 0
' .Solid
End With
End With
End Sub

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

Resources