תרשימי VBA | דוגמאות להוספת תרשים באמצעות קוד VBA

תרשימי VBA של Excel

ניתן לכנות תרשימים כאובייקטים ב- VBA, בדומה לגליון העבודה אנו יכולים גם להכניס תרשימים ב- VBA באותו אופן, ראשית אנו בוחרים את הנתונים ואת סוג התרשים שאנו רוצים עבור נתונים מחוץ, כעת ישנם שני סוגים שונים של תרשימים שאנו מספקים אחד הוא תרשים ההטמעה שבו התרשים נמצא באותו גיליון נתונים ונתון אחר מכונה גיליון התרשים שבו התרשים נמצא בגיליון הנפרד של הנתונים.

בניתוח נתונים, אפקטים חזותיים הם מדדי הביצוע העיקריים של האדם שביצע את הניתוח. ויזואליה היא הדרך הטובה ביותר האפשרית שאנליסט יכול להעביר את המסר שלו. מכיוון שכולנו משתמשים מצוינים אנו בדרך כלל משקיעים זמן ניכר בניתוח הנתונים ומסיקים מסקנות עם מספרים ותרשימים. יצירת תרשים היא האמנות לשלוט בה ואני מקווה שיש לך ידע טוב ביצירת תרשימים עם Excel. במאמר זה נראה לכם כיצד ליצור תרשימים באמצעות קידוד VBA.

כיצד להוסיף תרשימים באמצעות קוד VBA ב- Excel?

תוכל להוריד תבנית Excel זו של תרשימי VBA כאן - תבנית Excel של תרשימי VBA

מס '1 - צור תרשים באמצעות קידוד VBA

כדי ליצור כל תרשים עלינו לקבל איזושהי נתונים מספריים. לדוגמא זו, אשתמש בנתוני הדוגמה הבאים.

אוקי, בוא נקפוץ לעורך VBA.

שלב 1: התחל הליך משנה.

קוד:

 תרשימי משנה_דוגמה 1 () סיום משנה 

שלב 2: הגדר את המשתנה כתרשים.

קוד:

 תרשימי משנה_דוגמה 1 () עמעום את MyChart כסיום תרשים 

שלב 3: מכיוון שהתרשים הוא משתנה אובייקט עלינו להגדיר אותו.

קוד:

 תרשימי משנה_דוגמה 1 () עמעום את MyChart כתרשים הגדר MyChart = תרשימים. הוסף משנה משנה 

הקוד שלעיל יוסיף גיליון חדש כגיליון תרשים, ולא כגליון עבודה.

שלב 4: כעת עלינו לעצב את התרשים. פתח עם הצהרה.

קוד:

 תרשימי משנה_דוגמה 1 () עמעום את MyChart כתרשים הגדר MyChart = תרשימים. הוסף עם MyChart End עם Sub End 

שלב 5:  הדבר הראשון עם התרשים שעלינו לעשות הוא להגדיר את טווח המקור על ידי בחירה בשיטת "הגדר נתוני מקור" .

קוד:

 תרשימי משנה_דוגמה 1 () עמעום את MyChart כתרשים קבע MyChart = תרשימים. הוסף עם MyChart. SetSourceData סוף עם סוף סוף 

שלב 6: כאן עלינו להזכיר את טווח המקור. במקרה זה, טווח המקור שלי נמצא בגיליון הנקרא "Sheet1" והטווח הוא "A1 עד B7".

קוד:

 תרשימי משנה_דוגמה 1 () עמעום את MyChart כתרשים הגדר MyChart = תרשימים. הוסף באמצעות MyChart. SheetsSetSourceData ("Sheet1"). טווח ("A1: B7") סיום עם Sub סוף 

שלב 7: בשלב הבא עלינו לבחור את סוג התרשים שאנחנו הולכים ליצור. לשם כך, עלינו לבחור במאפיין סוג תרשים .

קוד:

 תרשימי משנה_דוגמה 1 () עמעום MyChart כתרשים הגדר MyChart = תרשימים. הוסף באמצעות MyChart. SheetsSetSourceData ("Sheet1"). טווח ("A1: B7") .ChartType = סוף עם סוף סוף 

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