0% found this document useful (0 votes)
147 views

Charts Using Vba

The document provides instructions for performing various actions related to charts in Excel VBA, such as creating charts, adding titles and legends, modifying attributes, and removing elements. It discusses two methods for creating charts, with one specifying chart dimensions and the other not. It also covers adding titles and legends to charts; positioning titles; adding axes, gridlines, and trend lines; modifying scales, fonts, and other formatting; deleting series, gridlines, and other elements; and removing charts entirely. The document serves as a reference for programmers to learn how to programmatically build and modify charts using VBA.

Uploaded by

Pallab Datta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
147 views

Charts Using Vba

The document provides instructions for performing various actions related to charts in Excel VBA, such as creating charts, adding titles and legends, modifying attributes, and removing elements. It discusses two methods for creating charts, with one specifying chart dimensions and the other not. It also covers adding titles and legends to charts; positioning titles; adding axes, gridlines, and trend lines; modifying scales, fonts, and other formatting; deleting series, gridlines, and other elements; and removing charts entirely. The document serves as a reference for programmers to learn how to programmatically build and modify charts using VBA.

Uploaded by

Pallab Datta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Inserting A Chart

Method 1:

Sub CreateChart()
'PURPOSE: Create a chart (chart dimensions are not required)

Dim rng As Range


Dim cht As Object

'Your data range for the chart


Set rng = ActiveSheet.Range("A24:M27")

'Create a chart
Set cht = ActiveSheet.Shapes.AddChart2

'Give chart some data


cht.Chart.SetSourceData Source:=rng

'Determine the chart type


cht.Chart.ChartType = xlXYScatterLines

End Sub

Method 2:

Sub CreateChart()
'PURPOSE: Create a chart (chart dimensions are required)

Dim rng As Range


Dim cht As ChartObject

'Your data range for the chart


Set rng = ActiveSheet.Range("A24:M27")

'Create a chart
Set cht = ActiveSheet.ChartObjects.Add( _
Left:=ActiveCell.Left, _
Width:=450, _
Top:=ActiveCell.Top, _
Height:=250)

'Give chart some data


cht.Chart.SetSourceData Source:=rng
'Determine the chart type
cht.Chart.ChartType = xlXYScatterLines

End Sub

Adding & Modifying A Chart Title

Sub AddChartTitle()
'PURPOSE: Add a title to a specific chart

Dim cht As ChartObject

Set cht = ActiveSheet.ChartObjects("Chart 1")

'Ensure chart has a title


cht.Chart.HasTitle = True

'Change chart's title


cht.Chart.ChartTitle.Text = "My Graph"

End Sub

Sub RepositionChartTitle()
'PURPOSE: Reposition a chart's title

Dim cht As ChartObject

Set cht = ActiveSheet.ChartObjects("Chart 1")

'Reposition title
With cht.Chart.ChartTitle
.Left = 100
.Top = 50
End With

End Sub

Adding & Modifying A Graph Legend

Sub InsertChartLegend()

Dim cht As Chart


Set cht = ActiveSheet.ChartObjects("Chart 1").Chart

'Add Legend to the Right


cht.SetElement (msoElementLegendRight)

'Add Legend to the Left


cht.SetElement (msoElementLegendLeft)

'Add Legend to the Bottom


cht.SetElement (msoElementLegendBottom)

'Add Legend to the Top


cht.SetElement (msoElementLegendTop)

'Add Overlaying Legend to the Left


cht.SetElement (msoElementLegendLeftOverlay)

'Add Overlaying Legend to the Right


cht.SetElement (msoElementLegendRightOverlay)

End Sub

Sub DimensionChartLegend()

Dim lgd As Legend

Set lgd = ActiveSheet.ChartObjects("Chart 1").Chart.Legend

lgd.Left = 240.23
lgd.Top = 6.962
lgd.Width = 103.769
lgd.Height = 25.165

End Sub

Adding Various Chart Attributes

Sub AddStuffToChart()

Dim cht As Chart

Set cht = ActiveSheet.ChartObjects("Chart 1").Chart

'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]

'Add y-axis title


cht.Axes(xlValue, xlPrimary).HasTitle = True '[Method #1]
cht.SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis) '[Method #2]

'Add Data Labels (Centered)


cht.SetElement (msoElementDataLabelCenter)

'Add Major Gridlines


cht.SetElement (msoElementPrimaryValueGridLinesMajor)

'Add Linear Trend Line


cht.SeriesCollection(1).Trendlines.Add Type:=xlLinear

End Sub

Modifying Various Chart Attributes

Sub ChangeChartFormatting()

Dim cht As Chart

Set cht = ActiveSheet.ChartObjects("Chart 1").Chart

'Adjust y-axis Scale


cht.Axes(xlValue).MinimumScale = 40
cht.Axes(xlValue).MaximumScale = 100

'Adjust x-axis Scale


cht.Axes(xlCategory).MinimumScale = 1
cht.Axes(xlCategory).MaximumScale = 10

'Adjust Bar Gap


cht.ChartGroups(1).GapWidth = 60

'Format Font Size


cht.ChartArea.Format.TextFrame2.TextRange.Font.Size = 12
'Format Font Type
cht.ChartArea.Format.TextFrame2.TextRange.Font.Name = "Arial"

'Make Font Bold


cht.ChartArea.Format.TextFrame2.TextRange.Font.Bold = msoTrue

'Make Font Italicized


cht.ChartArea.Format.TextFrame2.TextRange.Font.Italic = msoTrue

End Sub

Removing Various Chart Attributes

Sub RemoveChartFormatting()

Dim cht As Chart

Set cht = ActiveSheet.ChartObjects("Chart 1").Chart

'Remove Chart Series


cht.SeriesCollection(2).Delete

'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

'Remove ChartArea border


cht.ChartArea.Border.LineStyle = xlNone

'No background color fill


cht.ChartArea.Format.Fill.Visible = msoFalse
cht.PlotArea.Format.Fill.Visible = msoFalse
End Sub

What Are Events?


Event programming is the most useful tool which helps to monitor specific user actions within Excel.
For example, if the user Opening a Workbook, Selects a Worksheet, enters data into a Cell, or saves a
Workbook, these actions are all Excel Events.

What Are The Different Types of Events?


Events are linked to Excel Worksheets, Charts, Workbooks, UserForms, or to the Excel Application
itself. We can mainly classify these events as following:

Workbook Events:

Events that linked to a particular workbook.


For examples, Open (when the workbook is opened or created) , BeforeSave (when the workbook is
about to be saved),etc…

Worksheet Events:

Events that linked to a particular worksheet.


For examples, Change (when a Cell on the sheet is changed), SelectionChange (when the user moves
selection from one the cell to another), etc…

Chart Events:

Events that linked to a particular chart.

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:

Events that linked to the application (Excel).


For examples, WorkbookBeforeClose (when any workbook is about to be closed), and SheetChange
(a cell in any open workbook is altered).

You might also like