Sub setgraphstyle(ByVal wksht As Worksheet, ByVal chtname As String, ByVal chttitle As String, ByVal chtxtitle As String, ByVal chtytitle As String, Optional ByVal chtytitle2 As String, Optional ByVal chtbkcol As Colour, Optional ByVal chtbordercol As Colour, Optional ByVal chtlegend As Boolean, Optional ByVal chtlegbord As Boolean, Optional ByVal chttitlefont As NewFont, Optional chtaxisfont As NewFont, Optional ByVal chtdatafont As NewFont, Optional ByVal xmin As String, Optional ByVal xmax As String, Optional ByVal ymin As String, Optional ByVal ymax As String, Optional ByVal ymin2 As String, Optional ByVal ymax2 As String, Optional ByVal xgrid As Boolean, Optional ByVal ygrid As Boolean, Optional ByVal majtick As XlTickMark, Optional ByVal mintick As XlTickMark)
On Error Resume Next
'-------------------------------------------------
' Parameters:
'
' Worksheet
' Chart name
' Chart title
' x-axis title
' y-axis title
' Back colour
' Border colour
' Legend? (True/False)
' Legend border
' Chart title font
' Axis title font
' Axis and Legend font
' Min and max of x, y, second y axes
' Gridlines
' Tickmarks
'
'-------------------------------------------------
Dim newcht As ChartObject
Set newcht = wksht.ChartObjects(chtname)
'Define a general font
Dim genfont As New NewFont
genfont.Name = "Arial"
genfont.Size = 10
genfont.Bold = False
genfont.Italic = False
genfont.Underline = False
'Tidy up missing parameters
If chttitlefont Is Nothing Then Set chttitlefont = genfont
If chtaxisfont Is Nothing Then Set chtaxisfont = genfont
If chtdatafont Is Nothing Then Set chtdatafont = genfont
If chtbkcol = 0 Then chtbkcol = White
If chtbordercol = 0 Then chtbordercol = Black
'Set Title
If chttitle <> "" Then
newcht.Chart.HasTitle = True
newcht.Chart.ChartTitle.Text = chttitle
newcht.Chart.ChartTitle.Font.Name = chttitlefont.Name
newcht.Chart.ChartTitle.Font.Size = chttitlefont.Size
newcht.Chart.ChartTitle.Font.Bold = chttitlefont.Bold
newcht.Chart.ChartTitle.Font.Italic = chttitlefont.Italic
newcht.Chart.ChartTitle.Font.Underline = chttitlefont.Underline
End If
'Set Axis titles
If chtxtitle <> "" Then
newcht.Chart.Axes(xlCategory).HasTitle = True
newcht.Chart.Axes(xlCategory).AxisTitle.Caption = chtxtitle
newcht.Chart.Axes(xlCategory).AxisTitle.Font.Name = chtaxisfont.Name
newcht.Chart.Axes(xlCategory).AxisTitle.Font.Size = chtaxisfont.Size
newcht.Chart.Axes(xlCategory).AxisTitle.Font.Bold = chtaxisfont.Bold
newcht.Chart.Axes(xlCategory).AxisTitle.Font.Italic = chtaxisfont.Italic
newcht.Chart.Axes(xlCategory).AxisTitle.Font.Underline = chtaxisfont.Underline
End If
If chtytitle <> "" Then
newcht.Chart.Axes(xlValue).HasTitle = True
newcht.Chart.Axes(xlValue).AxisTitle.Caption = chtytitle
newcht.Chart.Axes(xlValue).AxisTitle.Font.Name = chtaxisfont.Name
newcht.Chart.Axes(xlValue).AxisTitle.Font.Size = chtaxisfont.Size
newcht.Chart.Axes(xlValue).AxisTitle.Font.Bold = chtaxisfont.Bold
newcht.Chart.Axes(xlValue).AxisTitle.Font.Italic = chtaxisfont.Italic
newcht.Chart.Axes(xlValue).AxisTitle.Font.Underline = chtaxisfont.Underline
End If
If chtytitle2 <> "" Then
newcht.Chart.Axes(xlValue, xlSecondary).HasTitle = True
newcht.Chart.Axes(xlValue, xlSecondary).AxisTitle.Caption = chtytitle2
newcht.Chart.Axes(xlValue, xlSecondary).AxisTitle.Font.Name = chtaxisfont.Name
newcht.Chart.Axes(xlValue, xlSecondary).AxisTitle.Font.Size = chtaxisfont.Size
newcht.Chart.Axes(xlValue, xlSecondary).AxisTitle.Font.Bold = chtaxisfont.Bold
newcht.Chart.Axes(xlValue, xlSecondary).AxisTitle.Font.Italic = chtaxisfont.Italic
newcht.Chart.Axes(xlValue, xlSecondary).AxisTitle.Font.Underline = chtaxisfont.Underline
End If
'Set axis data fonts
With newcht.Chart.Axes(xlCategory).TickLabels.Font
.Name = chtdatafont.Name
.Size = chtdatafont.Size
.Bold = chtdatafont.Bold
.Italic = chtdatafont.Italic
.Underline = chtdatafont.Underline
End With
With newcht.Chart.Axes(xlValue).TickLabels.Font
.Name = chtdatafont.Name
.Size = chtdatafont.Size
.Bold = chtdatafont.Bold
.Italic = chtdatafont.Italic
.Underline = chtdatafont.Underline
End With
If chtytitle2 <> "" Then
With newcht.Chart.Axes(xlValue, xlSecondary).TickLabels.Font
.Name = chtdatafont.Name
.Size = chtdatafont.Size
.Bold = chtdatafont.Bold
.Italic = chtdatafont.Italic
.Underline = chtdatafont.Underline
End With
End If
'Set chart interior and border
newcht.Chart.PlotArea.Interior.ColorIndex = chtbkcol
newcht.Chart.PlotArea.Border.ColorIndex = chtbordercol
'Set Legend
newcht.Chart.HasLegend = chtlegend
If chtlegend = True Then
newcht.Chart.Legend.Border.LineStyle = chtlegbord
newcht.Chart.Legend.Font.Name = chtdatafont.Name
newcht.Chart.Legend.Font.Size = chtdatafont.Size
newcht.Chart.Legend.Font.Bold = chtdatafont.Bold
newcht.Chart.Legend.Font.Italic = chtdatafont.Italic
newcht.Chart.Legend.Font.Underline = chtdatafont.Underline
End If
'Set chart axis limits
If xmin <> "" Then
newcht.Chart.Axes(xlCategory).MinimumScale = Val(xmin)
Else
newcht.Chart.Axes(xlCategory).MinimumScaleIsAuto = True
End If
If xmax <> "" Then
newcht.Chart.Axes(xlCategory).MaximumScale = Val(xmax)
Else
newcht.Chart.Axes(xlCategory).MaximumScaleIsAuto = True
End If
If ymin <> "" Then
newcht.Chart.Axes(xlValue).MinimumScale = Val(ymin)
Else
newcht.Chart.Axes(xlValue).MinimumScaleIsAuto = True
End If
If ymax <> "" Then
newcht.Chart.Axes(xlValue).MaximumScale = Val(ymax)
Else
newcht.Chart.Axes(xlValue).MaximumScaleIsAuto = True
End If
If ymin2 <> "" Then
newcht.Chart.Axes(xlValue, xlSecondary).MinimumScale = Val(ymin2)
Else
newcht.Chart.Axes(xlValue, xlSecondary).MinimumScaleIsAuto = True
End If
If ymax2 <> "" Then
newcht.Chart.Axes(xlValue, xlSecondary).MaximumScale = Val(ymax2)
Else
newcht.Chart.Axes(xlValue, xlSecondary).MaximumScaleIsAuto = True
End If
'Set gridlines
If xgrid <> "" Then
newcht.Chart.Axes(xlCategory).HasMajorGridlines = xgrid
Else
newcht.Chart.Axes(xlCategory).HasMajorGridlines = False
End If
If ygrid <> "" Then
newcht.Chart.Axes(xlValue).HasMajorGridlines = ygrid
Else
newcht.Chart.Axes(xlValue).HasMajorGridlines = False
End If
'Set tickmarks
newcht.Chart.Axes(xlCategory).MajorTickMark = majtick
newcht.Chart.Axes(xlCategory).MinorTickMark = mintick
newcht.Chart.Axes(xlValue).MajorTickMark = majtick
newcht.Chart.Axes(xlValue).MinorTickMark = mintick
If chtytitle2 <> "" Then
newcht.Chart.Axes(xlValue, xlSecondary).MajorTickMark = majtick
newcht.Chart.Axes(xlValue, xlSecondary).MinorTickMark = mintick
End If
End Sub