该数组已固定或暂时锁定
我正在使用 split 函数并在变量中分配值,并在几次迭代后循环运行代码,它给出了“此数组已固定或暂时锁定(Visual Basic)”的错误
。这里从Excel读取的movies_cat1的值是这样的----- “电影 -> 列出所有电影,电影 -> 世界电影 -> 亚洲,电影 -> 按语言排列的电影 -> 僧伽罗语,电影 -> 戏剧”
For crow = 1 To 100
Value = Worksheets("Movies_categories").Range("A" & crow).Value
cat_final = Worksheets("Movies_categories").Range("B" & crow).Value
If Value = "y" Or Value = "Y" Then
'Loop for reading the data from tabsheet- Movies
For crowss = 5 To 3000
movies_cat1 = Worksheets("Movies").Range("B" & crowss).Value
movies_language = Worksheets("Movies").Range("C" & crowss).Value
If movies_language = "English" Then
Temp = Split(movies_cat, ",") 'run time Error:10 occurs here..
For Each boken_c In Temp
flag = 0
boken_c = Trim(boken_c)
If RTrim(LTrim(boken_c)) = LTrim(RTrim(cat_final)) Then
flag = 1
GoTo Line4:
End If
Next boken_c
End If
Next crowss
End If
Line4: Next crow
此语句出现错误:Temp = Split (movies_cat, ",")
,它表示该数组是固定的或暂时锁定的,因为我认为它最初将“temp”作为变量,但在返回值时split 函数,变量“Temp”在第一个循环完成后变为数组(即在 crow = 6,7 之后......)
I am using split function and assigning the value in a variable and running the code in loop after few iterations its giving an error of "This array is fixed or temporarily locked (Visual Basic)"..
e.g; here value of movies_cat1 read from excel is in form of this------
"Movies->List All Movies , Movies->World Cinema->Asia , Movies->Movies by Language->Sinhalese , Movies->Drama"
For crow = 1 To 100
Value = Worksheets("Movies_categories").Range("A" & crow).Value
cat_final = Worksheets("Movies_categories").Range("B" & crow).Value
If Value = "y" Or Value = "Y" Then
'Loop for reading the data from tabsheet- Movies
For crowss = 5 To 3000
movies_cat1 = Worksheets("Movies").Range("B" & crowss).Value
movies_language = Worksheets("Movies").Range("C" & crowss).Value
If movies_language = "English" Then
Temp = Split(movies_cat, ",") 'run time Error:10 occurs here..
For Each boken_c In Temp
flag = 0
boken_c = Trim(boken_c)
If RTrim(LTrim(boken_c)) = LTrim(RTrim(cat_final)) Then
flag = 1
GoTo Line4:
End If
Next boken_c
End If
Next crowss
End If
Line4: Next crow
Error occurs at this statement: Temp = Split(movies_cat, ",")
, it says that the array is fixed or temporarily locked, because i think initially its taking 'temp' as a variable, but while returning the value of split function, variable 'Temp' becomes array after completion of first loop(i.e after crow = 6,7....)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
line4
标签位于 temp 变量的 for 循环之外,因此当您goto
时,它会保持锁定状态。您确实应该重构代码,不要在 foreach 循环中使用 goto。
也许:(
注意 **d 行。)
Your
line4
label is outside the for loop on the temp variable so when yougoto
it leaves it locked.You really should restructure your code to not use a goto inside the for each loop.
Maybe:
(Note the **d lines.)
我用VBA也遇到这个问题。我不能说我对我如何获得它感到自豪,但它是在这里提供的,以防其他人意外地犯错。
调试非常有趣,因为失败发生在调用子函数或函数时,而不是发生在失败点。幸运的是,您可以在报告错误之前跟踪代码直至被调用例程的有问题的行。
显然 VB RT 不会喜欢这样,因为在 debug.print 执行时,数组维度不存在。好吧,为什么你要传递一个全局声明的数组呢?被指控有罪。
因此,在上面的示例中,您在对 Sub1 的调用处收到错误,但导致该错误的是 sub 中的 Redim。
这个故事的寓意是不要将全局变量作为参数传递给子进程。另一个简单的修复方法是对 Sub1 进行稍微不同的声明:
这意味着
i
变量由 Sub 复制(但不返回)。I had this problem too with VBA. I cannot say I am proud of how I managed to get it, but it is supplied here just in can anyone else accidentally slips up on this.
It is quite interesting to debug as the failure occurs at the call to a sub or function - not at the point of failure. Luckily, you can follow the code through to the offending line of the called routine before it reports the error.
Clearly the VB RT is not going to like this as by the time the debug.print executes, the array dimension does not exist. Ok why the hell would you want to pass a globally declared array anyway? Guilty as charged.
So in the example above you get the error at the call to Sub1, but the thing causing it is the Redim in the sub.
The moral to the story is do not pass global variables as parameters to subs. Another simple fix is to declare the Sub1 slightly differently:
This means the
i
variable is copied (but not returned) by the Sub.谢谢迪安娜的回答!我在下一个代码片段的最后一行的
ReDim Preserve
语句上有类似的消息:在从
With< 内部提取赋值
.lnEnd = ln
后/code> 程序运行良好:Thanks, Deanna for the answer! I have a similar message on
ReDim Preserve
statement at the last line in next fragment of code:And after extracting assignment
.lnEnd = ln
from inside of theWith
the program works fine: