在 VBA 中为范围内的行(行,列)使用变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41004389/
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
Using a variable for row in (row,column) for a range in VBA
提问by aquarules
I have a global variable called processRowBegin that contains the row number for the beginning of a process. I also have a global variable called processRowEnd that contains the row number for the end of a process.
我有一个名为 processRowBegin 的全局变量,它包含进程开始的行号。我还有一个名为 processRowEnd 的全局变量,它包含进程结束的行号。
What I am trying to do is create a chart for two columns, Columns C and E on the active worksheet, where the range for the selected rows to be plotted is from processRowBegin to processRowEnd. The problem that I am coming across is that I do not know how to use a range with a variable for the row in (row, column) syntax. I have researched this question and so far have not found a solution that works. I tried using the (row,column) syntax at first with the variable names for the row in (row, column). This did not work and resulted in the error in the picture [Using variable for row in range error][1}.
我想要做的是为活动工作表上的两列(C 列和 E 列)创建一个图表,其中要绘制的选定行的范围是从 processRowBegin 到 processRowEnd。我遇到的问题是我不知道如何使用带有变量的范围作为(行,列)语法中的行。我已经研究过这个问题,到目前为止还没有找到有效的解决方案。我首先尝试使用 (row,column) 语法和 (row, column) 中行的变量名称。这不起作用并导致图片中的错误 [Using variable for row in range error][1}。
I then tried to implement a solution (code below) that I found via another user's question but still receive the same error. How does one go about doing this in VBA?
然后我尝试实施我通过另一个用户的问题找到的解决方案(下面的代码),但仍然收到相同的错误。如何在 VBA 中做到这一点?
Range(("C" & processRowBegin):("C" & processRowEnd)).Select
Range(("E" & processRowBegin):("E" & processRowEnd)).Select
I am a beginner in Excel VBA as well as programming and would appreciate help. This is my last step to complete an important work project. If anyone would like to view my entire code, please let me know.
我是 Excel VBA 和编程的初学者,希望得到帮助。这是我完成一个重要工作项目的最后一步。如果有人想查看我的完整代码,请告诉我。
回答by Scott Craner
You would do it this way:
你会这样做:
Range("C" & processRowBegin & ":C" & processRowEnd).Select
Range takes either two cells or a string to denote the extents.
Range 采用两个单元格或一个字符串来表示范围。
To do it by the two cells:
要通过两个单元格来做到这一点:
Range(Cells(processRowBegin,3),Cells(processRowEnd,3)).Select
A couple notes:
一些注意事项:
One should avoid using select and simply do what is desired with the cells. For more information on how to avoid it see this POST
One should always qualify all all range objects to their parent sheets. If using the Cells() they too need to be qualified
人们应该避免使用 select 并简单地对单元格执行所需的操作。有关如何避免它的更多信息,请参阅此POST
应该始终将所有范围对象限定为它们的父工作表。如果使用 Cells() 他们也需要被限定
Example
例子
With WorkSheets("Sheet1")
.Range(.Cells(processRowBegin,3),.Cells(processRowEnd,3)).Value = .Range(.Cells(processRowBegin,5),.Cells(processRowEnd,5)).Value
End With