VBAチャート| VBAコードを使用してグラフを追加する例

ExcelVBAチャート

チャートはVBAのオブジェクトと呼ぶことができます。ワークシートと同様に、同じ方法でVBAにチャートを挿入することもできます。最初に、出力データに必要なデータとチャートタイプを選択します。次に、2つの異なるタイプのチャートを提供します。は、グラフが同じデータシートにある埋め込みグラフであり、別のグラフは、グラフがデータの別のシートにあるグラフシートと呼ばれます。

データ分析では、視覚効果は分析を行った人の主要業績評価指標です。ビジュアルは、アナリストがメッセージを伝えるための最善の方法です。私たちは皆Excelユーザーであるため、通常、データの分析にかなりの時間を費やし、数値とグラフで結論を導き出します。チャートの作成は習得するための芸術であり、Excelを使用してチャートを作成するための十分な知識があることを願っています。この記事では、VBAコーディングを使用してグラフを作成する方法を紹介します。

ExcelでVBAコードを使用してグラフを追加する方法は?

このVBAチャートExcelテンプレートはここからダウンロードできます–VBAチャートExcelテンプレート

#1-VBAコーディングを使用してグラフを作成する

グラフを作成するには、ある種の数値データが必要です。この例では、以下のサンプルデータを使用します。

では、VBAエディターにジャンプしましょう。

ステップ1:サブプロシージャを開始します。

コード:

 Sub Charts_Example1()End Sub 

ステップ2:変数をチャートとして定義します。

コード:

 Sub Charts_Example1()Dim MyChart As Chart End Sub 

ステップ3:チャートはオブジェクト変数なので、設定する必要があります。

コード:

 Sub Charts_Example1()Dim MyChart As Chart Set MyChart = Charts.Add End Sub 

上記のコードは、ワークシートとしてではなく、チャートシートとして新しいシートを追加します。

ステップ4:次に、グラフを設計する必要があります。ステートメントで開きます。

コード:

 Sub Charts_Example1()Dim MyChart As Chart Set MyChart = Charts.Add With MyChart End With End Sub 

ステップ5: チャートで最初に行う必要があるのは、「ソースデータの設定」メソッドを選択してソース範囲を設定することです。

コード:

 Sub Charts_Example1()Dim MyChart As Chart Set MyChart = Charts.Add With MyChart .SetSourceData End With End Sub 

ステップ6:ここでソース範囲について言及する必要があります。この場合、私のソース範囲は「Sheet1」という名前のシートにあり、範囲は「A1からB7」です。

コード:

 Sub Charts_Example1()Dim MyChart As Chart Set MyChart = Charts.Add With MyChart .SetSourceData Sheets( "Sheet1")。Range( "A1:B7")End With End Sub 

ステップ7:次に、作成するグラフの種類を選択する必要があります。このために、ChartTypeプロパティを選択する必要があります。

コード:

 Sub Charts_Example1()Dim MyChart As Chart Set MyChart = Charts.Add With MyChart .SetSourceData Sheets( "Sheet1")。Range( "A1:B7")。ChartType = End With End Sub 

Step 8: Here we have a variety of charts. I am going to select the “xlColumnClustered” chart.

Code:

 Sub Charts_Example1() Dim MyChart As Chart Set MyChart = Charts.Add With MyChart .SetSourceData Sheets("Sheet1").Range("A1:B7") .ChartType = xlColumnClustered End With End Sub 

Ok, at this moment let’s run the code using F5 key or manually and see how the chart looks.

Step 9: Now change other properties of the chart. To change the chart title below is the code.

Like this, we have many properties and methods with charts. Use each one of them to see the impact and learn.

 Sub Charts_Example1() Dim MyChart As Chart Set MyChart = Charts.Add With MyChart .SetSourceData Sheets("Sheet1").Range("A1:B7") .ChartType = xlColumnClustered .ChartTitle.Text = "Sales Performance" End With End Sub 

#2 – Create a Chart with Same Excel Sheet as Shape

To create the chart with the same worksheet (datasheet) as shape we need to use a different technique.

Step 1: First Declare threes Object Variables.

Code:

 Sub Charts_Example2() Dim Ws As Worksheet Dim Rng As Range Dim MyChart As Object End Sub 

Step 2: Then Set the Worksheet reference.

Code:

 Sub Charts_Example2() Dim Ws As Worksheet Dim Rng As Range Dim MyChart As Object Set Ws = Worksheets("Sheet1") End Sub 

Step 3: Now set the range object in VBA

Code:

 Sub Charts_Example2() Dim Ws As Worksheet Dim Rng As Range Dim MyChart As Object Set Ws = Worksheets("Sheet1") Set Rng = Ws.Range("A1:B7") End Sub 

Step 4: Now set the chart object.

Code:

 Sub Charts_Example2() Dim Ws As Worksheet Dim Rng As Range Dim MyChart As Object Set Ws = Worksheets("Sheet1") Set Rng = Ws.Range("A1:B7") Set MyChart = Ws.Shapes.AddChart2 End Sub 

Step 5: Now, as usual, we can design the chart by using the “With” statement.

Code:

 Sub Charts_Example2() Dim Ws As Worksheet 'To Hold Worksheet Reference Dim Rng As Range 'To Hold Range Reference in the Worksheet Dim MyChart As Object Set Ws = Worksheets("Sheet1") 'Now variable "Ws" is equal to the sheet "Sheet1" Set Rng = Ws.Range("A1:B7") 'Now variable "Rng" holds the range A1 to B7 in the sheet "Sheet1" Set MyChart = Ws.Shapes.AddChart2 'Chart will be added as Shape in the same worksheet With MyChart.Chart .SetSourceData Rng 'Since we already set the range of cells to be used for chart we have use RNG object here .ChartType = xlColumnClustered .ChartTitle.Text = "Sales Performance" End With End Sub 

This will add the chart below.

#3 – Code to Loop through the Charts

Like how we look through sheets to change the name or insert values, hide & unhide them. Similarly to loop through the charts we need to use chart object property.

The below code will loop through all the charts in the worksheet.

Code:

 Sub Chart_Loop() Dim MyChart As ChartObject For Each MyChart In ActiveSheet.ChartObjects 'Enter the code here Next MyChart End Sub 

#4 – Alternative Method to Create Chart

We can use the below alternative method to create charts. We can use the Chart Object. Add method to create the chart below is the example code.

This will also create a chart like the previous method.

Code:

 Sub Charts_Example3() Dim Ws As Worksheet Dim Rng As Range Dim MyChart As ChartObject Set Ws = Worksheets("Sheet1") Set Rng = Ws.Range("A1:B7") Set MyChart = Ws.ChartObjects.Add(Left:=ActiveCell.Left, Width:=400, Top:=ActiveCell.Top, Height:=200) MyChart.Chart.SetSourceData Source:=Rng MyChart.Chart.ChartType = xlColumnStacked MyChart.Chart.ChartTitle.Text = "Sales Performance" End Sub