连接字母和变量以创建单元格引用的 VBA 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18492452/
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
VBA code that concatenates a letter and variable to create a cell reference
提问by GeorgeCiesinski
I have a piece of code that determines the lowest row with data in it. I want to write to the cell underneath this row so that I don't overwrite old data, but I'm having trouble referencing this cell. To clarify, I already have code that determines, for example, that the 51st row is the lowest row to contain data. I need to correct my second piece of code which writes to the cell below that row:
我有一段代码可以确定包含数据的最低行。我想写入此行下方的单元格,以免覆盖旧数据,但我无法引用此单元格。为了澄清起见,我已经有代码来确定,例如,第 51 行是包含数据的最低行。我需要更正写入该行下方单元格的第二段代码:
Note: The integer "highest" is the row number lowest on the table that contains data
注意:整数“最高”是包含数据的表中最低的行号
sh1.Range(Concatenate(",'B', (highest+1),")).Value
I also tried
我也试过
sh1.Range("B" & (highest+1)
Finally, I checked several forums and they hinted that I should use indirect to create a cell reference, but I haven't had much luck with that. What would the correct way to do this be?
最后,我检查了几个论坛,他们暗示我应该使用间接来创建单元格引用,但我对此不太走运。这样做的正确方法是什么?
回答by tigeravatar
You're very close, remove the second ( from this line
你非常接近,删除第二个(从这一行
sh1.Range("B" & (highest+1)
So that it reads like this:
所以它读起来像这样:
sh1.Range("B" & highest + 1)
回答by Dave Sexton
You can also use offset:
您还可以使用偏移量:
sh1.Range("B" & highest).Offset(1,0)