用于更改数据透视图类型的 Excel VBA 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16561032/
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 21:07:10  来源:igfitidea点击:

Excel VBA code to change pivot chart type

vbaexcel-vbagraphdashboardexcel-2013

提问by Charlie

I'm looking for a code to create a button which upon clicking will change the pivot table chart from a bar chart to a line graph. I want the button to be at the bottom of the graph so the user can choose how they can see the data at the click of a button.

我正在寻找一个代码来创建一个按钮,单击该按钮会将数据透视表图表从条形图更改为折线图。我希望该按钮位于图表的底部,以便用户可以通过单击按钮选择如何查看数据。

Does anyone know the code for this? Ideally a plug and play solution as I'm a beginner to VBA

有谁知道这个代码?理想的即插即用解决方案,因为我是 VBA 的初学者

Thanks

谢谢

回答by Skip Intro

You had two sub routines, but only one end:

你有两个子例程,但只有一个结束:

Private Sub CommandButton20_Click()

  ActiveSheet.ChartObjects("Chart 60").Activate
  ActiveChart.ChartType = xlLine

End Sub

Works for me.

对我来说有效。

You can also assign a macro to a chart so something like:

您还可以将宏分配给图表,例如:

Sub ChangeMe()

ActiveSheet.ChartObjects("Chart 60").Activate

    If ActiveChart.ChartType = xlLine Then
            ActiveChart.ChartType = xlBarClustered
                Else
                ActiveChart.ChartType = xlLine
    End If

End Sub

might be worth investigating.

可能值得研究。