2011-08-12 96 views

回答

1

下面的代碼未經測試,但它應該是相當接近,之後的代碼已經運行了所有不能被5個元素整除的元素將被包含在數組'strSecond'中。

UPDATE

代碼下面現已測試 -

Dim strFirst(49) As String 
Dim strSecond() As String 
Dim ArrCount As Integer 

ArrCount = 0 

For i = 1 To 50 
    strFirst(i - 1) = i 
Next 

For i = 0 To 49 
If (i + 1) Mod 5 <> 0 Then 
    ReDim Preserve strSecond(ArrCount) 
    strSecond(ArrCount) = strFirst(i) 
    ArrCount = ArrCount + 1 
    End If 
Next 

For i = 0 To UBound(strSecond) 
    Debug.Print strSecond(i) 
Next 

UPDATE II

這將工作多維陣列(僅上限的最後的當您使用Preserve關鍵字時,可以更改多維陣列中的尺寸) -

Dim strFirst(1, 49) As String 
Dim strSecond() As String 
Dim ArrCount As Integer 

ArrCount = 0 

For i = 1 To 50 
    strFirst(0, i - 1) = i 
    strFirst(1, i - 1) = i 
Next 

For i = 0 To 49 
If (i + 1) Mod 5 <> 0 Then 
    ReDim Preserve strSecond(1, ArrCount) 
    'If you have more than two dimensions could write a loop to do this 
    'For j = 0 To UBound(strFirst,1) etc 
    strSecond(0, ArrCount) = strFirst(0, i) 
    strSecond(1, ArrCount) = strFirst(1, i) 
    ArrCount = ArrCount + 1 
    End If 
Next 

For i = 0 To UBound(strSecond, 2) 
    Debug.Print strSecond(0, i) + "-" + strSecond(1, i) 
Next 
+0

這是正確的答案......但我剛剛意識到我的數組是多維的,ReDim Preserve不起作用。每個元素X本身都包含Y元素,我不認爲Preserve適用於多維數組。有沒有辦法做到這一點與多維數組? – phan

+0

我已經更新了處理多維數組的答案。 – ipr101

相關問題