VBA图表setsourcedata [英] VBA Chart setsourcedata

查看:479
本文介绍了VBA图表setsourcedata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

此语法是否正确,我可以直接使用吗?

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

推荐答案

首先总而言之,我有点犹豫要回答这个问题,因为您可以找到很多资源。首先,您可以使用以下代码根据B到H列数据为图表设置 SetSourceData源。为此,您应该已经将图表放在Sheet1中,并且已经使用图表向导选择了图表类型。否则,您也可以使用VBA编写添加图表,设置图表类型,设置源数据,格式/颜色以及所有内容。 (所有内容都取决于您的意图,因为可以使用所需的技术)

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)

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

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

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代码的源代码。请让我们知道您为什么打算使用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...

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

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

我最喜欢的 Excel图表博客:

这篇关于VBA图表setsourcedata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆