2016-11-12 71 views
1

我怎麼能扭轉一個數組,它是全整數例如: -VBA:反轉數組?

[1;5;8;45;54] 

向:

[54;45;8;5;1] 

是否有任何內置的功能,我可以使用?

我嘗試使用this方法:

Array.Reverse(arr) 

我加的Mscorlib.dll從工具>參考,但它顯示錯誤:語法錯誤。在Array.Reverse(arr)位置。

+0

[這](https://msdn.microsoft.com/en-us/library/0fbdt7b9(V = VS.100)的.aspx)應該是有幫助的。 –

+0

(使用'Array.Reverse')。 –

+0

我之前嘗試過,但沒有奏效。 – Rudolph

回答

4

Array.Reverse聽起來像VB.Net,而不是VBA。

芯片皮爾遜有幾乎任何你想要做的數組(和其他結構)的功能。

http://www.cpearson.com/excel/vbaarrays.htm - >ReverseArrayInPlace

相關部分是:

Ndx2 = UBound(InputArray) 
' loop from the LBound of InputArray to the midpoint of InputArray 
For Ndx = LBound(InputArray) To ((UBound(InputArray) - LBound(InputArray) + 1) \ 2) 
    'swap the elements 
    Temp = InputArray(Ndx) 
    InputArray(Ndx) = InputArray(Ndx2) 
    InputArray(Ndx2) = Temp 
    ' decrement the upper index 
    Ndx2 = Ndx2 - 1 
Next Ndx 
6

你可以使用ArrayList類和包裝其Reverse方法:

Function ReverseArray(arr As Variant) As Variant 
    Dim val As Variant 

    With CreateObject("System.Collections.ArrayList") '<-- create a "temporary" array list with late binding 
     For Each val In arr '<--| fill arraylist 
      .Add val 
     Next val 
     .Reverse '<--| reverse it 
     ReverseArray = .Toarray '<--| write it into an array 
    End With 
End Function 

使用,如:

Sub main() 
    Dim arr As Variant 

    arr = ReverseArray(Array(1, 2, 3, 4, 5)) '<-- it returns an array of Variant/Integer with values 5,4,3,2,1   
End Sub 
1

安德烈的答案指的是Chip Pearson的函數我相信for循環中的+1是錯誤的,在LBound和UBound的情況下,兩者都不是偶數或者兩者都是ODD結果在中點逆轉被還原。即LBound和UBound之間的差異是ODD。

考慮0 = LBound和9 = UBound。

9 + 1 = 10/2 = 5

所以環路將是NDX = 0至5。即6次迭代。一次迭代太多了。

結果在以下掉期。
NDX = 0,Ndx2 = 9:0 <> 9
NDX = 1,Ndx2 = 8:1 <> 8
NDX = 2,Ndx2 = 7:2 <> 7
NDX = 3,Ndx2 = 6:3 <> 6
NDX = 4,Ndx2 = 5:4 <> 5
NDX = 5,Ndx2 = 4:5 <> 4

所以中點元件4和5被交換,然後交換回來。
產生的順序:9,8,7,6,4,5,3,2,1,0

此外LBound應添加到UBound,而不是減去。如果減去那麼它只適用於LBound爲零。考慮50 = LBound,100 = UBound。這會導致For Ndx = 50到25.注意,這應該是FROM,TO計算而不是迭代次數計算。

這裏是我的反轉一維和二維數組的功能。
它們也可以選擇保留指定數量的標題行。

' Reverse array (one dimensional), optionally retain header rows. 
Private Sub Reverse_Array_1d(ByRef Ary As Variant, Optional Header_Rows As Integer = 0) 

Dim Dimension_Y As Integer  ' Rows (height) 
Dim Y_first As Long 
Dim Y_last As Long 
Dim Y_last_plus_Y_first As Long 
Dim Y_next As Long 

Dimension_Y = 1 
Y_first = LBound(Ary, Dimension_Y) + Header_Rows 
Y_last = UBound(Ary, Dimension_Y) 
Y_last_plus_Y_first = Y_last + Y_first 

Dim tmp As Variant 

For Y = Y_first To Y_last_plus_Y_first/2 
    Y_next = Y_last_plus_Y_first - Y 
    tmp = Ary(Y_next) 
    Ary(Y_next) = Ary(Y) 
    Ary(Y) = tmp 
Next 

End Sub 

ReDim Ary(0 To 9) As Variant 
Header_Rows = 1 
Call Reverse_1d_Array(Ary, CInt(Header_Rows)) 

 

' Reverse array (two dimensional), optionally retain header rows. 
Private Sub Reverse_Array_2d(ByRef Ary As Variant, Optional Header_Rows As Integer = 0) 

Dim Dimension_Y As Integer  ' Rows (height) 
Dim Y_first As Long 
Dim Y_last As Long 
Dim Y_last_plus_Y_first As Long 
Dim Y_next As Long 

Dimension_Y = 1 
Y_first = LBound(Ary, Dimension_Y) + Header_Rows 
Y_last = UBound(Ary, Dimension_Y) 
Y_last_plus_Y_first = Y_last + Y_first 

Dim Dimension_X As Integer  ' Columns (width) 
Dim X_first As Long 
Dim X_last As Long 

Dimension_X = 2 
X_first = LBound(Ary, Dimension_X) 
X_last = UBound(Ary, Dimension_X) 

ReDim tmp(X_first To X_last) As Variant 

For Y = Y_first To Y_last_plus_Y_first/2 
    Y_next = Y_last_plus_Y_first - Y 
    For X = X_first To X_last 
     tmp(X) = Ary(Y_next, X) 
     Ary(Y_next, X) = Ary(Y, X) 
     Ary(Y, X) = tmp(X) 
    Next 
Next 

End Sub 

ReDim Ary(0 To 9, 0 To 3) As Variant 
Header_Rows = 1 
Call Reverse_2d_Array(Ary, CInt(Header_Rows))