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

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

问题描述

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



我需要选择一个范围的数据在一张纸上(例如 f10:i14 ),然后将其设置为一个图表的来源



一段代码工作



ActiveChart.SetSourceData源:=表格(每周趋势)范围(f10,i14)



但这不会



ActiveChart.SetSourceData Source:=表格(每周趋势)范围(单元格(x,y),单元格(k,z))



code> 1004错误



但我需要根据变量索引指定范围用于我的报告目的,请帮助

解决方案

您必须完全限定 .Cells 对象



尝试此



ActiveChart.SetSourceData源:=表格(每周趋势)范围每周趋势)。细胞(x,y),表格(每周趋势)细胞(k,Z))



 使用表格(每周趋势)
ActiveChart.SetSourceData Source:= .Range(.Cells(x,y),.Cells(k,Z))
结束于


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

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

The following piece of code works

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

but this does not

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

it returns a 1004 error

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

解决方案

You have to fully qualify the .Cells object

Try this

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天全站免登陆