无法设置dynamic范围没有错误

我试图select一个macros的范围,但我不断收到

“编译错误:”

“预期:列表分隔符或)”

我只是试图设置一个dynamic范围使用我之前创build的variables

With Sheets("Recon") rownum3 = rownum2 + 2 rownum4 = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row set r3 = .range("K"&rownum3&":K"&rownum4) 

这给了我错误

提前致谢

听起来很愚蠢,你需要在你的范围内添加空格(也可以添加end with

 With Sheets("Recon") rownum3 = rownum2 + 2 rownum4 = .UsedRange.SpecialCells(xlCellTypeLastCell).Row set r3 = .range("K" & rownum3 & ":K" & rownum4) end with 

出于某种原因,使用variables时,即使用&分开它们,VBA也不喜欢引号旁边的variables。 只需在您的&连接器之间添加一个空间,它应该可以正常工作。

解决这个非自动格式问题的一种方法是使用Range(cells(),cells())

Set r3 =.Range(.Cells(rownum3,11),.cells(rownum4,11))

Cells()是Cells([row #],[col. #]) ,所以把它包裹在Range()是另一种方法(我个人喜欢的方式)。