Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse VBA code to make a chart in Excel easily
DescriptionThis example shows how to Use VBA code to make a chart in Excel easily. To make a chart in Excel, you need to write the data into a worksheet and then build a chart that uses the data as a data source. This example uses a VBA subroutine to build a chart easily in code.
KeywordsExcel, Office, chart, VBA
CategoriesOffice, Graphics
 
To make a chart in Excel, you need to write the data into a worksheet and then build a chart that uses the data as a data source. This example uses a VBA subroutine to build a chart easily in code.

The MakeGraph subroutine takes as a parameter an array of values and some optional titles. It creates a new worksheet and writes the values onto it. It then makes a chart that uses the values. It finishes by setting the chart's title and axis labels if they were passed as parameters.

 
Public Sub MakeGraph(values() As Single, Optional ByVal _
    title As String = "", Optional ByVal x_label As String _
    = "", Optional ByVal y_label As String = "")
Dim work_book As Workbook
Dim last_sheet As Worksheet
Dim new_sheet As Worksheet
Dim r As Integer
Dim new_chart As Chart
Dim chart_shape As Shape

    ' Make a new worksheet.
    Set work_book = Application.ActiveWorkbook
    Set last_sheet = _
        work_book.Sheets(work_book.Sheets.Count)
    Set new_sheet = work_book.Sheets.Add(after:=last_sheet)
    new_sheet.Name = "New Chart"

    ' Write the data onto it.
    For r = 1 To UBound(values)
        new_sheet.Cells(r, 1) = values(r)
    Next r

    ' Make the chart.
    Set new_chart = Charts.Add()
    ActiveChart.ChartType = xlLineMarkers
    ActiveChart.SetSourceData _
        Source:=new_sheet.Range("A1:A" & UBound(values)), _
        PlotBy:=xlColumns
    ActiveChart.Location Where:=xlLocationAsObject, _
        Name:="New Chart"

    ' Set the chart's title abd axis labels.
    With ActiveChart
        If Len(title) = 0 Then
            .HasTitle = False
        Else
            .HasTitle = True
            .ChartTitle.Characters.Text = title
        End If

        If Len(x_label) = 0 Then
            .Axes(xlCategory, xlPrimary).HasTitle = False
        Else
            .Axes(xlCategory, xlPrimary).HasTitle = True
            .Axes(xlCategory, _
                xlPrimary).AxisTitle.Characters.Text = _
                x_label
        End If

        If Len(y_label) = 0 Then
            .Axes(xlValue, xlPrimary).HasTitle = False
        Else
            .Axes(xlValue, xlPrimary).HasTitle = True
            .Axes(xlValue, _
                xlPrimary).AxisTitle.Characters.Text = _
                y_label
        End If
    End With

    ' Make it bigger.
    Set chart_shape = _
        new_sheet.Shapes(new_sheet.Shapes.Count)
    With chart_shape
        .ScaleWidth 1.31, msoFalse, msoScaleFromBottomRight
        .ScaleHeight 1.47, msoFalse, msoScaleFromBottomRight
        .ScaleWidth 1.18, msoFalse, msoScaleFromTopLeft
        .ScaleHeight 1.11, msoFalse, msoScaleFromTopLeft
    End With
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated