简体   繁体   English

在Excel宏函数中插入饼图

[英]Insert pie chart in Excel macro function

I am new to macro function. 我是宏功能的新手。 I have been trying to insert a pie chart on the Subject_name fields (X,Y,Z) in Excel via macro function. 我一直试图通过宏功能在Excel中的Subject_name字段(X,Y,Z)上插入饼图。 The user should be able to plot this graph when clicking a macro command button. 单击宏命令按钮时,用户应该能够绘制此图形。 Basically, it should count and then use the number to plot the chart. 基本上,它应该计算,然后使用数字绘制图表。

Here is the example of current data: 以下是当前数据的示例:

Channel_Type | Channel_Category | Category | **Subject_Name**
  MICROBLOG          General         A           X
   Forum             General         A           Z
   BLOG              General         A           Y
   FORUM             General         A           X
  MICROBLOG          General         A           Z
   BLOG              General         A           Z

Here is the desired outcome: 这是期望的结果:

X : 2 | X:2 | Y : 1 | Y:1 | Z : 3 Z:3

在此输入图像描述

This is what i tried before to plot the graph for the whole sheet but i am unsure of how to just select one column: 这是我之前尝试绘制整个图表的图形,但我不确定如何选择一列:

Option Explicit

Sub Example()
Dim cht As ChartObjects
For Each cht In Worksheets(1).ChartObjects

Next cht
cht.Chart.ChartType = xlPie


End Sub

I have been stuck for awhile. 我被困了一段时间。 Appreciate any help. 感谢任何帮助。

Before you can plot a chart, you need to aggregate the data and provide the numbers that you want to plot in a chart. 在绘制图表之前,您需要聚合数据并提供要在图表中绘制的数字。 A chart does not do that automatically and no chart can plot text values. 图表不会自动执行此操作,并且图表不能绘制文本值。

There are many ways to aggregate the data into the three number values you need for the pie chart, for example a pivot table or formulas using Countif. 有许多方法可以将数据聚合为饼图所需的三个数值,例如使用Countif的数据透视表或公式。 Or you could write VBA that loops over the text values and aggregates the count into three variables. 或者您可以编写循环文本值的VBA并将计数聚合为三个变量。

Once the numbers have been generated, you can use them to create a chart. 生成数字后,您可以使用它们来创建图表。

Any of the above can be done manually or with VBA. 以上任何一种都可以手动完成或使用VBA完成。 If you use the Pivot approach, VBA may not even be required. 如果使用Pivot方法,甚至可能不需要VBA。 It depends on your circumstances. 这取决于你的情况。

Consider setting your data up in a table which includes a count column at the end. 考虑在表格中设置数据,该表格包含最后的计数列。 You can enter =1 into this (assuming each row is a unique record) and it will auto-fill down for each row. 您可以在此输入= 1(假设每行是唯一的记录),它将自动填充每一行。 Create a pivot chart (pie chart) off the 'Subject name' and 'count' fields. 在“主题名称”和“计数”字段之外创建数据透视表(饼图)。 Then you can tie the refresh of the pivottable to a command button (if you must use VBA) and the chart will update at a button push. 然后,您可以将pivottable的刷新与命令按钮绑定(如果必须使用VBA),并且图表将在按下按钮时更新。 The data being in an Excel table means it will also pick up new rows as added. Excel表中的数据意味着它还会添加新行。

You can do this completely without VBA and even have it updated automatically. 您可以在没有VBA的情况下完全执行此操作,甚至可以自动更新。

The formula in H2 is =COUNTIF(D:D,G1) and copied down up to H4. H2中的公式为=COUNTIF(D:D,G1)并向下复制到H4。 The chart was just inserted manually through menu: 图表只是通过菜单手动插入:

  • select G1:H4 选择G1:H4
  • menu => insert => chart (select 2D circle) menu => insert => chart(选择2D圆圈)
  • select the template you like 选择你喜欢的模板

This chart will update automatically if you add entries in column AD. 如果在AD列中添加条目,此图表将自动更新。

No VBA! 没有VBA!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM