แผนภูมิ VBA | ตัวอย่างการเพิ่มแผนภูมิโดยใช้รหัส VBA

แผนภูมิ Excel VBA

แผนภูมิสามารถเรียกได้ว่าเป็นวัตถุใน VBA เช่นเดียวกับแผ่นงานที่เราสามารถแทรกแผนภูมิใน VBA ได้ในลักษณะเดียวกันอันดับแรกเราเลือกข้อมูลและประเภทแผนภูมิที่เราต้องการสำหรับข้อมูลออกตอนนี้มีแผนภูมิสองประเภทที่แตกต่างกันที่เรามีให้ เป็นแผนภูมิฝังที่แผนภูมิอยู่ในแผ่นข้อมูลเดียวกันและอีกแผ่นหนึ่งเรียกว่าแผ่นงานแผนภูมิโดยที่แผนภูมิอยู่ในแผ่นข้อมูลแยกต่างหาก

ในการวิเคราะห์ข้อมูลวิชวลเอฟเฟกต์เป็นตัวบ่งชี้ประสิทธิภาพหลักของผู้ที่ทำการวิเคราะห์ ภาพเป็นวิธีที่ดีที่สุดที่นักวิเคราะห์สามารถถ่ายทอดข้อความของตนได้ เนื่องจากเราทุกคนเป็นผู้ใช้ excel เรามักจะใช้เวลาส่วนใหญ่ในการวิเคราะห์ข้อมูลและสรุปผลด้วยตัวเลขและแผนภูมิ การสร้างแผนภูมิเป็นศิลปะที่ต้องเชี่ยวชาญและฉันหวังว่าคุณจะมีความรู้ที่ดีในการสร้างแผนภูมิด้วย excel ในบทความนี้เราจะแสดงวิธีสร้างแผนภูมิโดยใช้การเข้ารหัส VBA

วิธีเพิ่มแผนภูมิโดยใช้รหัส VBA ใน Excel

คุณสามารถดาวน์โหลดเทมเพลต Excel VBA Charts ได้ที่นี่ - เทมเพลต Excel ของแผนภูมิ VBA

# 1 - สร้างแผนภูมิโดยใช้การเข้ารหัส VBA

ในการสร้างแผนภูมิใด ๆ เราควรมีข้อมูลตัวเลขบางประเภท สำหรับตัวอย่างนี้ฉันจะใช้ข้อมูลตัวอย่างด้านล่าง

โอเคข้ามไปที่ตัวแก้ไข VBA

ขั้นตอนที่ 1:เริ่มขั้นตอนย่อย

รหัส:

 Sub Charts_Example1 () End Sub 

ขั้นตอนที่ 2:กำหนดตัวแปรเป็นแผนภูมิ

รหัส:

 Sub Charts_Example1 () Dim MyChart เป็น Chart End Sub 

ขั้นตอนที่ 3:ตั้งแต่แผนภูมิเป็นตัวแปรวัตถุที่เราต้องตั้งมัน

รหัส:

 Sub Charts_Example1 () Dim MyChart As Chart Set MyChart = Charts เพิ่ม End Sub 

รหัสด้านบนจะเพิ่มแผ่นงานใหม่เป็นแผ่นงานแผนภูมิไม่ใช่แผ่นงาน

ขั้นตอนที่ 4:ตอนนี้เราต้องออกแบบแผนภูมิ เปิดด้วยคำชี้แจง

รหัส:

 Sub Charts_Example1 () Dim MyChart As Chart Set MyChart = Charts เพิ่มด้วย MyChart End ด้วย End Sub 

ขั้นตอนที่ 5: ครั้งแรก  สิ่งที่มีแผนภูมิที่เราต้องทำคือการตั้งค่าช่วงแหล่งที่มาโดยการเลือก“ชุดแหล่งข้อมูล”วิธีการ

รหัส:

 Sub Charts_Example1 () Dim MyChart As Chart Set MyChart = Charts เพิ่มด้วย MyChart.SetSourceData ลงท้ายด้วย End Sub 

ขั้นตอนที่ 6: ที่นี่เราต้องพูดถึงช่วงแหล่งที่มา ในกรณีนี้ช่วงซอร์สของฉันอยู่ในชีตที่ชื่อว่า "Sheet1" และช่วงคือ "A1 ถึง B7"

รหัส:

 Sub Charts_Example1 () Dim MyChart As Chart Set MyChart = Charts.Add With MyChart.SetSourceData Sheets ("Sheet1") ช่วง ("A1: B7") ปิดท้ายด้วย End Sub 

ขั้นตอนที่ 7:ถัดไปเราต้องเลือกประเภทของแผนภูมิที่เราจะสร้าง สำหรับสิ่งนี้เราต้องเลือกคุณสมบัติประเภทแผนภูมิ

รหัส:

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

ขั้นตอนที่ 8: ที่นี่เรามีแผนภูมิที่หลากหลาย ฉันจะเลือกแผนภูมิ“ xlColumnClustered

รหัส:

 Sub Charts_Example1 () Dim MyChart As Chart Set MyChart = Charts.Add With MyChart.SetSourceData Sheets ("Sheet1") ช่วง ("A1: B7") .ChartType = xlColumnClustered ปิดท้ายด้วย End Sub 

ตกลงในขณะนี้ให้เรียกใช้รหัสโดยใช้ปุ่ม F5 หรือด้วยตนเองและดูว่าแผนภูมิมีลักษณะอย่างไร

ขั้นตอนที่ 9:ตอนนี้เปลี่ยนคุณสมบัติอื่น ๆ ของแผนภูมิ ในการเปลี่ยนชื่อแผนภูมิด้านล่างคือรหัส

เช่นนี้เรามีคุณสมบัติและวิธีการมากมายพร้อมแผนภูมิ ใช้แต่ละรายการเพื่อดูผลกระทบและเรียนรู้

 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