VBA 图表集源数据

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

VBA Chart setsourcedata

excel-vbachartsdatasourcevbaexcel

提问by pankaj_ar

I have to write a VBA script to define Data Source for a chart, but I am not a VBA programmer. In my workbook, I have many sheets and many sheets have many charts. In my VBA I have to define data source for every chart. For this, I want to know whether I can write in my module the below statement:

我必须编写一个 VBA 脚本来定义图表的数据源,但我不是 VBA 程序员。在我的工作簿中,我有很多工作表,很多工作表都有很多图表。在我的 VBA 中,我必须为每个图表定义数据源。为此,我想知道是否可以在我的模块中编写以下语句:

<sheet-name>.<chart-name>.SetSourceData Source ='CPU_UTIL'!$A:$A,'CPU_UTIL'!$N:$O

Whether this syntax is correct and can I use it directly?

这个语法是否正确,可以直接使用吗?

回答by bonCodigo

First of all, I am a little hesitant to answer this question since there are lots of resources out there you could find. For a basic start you can use the following code to set 'SetSourceData' source for a Chart based on B to H column data. To do this, you should already have your chart in Sheet1 and it's Chart Type is already chosen using the Chart Wizard. Otherwise you could write adding the chart, setting up the Chart Type, setting source data, formatting/colours, everything using VBA as well. (All depends on your intention given the technology required is available)

首先,我有点犹豫要不要回答这个问题,因为您可以找到很多资源。对于基本的开始,您可以使用以下代码为基于 B 到 H 列数据的图表设置“SetSourceData”源。为此,您应该已经在 Sheet1 中拥有您的图表,并且已经使用图表向导选择了它的图表类型。否则,您可以编写添加图表、设置图表类型、设置源数据、格式/颜色以及使用 VBA 的所有内容。(鉴于所需的技术可用,一切都取决于您的意图)

Sub SetMyChartSourceData()
  With Sheets("Sheets1)
    .ChartObjects(1).Chart.SetSourceData Source:= .Range("B2:H100"), PlotBy:=xlColumns
  End With
End Sub

You mentioned "VBA" Script but looking at your code, it's "VB"Script. Then you can search how to modify an existing chart using VBSCript in Excel. However, I recommend you to stick to VBA, if coding is really required.

您提到了“VBA”脚本,但查看您的代码,它是“VB”脚本。然后您可以搜索如何在 Excel 中使用 VBSCript 修改现有图表。但是,如果确实需要编码,我建议您坚持使用 VBA。

In the first place you can set source without a vba code. So Please let us know why you are intending to use VBA? Usually most questions are answered quickly. But you should wonder why yours a relatively pretty straightforward question didn't get any respond for two hours...

首先,您可以在没有 vba 代码的情况下设置源代码。所以请让我们知道您为什么打算使用 VBA?通常大多数问题都会很快得到解答。但是你应该想知道为什么你的一个相对相当简单的问题在两个小时内没有得到任何回应......

Some main(MSDN) and good references for VBA Charts in Excel:

Excel 中 VBA 图表的一些主要(MSDN)和良好参考: