vba SeriesCollection.Name 到范围?电子表格

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

SeriesCollection.Name to Range? Excel

excelvbaexcel-vba

提问by Lee Jacobs

I'm attempting to get a proper legend from my excel sheet and I'm attempting to create the legend using the

我正在尝试从我的 excel 表中获取正确的图例,并且我正在尝试使用

SeriesCollection(1).Name

Method in VBA. What I'm doing looks like this

VBA中的方法。我在做什么看起来像这样

ActiveChart.SeriesCollection(1).Name = Range(" some range ").Text

This however gives me the error of Type Mismatch. Any ideas on what the issue is? Or how I can go about doing this a different way?

然而,这给了我类型不匹配的错误。关于问题是什么的任何想法?或者我怎样才能以不同的方式做到这一点?

回答by martin

Is the range you refer to only one cell? In any case, you can write:

您所指的范围是否仅指一个单元格?在任何情况下,你都可以写:

ActiveChart.SeriesCollection(1).Name = Range("RangeName").Cells(1, 1).Value

回答by David Zemens

If you're referencing an array (range of multiple cells) you'll have to loop through the array and concatenate the individual cell values into a single string variable, and then use that string variable to set the ActiveChart.SeriesCollection(1).Name

如果要引用数组(多个单元格的范围),则必须遍历数组并将各个单元格值连接到单个字符串变量中,然后使用该字符串变量设置 ActiveChart.SeriesCollection(1).Name

Dim myString as String  'Declare a string variable to concatenate the range values
Dim rng as Range        'This is your range variable
Dim cell as Range       'this will be a cell range within rng

Set rng = Range(" some range ")
For each cell in rng
    myString = myString & cell.Value
Next

ActiveChart.SeriesCollection(1).Name = myString