在 VBA 中使用可变整数定义范围

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

Defining a range using a variable integer in VBA

excelvbaexcel-vba

提问by Heisenberg

So here is my problem. I know we can define a range in VBA, say for example like Range("A1:A300"). Now say I have some integer x. I want the range from A1 to Ax. How can I write this in a code? Range("A1:Ax") clearly does not work

所以这是我的问题。我知道我们可以在 VBA 中定义一个范围,例如 Range("A1:A300")。现在说我有一些整数 x。我想要从 A1 到 Ax 的范围。我怎样才能在代码中写这个?Range("A1:Ax") 显然不起作用

采纳答案by paxdiablo

That would be something like:

那将是这样的:

Range("A1:A" & CStr(x))

The CStrfunction will turn an integer into a string though I think you can actually just leave it off in later versions of Excel:

CStr函数会将整数转换为字符串,但我认为您实际上可以在更高版本的 Excel 中将其关闭:

Range("A1:A" & x)

回答by Alex K.

You can specify the anchor cell then extend:

您可以指定锚单元然后扩展:

range("A1").resize(300)