2014-02-19 142 views
0

我有以下代码:静态或动态数组

Sub nummm() 

    Dim num() As String 

    For x = 2 To 1000 
     c = 0 
     ReDim Preserve num(UBound(Split(Cells(x, 7).Value, " ")) + 1) 
     num = Split(Cells(x, 7).Value, " ") 

     For Each b In num 
      c = c + 1 
      If c = UBound(num) + 1 Then GoTo vv: 
     Next 

vv: 
    Next 

End Sub 

它的运行很好,如果我删除线

If c = UBound(num) + 1 Then GoTo vv: 

,但如果它不除,我得到运行时错误:“这个数组固定或暂时锁定“ 如何使变量num动态? THX求助

回答

2

我不知道你的b和其他变量(除num)已宣告但也许这可以帮助你:

Sub nummm() 

    Dim num As Variant 'So you can directly assign the array from Split 

    For x = 2 To 1000 
     c = 0 
     num = Split(Cells(x, 7).Value, " ") 

     For Each b In num 
      c = c + 1 

      'Rather than "GoTo somewhere", "Exit For" will exit the current For loop 
      If c = UBound(num) + 1 Then Exit For 
     Next b 

    Next x 

End Sub 
+1

+1出境的:) –