Excel 2010 Redim Preserve崩溃 [英] Excel 2010 Redim Preserve crash
问题描述
通过在全新工作簿中的IDE中输入以下内容,我能够以某种方式使Excel 2010崩溃:
I am somehow able to crash Excel 2010 by entering the following into the IDE in a brand new workbook:
private sub foo
redim v(,1 to 3)
好吧,您不太可能输入... ...但这是实际发生的情况:
OK, you're not likely to type that ... but here's what really happened:
private sub foo
dim v(1 to N, 1 to M)
...
M = New_Value
redim preserve v(,1 to M) ' seemed reasonable ... then CRASH !!!
有趣的是,VBA要求您明确使用2D Redim保留的第一个参数(因为您不允许更改第一个尺寸).但是,当一个简单的语法错误的代价是一次严重的崩溃时,这并不可笑.这是一个具有相当干净环境的IT部门(没有安装外接程序,在IDE中启动w的空白项目窗格),"redim v(,1到M)"也崩溃了我邻居的机器-所以不仅仅是我.
It's funny that VBA requires that you be explicit with the 1st param of a 2D redim preserve (in that you're not allowed to alter the 1st dimension). But it's not funny when the penalty for a simple syntax error is a hard crash. This is an IT dept with a reasonably clean environment (no installed addins, starting w empty project pane in IDE), and "redim v(,1 to M)" crashed my neighbor's machine too - so it's not just me.
我想知道这种行为是否会在其他人身上发生,并发布它,以防万一我浪费了重建工作簿直到解决错误之前所花费的时间.
I am wondering if this behavior occurs with others, and am posting it just in case it saves someone else the hours I wasted rebuilding my workbooks until I pinned down the bug.
推荐答案
崩溃的发生既有第一维度的遗漏,也有可能是措辞上 nLastDimension-1 的遗漏,还有在以下维度中使用TO关键字.
The crash occurs from both the omission of the first dimension, or perhaps better phrased, an omission at nLastDimension - 1, and also use of the TO keyword in the following dimension.
我拿了您的小样本并尝试使其不崩溃:P
I took your small sample and tried to make it NOT crash :P
Option Explicit 'Just wanted to make the Interpreter more picky
Private Sub test()
'Go through some basic declaration
Dim v() As Variant
Dim M As Integer
Dim New_Value As Integer
ReDim v(2, 2) 'No Problem
ReDim Preserve v(1, 1 To M) ' No Problem
ReDim Preserve v(1 To M) ' No Problem
ReDim Preserve v(,400) 'Compile Error
'Crash on this
'ReDim Preserve v(,1 To M) 'BOOM!!!
'and this:
'ReDim Preserve v(,1 To 2) 'BOOM!!!
'and this:
'ReDim Preserve v(1 to 5,,1 To 2) 'BOOM!!!
End Sub
最下面的一行,"BOOM"行之前的任何代码都不会缓冲跌落.我会理解的一个编译器错误,但是VBA并未编码为处理
Bottom line, none of the code before the "BOOM" lines will cushion the fall. A compiler error I would understand, but VBA is not coded to handle
ReDim myVar(,x TO y)
我用过Office 2007.
I used Office 2007.
对于踢脚和笑容,我还在以下位置尝试了此代码:
For kicks and grins, I also tried this code in the following places:
- 访问'97(CRASH)
- Visual Basic 5 IDE(CRASH)
- 用于OpenOffice Calc的BASIC环境. (请参见下文)
它没有崩溃,并且我收到以下两个编译器错误: 首先这个:
It did not crash and I received the following two compiler errors: First this:
然后这样:
这篇关于Excel 2010 Redim Preserve崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!