2014-02-19 189 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出境的:) –