在 VBA 中根据可变单元格范围设置图表源数据 [英] Setting a chart source data based on a variable cell range in VBA

查看:17
本文介绍了在 VBA 中根据可变单元格范围设置图表源数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个报告模板,其中的一部分将生成结构类似于移动平均线的数据图表

I am creating a report template part of which will be generating data charts with a structure similar to a moving average

为此,我需要在一张纸中选择一系列数据(例如 f10:i14)并将其放置并设置为一个图表的来源

for this I need to select a range of data in one sheet (e.g. f10:i14) and place and set it as the source of one chart

以下代码有效

ActiveChart.SetSourceData Source:=Sheets("Weekly Trends").Range("f10,i14")

但这没有

ActiveChart.SetSourceData Source:=Sheets("Weekly Trends").Range(Cells(x, y), Cells(k, z))

它返回一个 1004 错误

但是为了我的报告目的,我需要根据变量索引指定范围,请帮忙

But I need to specify the range based on variable indices for my report purposes, please help

推荐答案

您必须完全限定 .Cells 对象

You have to fully qualify the .Cells object

试试这个

ActiveChart.SetSourceData Source:=Sheets("Weekly Trends").Range(Sheets("Weekly Trends").Cells(x, y), Sheets("Weekly Trends").Cells(k, Z)))

你也可以把上面的代码变短

You can also make the above code short

With Sheets("Weekly Trends")
    ActiveChart.SetSourceData Source:=.Range(.Cells(x, y), .Cells(k, Z))
End With

这篇关于在 VBA 中根据可变单元格范围设置图表源数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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