Charts Using Vba
Charts Using Vba
Method 1:
Sub CreateChart()
'PURPOSE: Create a chart (chart dimensions are not required)
'Create a chart
Set cht = ActiveSheet.Shapes.AddChart2
End Sub
Method 2:
Sub CreateChart()
'PURPOSE: Create a chart (chart dimensions are required)
'Create a chart
Set cht = ActiveSheet.ChartObjects.Add( _
Left:=ActiveCell.Left, _
Width:=450, _
Top:=ActiveCell.Top, _
Height:=250)
End Sub
Sub AddChartTitle()
'PURPOSE: Add a title to a specific chart
End Sub
Sub RepositionChartTitle()
'PURPOSE: Reposition a chart's title
'Reposition title
With cht.Chart.ChartTitle
.Left = 100
.Top = 50
End With
End Sub
Sub InsertChartLegend()
End Sub
Sub DimensionChartLegend()
lgd.Left = 240.23
lgd.Top = 6.962
lgd.Width = 103.769
lgd.Height = 25.165
End Sub
Sub AddStuffToChart()
'Add X-axis
cht.HasAxis(xlCategory, xlPrimary) = True '[Method #1]
cht.SetElement (msoElementPrimaryCategoryAxisShow) '[Method #2]
'Add X-axis title
cht.Axes(xlCategory, xlPrimary).HasTitle = True '[Method #1]
cht.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis) '[Method #2]
'Add y-axis
cht.HasAxis(xlValue, xlPrimary) = True '[Method #1]
cht.SetElement (msoElementPrimaryValueAxisShow) '[Method #2]
End Sub
Sub ChangeChartFormatting()
End Sub
Sub RemoveChartFormatting()
'Remove Gridlines
cht.Axes(xlValue).MajorGridlines.Delete
cht.Axes(xlValue).MinorGridlines.Delete
'Remove X-axis
cht.Axes(xlCategory).Delete
'Remove Y-axis
cht.Axes(xlValue).Delete
'Remove Legend
cht.Legend.Delete
'Remove Title
cht.ChartTitle.Delete
Workbook Events:
Worksheet Events:
Chart Events:
For examples, Select (when an object in the chart is selected), SeriesChange (when a value of a data
point in a series is changed). etc…
Application Events: