vba EXCEL Graphs - 动态更改图表类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10815487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:20:12  来源:igfitidea点击:

EXCEL Graphs - change the graph TYPE dynamically

excelexcel-vbagraphvba

提问by Vivek Chandra

Wanted a way to change a graph type dynamically( without using VB script, is it even possible?) - say from bar to line or pie.

想要一种动态更改图形类型的方法(不使用 VB 脚本,甚至可能吗?) - 比如说从条形到线形或饼形。

this will be triggered when a user clicks on a radio button or a drop down or any other way.

这将在用户单击单选按钮或下拉菜单或任何其他方式时触发。

Beginner,Kindly help

初学者,请帮忙

回答by Jon Crowell

The best thing to do is create a simple chart, record a macro, and change the chart to several different types. Look at the recorded macro and you'll see the code you need to change the chart dynamically.

最好的办法是创建一个简单的图表,录制一个宏,然后将图表更改为几种不同的类型。查看录制的宏,您将看到动态更改图表所需的代码。

Here are a couple of examples that change the chart type, assuming your chart is called "Chart 1":

以下是一些更改图表类型的示例,假设您的图表称为“图表 1”:

Sub ApplyPieChart()
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.ChartType = xlPie
End Sub

Sub ApplyBarChart()
    ActiveSheet.ChartObjects("Chart 1").Activate
    ActiveChart.ChartType = xlBarClustered
End Sub

You can then assign these macros to a button, a hyperlink, or whatever else you want.

然后,您可以将这些宏分配给按钮、超链接或您想要的任何其他内容。

Note that you can't change chart types with formulas. You will need to use VBA.

请注意,您无法使用公式更改图表类型。您将需要使用 VBA。