本文介绍了如何将多个Range对象合并成一个,用作Chart源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试制作图表,多列作为源区域。 基本上,我想选择特定的列,我跳过一些列,并将它们全部合并到一个范围。我设置了一个循环,我创建一个范围,并将其地址附加到一个字符串,并用逗号分隔。我很确定这是Excel如何格式化。 但是,我似乎不能从这个字符串创建一个新的范围。 我希望有人能帮助我。 我非常希望避免将列复制到新的表单,只需将其全部标记为范围。 我有以下代码,用于组合范围: '每个段的循环对于Z = 1到Sheet1.txtNoSections '获取测量使用 Section =工作簿(ThisWorkbook .Name).Worksheets(1).Cells(26 + Z,6).Value '设置与根距离的距离$ var Dist =工作簿(ThisWorkbook.Name).Worksheets(1).Cells 26 + Z,3).Value '获取范围使用 Set ChartRange = ActiveSheet.Range(ActiveCell,ActiveCell.Offset(rc,Section)) RangeString = RangeString& ChartRange.AddressLocal 如果Z RangeString = RangeString& ,结束如果 下一个Z 我有然后试图获得一个这样的新范围,但没有运气。 Dim ActualRange As Range Set ActualRange = ActiveSheet.Range(RangeString) 打印RangeString时,看起来像这样: $ S $ 2 $ V $ 6181 $ S $ 2:$ X $ 6181,$ S $ 2:$ Z $ 6181,$ S $ 2:$ AB $ 6181,$ S $ 2:$ AD $ 6181,$ S $ 2:$ AF $ 6181,$ S $ 2:$ AH $ 6181,$ S $ 2:$ AJ $ 6181,$ S $ 2:$ AL $ 6181,$ S $ 2:$ 6181 AN $,$ S $ 2:$ AP $ 6181,$ S $ 2:$ AR $ 6181, $ S $ 2:$ AT $ 6181,$ S $ 2:$ AV $ 6181,$ S $ 2:$ AX $ 6181,$ S $ 2:$ AZ $ 6181,$ S $ 2:$ BB $ 6181,$ S $ 2:$ BD $ 6181,$ S $ 2:$ BF $ 6181,$ S $ 2:$ BH $ 6181,$ S $ 2:$ BJ $ 6181,$ S $ 2:$ BL $ 6181,$ S $ 2:$ BN $ 6181,$ S $ 2:$ BP $ 6181 似乎像同一个工会会做。解决方案如上述评论中所讨论的,处理这个问题的最好方法是使用本地VBA函数,例如 Union 。 你可以找到关于如何使用这些的几个参考: $ b $ 每日一次的b 剂量的Excel on vba Express 即使是更好的联盟 Chip Pearson的网站 然而,请注意,您可以回答自己的问题(甚至强烈推荐)并接受。这样,您可以与社区分享您的知识,并以您自己的代码解决问题的方式。 IMHO,这将比接受我的答案要好。 I'm trying to make a chart, with multiple columns as source area.Basically, I want to select specific columns, where I skip some columns, and merge them all into one range. I've setup a loop, where I create a range, and append it's address to a string, and seperates them with a comma. I'm pretty sure this is how Excel wants it formatted.BUT, I cannot seem to create a new range from this string.I hope someone here can help me out.I would very much like to avoid, having to copy the columns to a new sheet, and just mark it all as a range.I have the following code, for making the combined range:'Loops for each number of sectionsFor Z = 1 To Sheet1.txtNoSections 'Get gauge to use Section = Workbooks(ThisWorkbook.Name).Worksheets(1).Cells(26 + Z, 6).Value 'Sets varibel for distance from root Dist = Workbooks(ThisWorkbook.Name).Worksheets(1).Cells(26 + Z, 3).Value 'Get range to use Set ChartRange = ActiveSheet.Range(ActiveCell, ActiveCell.Offset(rc, Section)) RangeString = RangeString & ChartRange.AddressLocal If Z <> 1 Then RangeString = RangeString & "," End IfNext ZI have then tried to get a new range with something like this, but no luck.Dim ActualRange As RangeSet ActualRange = ActiveSheet.Range(RangeString)When printing the RangeString, it looks like this:$S$2$V$6181$S$2:$X$6181,$S$2:$Z$6181,$S$2:$AB$6181,$S$2:$AD$6181,$S$2:$AF$6181,$S$2:$AH$6181,$S$2:$AJ$6181,$S$2:$AL$6181,$S$2:$AN$6181,$S$2:$AP$6181,$S$2:$AR$6181,$S$2:$AT$6181,$S$2:$AV$6181,$S$2:$AX$6181,$S$2:$AZ$6181,$S$2:$BB$6181,$S$2:$BD$6181,$S$2:$BF$6181,$S$2:$BH$6181,$S$2:$BJ$6181,$S$2:$BL$6181,$S$2:$BN$6181,$S$2:$BP$6181Seems like the same union would do. 解决方案 As discussed in the comments above, the best way to handle this is to use native VBA functions such as Union.You can find several references on how to use this:on Daily dose of Excelon vba Expresseven a "better" Union on Chip Pearson's websiteYet, please note that you can answer you own question (it is even highly recommended) and accept it. This way, you can share your knowledge with the community and the way you've solved your issue with your own code.IMHO, this would be even better than accepting my answer. 这篇关于如何将多个Range对象合并成一个,用作Chart源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-18 22:52