vba MS Excel 2010 和 MS Excel 2013 之间的宏处理差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24319097/
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
Macro handling difference between MS Excel 2010 and MS Excel 2013
提问by clintgh
I am currently modifying a Macro that was made early 2005 and running on previous versions of Excel. Recently it is being used in MS Excel 2010, but I am using MS Excel 2013 to modify the codes. Will there be any effects or compatibility issues?
我目前正在修改 2005 年初制作并在以前版本的 Excel 上运行的宏。最近它在 MS Excel 2010 中使用,但我正在使用 MS Excel 2013 来修改代码。会不会有任何影响或兼容性问题?
A link explaining the handling of macros on different version of Excels will be very much appreciated. Thank you.
一个解释在不同版本的 Excel 上处理宏的链接将不胜感激。谢谢你。
回答by Dan Wagner
Here's one "gotcha" to be on the look-out for: with Excel 2007, Microsoft introduced the "Big Grid". This was a huge jump in the number of available rows and columns, which your users have potentially started leveraging.
这里有一个需要注意的“问题”:在 Excel 2007 中,Microsoft 引入了“Big Grid”。这是可用行和列数量的巨大飞跃,您的用户可能已经开始利用这些。
Perhaps you had a forward-thinking group writing VBA for your business in 2005, but at my old gig the following was status quo for identifying the last row:
也许您在 2005 年有一个有远见的小组为您的企业编写 VBA,但在我的旧演出中,以下是识别最后一行的现状:
LastRow = Range("A65536").End(xlUp).Row
If a 2007, 2010 or 2013 worksheet has more than 65,536 populated rows, the above is not going to return the expected result, and your old script will fail, or, worse, will return an incorrect result.
如果 2007、2010 或 2013 工作表的填充行数超过 65,536,则上述不会返回预期结果,您的旧脚本将失败,或者更糟的是,将返回不正确的结果。
You should sniff out any last row (or last column) identification like the above and refactor it using the techniques described by @SiddharthRout in the definitive post on finding the last row: Error in finding last used cell in VBA... In a nutshell:
您应该像上面一样嗅出任何最后一行(或最后一列)标识,并使用@SiddharthRout 在有关查找最后一行的权威帖子中描述的技术重构它:在 VBA 中查找最后使用的单元格时出错......简而言之:
Find the last row in a column(below looks for the last occupied row in column A on Sheet1)
查找列中的最后一行(下面查找 Sheet1 上 A 列中最后一个占用的行)
With Sheets("Sheet1")
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
Find the last row in a sheet(below uses CountA
to protect against an empty Sheet1)
查找工作表中的最后一行(以下用于CountA
防止出现空的 Sheet1)
With Sheets("Sheet1")
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lastrow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
lastrow = 1
End If
End With