该数组已固定或暂时锁定

发布于 2024-12-08 17:57:52 字数 1270 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦行七里 2024-12-15 17:57:52

您的 line4 标签位于 temp 变量的 for 循环之外,因此当您 goto 时,它会保持锁定状态。

您确实应该重构代码,不要在 foreach 循环中使用 goto。

也许:(

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 
            **Exit For**
          End If
          **If flag = 1 Then Exit For**
        Next boken_c 
      End If 
      **If flag = 1 Then Exit For**
    Next crowss 
  End If 
Next crow 

注意 **d 行。)

Your line4 label is outside the for loop on the temp variable so when you goto it leaves it locked.

You really should restructure your code to not use a goto inside the for each loop.

Maybe:

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 
            **Exit For**
          End If
          **If flag = 1 Then Exit For**
        Next boken_c 
      End If 
      **If flag = 1 Then Exit For**
    Next crowss 
  End If 
Next crow 

(Note the **d lines.)

清风无影 2024-12-15 17:57:52

我用VBA也遇到这个问题。我不能说我对我如何获得它感到自豪,但它是在这里提供的,以防其他人意外地犯错。

调试非常有趣,因为失败发生在调用子函数或函数时,而不是发生在失败点。幸运的是,您可以在报告错误之前跟踪代码直至被调用例程的有问题的行。

Call Sub1(gArray(3))
debug.print gArray(3)
...
Sub Sub1(i as integer)
Redim gArray(0)
End sub

显然 VB RT 不会喜欢这样,因为在 debug.print 执行时,数组维度不存在。好吧,为什么你要传递一个全局声明的数组呢?被指控有罪。

因此,在上面的示例中,您在对 Sub1 的调用处收到错误,但导致该错误的是 sub 中的 Redim。

这个故事的寓意是不要将全局变量作为参数传递给子进程。另一个简单的修复方法是对 Sub1 进行稍微不同的声明:

Sub Sub1(ByVal i as integer)

这意味着 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.

Call Sub1(gArray(3))
debug.print gArray(3)
...
Sub Sub1(i as integer)
Redim gArray(0)
End sub

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:

Sub Sub1(ByVal i as integer)

This means the i variable is copied (but not returned) by the Sub.

葬﹪忆之殇 2024-12-15 17:57:52

谢谢迪安娜的回答!我在下一个代码片段的最后一行的 ReDim Preserve 语句上有类似的消息:

        If StrComp(Mid(s, 1, lE), txtE, vbBinaryCompare) = 0 Then
            For i = iMdl To 1 Step -1
                With blks(i)
                    If .lnEnd = 0 Then   ' ".lnEnd" is a member of blks(i)
                        .lnEnd = ln
                        GoTo NXT
                    End If
                End With
            Next
            errBegWith = errBegWith & ln & ", "
NXT:
        End If
    '...
    ReDim Preserve blks(iMdl)

在从 With< 内部提取赋值 .lnEnd = ln 后/code> 程序运行良好:

        If StrComp(Mid(s, 1, lE), txtE, vbBinaryCompare) = 0 Then
            For i = iMdl To 1 Step -1
                If blks(i).lnEnd = 0 Then
                    blks(i).lnEnd = ln
                    GoTo NXT
                End If
            Next
            errBegWith = errBegWith & ln & ", "
NXT:
        End If
    '...
    ReDim Preserve blks(iMdl)

Thanks, Deanna for the answer! I have a similar message on ReDim Preserve statement at the last line in next fragment of code:

        If StrComp(Mid(s, 1, lE), txtE, vbBinaryCompare) = 0 Then
            For i = iMdl To 1 Step -1
                With blks(i)
                    If .lnEnd = 0 Then   ' ".lnEnd" is a member of blks(i)
                        .lnEnd = ln
                        GoTo NXT
                    End If
                End With
            Next
            errBegWith = errBegWith & ln & ", "
NXT:
        End If
    '...
    ReDim Preserve blks(iMdl)

And after extracting assignment .lnEnd = ln from inside of the With the program works fine:

        If StrComp(Mid(s, 1, lE), txtE, vbBinaryCompare) = 0 Then
            For i = iMdl To 1 Step -1
                If blks(i).lnEnd = 0 Then
                    blks(i).lnEnd = ln
                    GoTo NXT
                End If
            Next
            errBegWith = errBegWith & ln & ", "
NXT:
        End If
    '...
    ReDim Preserve blks(iMdl)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文