1
A
回答
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))
相關問題
- 1. 在Excel中反轉數組VBA
- 2. 反轉數組?
- 3. 將VBA數組轉換爲JSON數組
- 4. VBA轉換數組類型
- 5. Excel VBA反向轉置
- 6. 垂直反轉2d數組?
- 7. 在C中反轉數組?
- 8. Javascript的數組反轉
- 9. 反轉字符數組
- 10. 反轉一維數組--PYTHON
- 11. 如何反轉數組?
- 12. 如何反轉數組
- 13. 如何反轉Int數組?
- 14. 反轉元組參數
- 15. 在C++中反轉數組
- 16. 複製和反轉數組?
- 17. numpy中的反轉數組?
- 18. JavaScript遞歸,反轉數組
- 19. 反轉字符數組
- 20. 數組的特殊反轉
- 21. Excel VBA函數:如何通過範圍,轉換爲數組,反轉,並返回數組
- 22. 在組件中反轉數組(masm)
- 23. 如何反轉這個整數數組?
- 24. 轉換一個VBA數組「列」陣列
- 25. 粘貼維數組VBA - 用轉
- 26. VBA - 2010 - 如何反轉過濾器
- 27. 將Excel VBA中的數組轉換爲Powerpoint VBA
- 28. 使用NumPy反轉數組的部分
- 29. 反轉與數組中的哈希值
- 30. 反轉一個數組(顏色形狀)
[這](https://msdn.microsoft.com/en-us/library/0fbdt7b9(V = VS.100)的.aspx)應該是有幫助的。 –
(使用'Array.Reverse')。 –
我之前嘗試過,但沒有奏效。 – Rudolph