VBA diagrammas - Piemēri diagrammas pievienošanai, izmantojot VBA kodu

Satura rādītājs

Excel VBA diagrammas

Diagrammas var saukt par objektiem VBA, līdzīgi kā darblapā, mēs varam arī ievietot diagrammas VBA tādā pašā veidā, vispirms mēs izvēlamies datus un diagrammas veidu, kuru vēlaties iegūt datiem, tagad ir divi dažādi diagrammu veidi, kurus mēs piedāvājam ir iegultā diagramma, kur diagramma atrodas tajā pašā datu lapā, un vēl viena ir pazīstama kā diagrammas lapa, kur diagramma atrodas atsevišķā datu lapā.

Datu analīzē vizuālie efekti ir galvenie rādītāji personai, kura veikusi analīzi. Vizuālais materiāls ir labākais iespējamais veids, kā analītiķis var nodot savu vēstījumu. Tā kā mēs visi esam Excel lietotāji, mēs parasti pavadām daudz laika, analizējot datus un izdarot secinājumus ar skaitļiem un diagrammām. Diagrammas izveide ir māksla, kas jāapgūst, un es ceru, ka jums ir labas zināšanas par diagrammu izveidošanu ar Excel. Šajā rakstā mēs parādīsim, kā izveidot diagrammas, izmantojot VBA kodēšanu.

Kā pievienot diagrammas, izmantojot VBA kodu programmā Excel?

# 1 - izveidojiet diagrammu, izmantojot VBA kodēšanu

Lai izveidotu jebkuru diagrammu, mums vajadzētu būt kaut kādiem skaitliskiem datiem. Šajā piemērā es izmantošu tālāk minētos datu paraugus.

Labi, pārejam pie VBA redaktora.

1. darbība. Sāciet apakšprocedūru.

Kods:

Apakšgrupas_piemērs1 () Beigu apakšgrupa

2. darbība: definējiet mainīgo kā diagrammu.

Kods:

Apakšdiagrammas_piemērs1 () Aptumšojiet diagrammu kā diagrammas beigu apakšgrupa

3. solis: Tā kā diagramma ir objekta mainīgais, mums tas ir jāiestata .

Kods:

Apakšdiagrammas_piemērs1 () Aptumšojiet MyChart kā diagrammu, iestatiet MyChart = Charts.Add End Sub

Iepriekš minētais kods pievienos jaunu lapu kā diagrammas lapu, nevis kā darblapu.

4. solis: Tagad mums jāveido diagramma. Atvērt ar paziņojumu.

Kods:

Apakšdiagrammas_piemērs1 () Aptumšojiet MyChart kā diagrammu, iestatiet MyChart = Charts. Pievienojiet ar MyChart End ar End Sub

5. darbība. Pirmā lieta ar diagrammu, kas mums jādara, ir iestatīt avota diapazonu, atlasot metodi “Iestatīt avota datus” .

Kods:

Apakšdiagrammas_piemērs1 () Aptumšot MyChart kā diagrammu Iestatiet MyChart = Charts.Pievienot ar MyChart .SetSourceData Beigas ar End Sub

6. solis: Šeit mums jāpiemin avotu diapazons. Šajā gadījumā mans avota diapazons ir lapā ar nosaukumu “Sheet1”, un diapazons ir “A1 līdz B7”.

Kods:

Apakšdiagrammas_piemērs1 () Aptumšojiet MyChart kā diagrammu kopu MyChart = Charts.Add With MyChart .SetSourceData Sheets ("Sheet1"). Diapazons ("A1: B7") Beigās Ar End Sub

7. solis: Tālāk mums jāizvēlas veida diagramma, kuru mēs izveidosim. Lai to izdarītu, mums jāizvēlas rekvizīts Chart Type .

Kods:

Apakšdiagrammas_piemērs1 () Aptumšojiet MyChart kā diagrammu kopu MyChart = Charts.Add With MyChart .SetSourceData Sheets ("Sheet1"). Diapazons ("A1: B7") .ChartType = Beigt ar End Sub

8. solis: Šeit mums ir dažādas diagrammas. Es izvēlēšos diagrammu “ xlColumnClustered ”.

Kods:

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 the 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 the 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:

Apakšdiagrammas_piemērs3 () Dim Ws kā darblapa Dim Rng kā diapazons Dim MyChart As ChartObject Set Ws = darblapas ("Sheet1") iestatīt Rng = Ws.Range ("A1: B7") iestatīt MyChart = Ws.ChartObjects.Add (pa kreisi: = ActiveCell.Left, platums: = 400, augšējais: = ActiveCell.Top, augstums: = 200) MyChart.Chart.SetSourceData Source: = Rng MyChart.Chart.ChartType = xlColumnStacked MyChart.Chart.ChartTitle.Text = "Pārdošanas veiktspēja" Apakšgrupa

Interesanti raksti...