vba .End(xlDown).Row 从 Excel 2007 更改为 2010

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

.End(xlDown).Row changes from Excel 2007 to 2010

vbaexcel-vbaexcel-2010excel-2007excel

提问by woody

After updating from Office 2007 to Office 2010, the macros that worked perfectly in Excel 2007 but do not work in 2010. Specifically i recieved a error on this line:

从 Office 2007 更新到 Office 2010 后,宏在 Excel 2007 中完美运行但在 2010 中不起作用。具体来说,我在此行收到错误:

    y = Worksheets("Raw Data").Range("A2").End(xlDown).Row

Ther error is "error 6 Overflow". I have come to realize that it is due to Excel selecting the max number(1048576) of rows in excel that creates the overflow. There is only data in 975 of these rows. In 2007 it only selected the rows with data. I am wondering what has caused the change in the way that code is handled from 2007 to 2010?Has anyone else experienced this?

错误是“错误 6 溢出”。我已经意识到这是由于 Excel 选择了 excel 中的最大行数(1048576)而导致溢出。其中 975 行只有数据。在 2007 年,它只选择了有数据的行。我想知道是什么导致了从 2007 年到 2010 年代码处理方式的变化?有没有其他人经历过这种情况?

回答by Kazimierz Jawor

Be sure that y variable(and other variables referring to row numbers) are declared as Long, like:

确保y variable(和其他引用行号的变量)声明为Long,例如:

Dim y As Long

I could guess that your variables are Integerat the moment.

我可以猜到你的变量是Integer此刻。

I could guess that you have possibly migrated your file from .xlsinto .xlsmin the meantime which could cause some of that problems.

我猜您可能在此期间将您的文件迁移.xls.xlsm了其中,这可能会导致其中一些问题。

回答by Santosh

Try below

试试下面

  With Worksheets("Raw Data")
        y = .Range("A" & .Rows.Count).End(xlUp).Row
    End With