2016-11-12 67 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))