Excel VBA函数:如何传递范围,转换为数组,反转,并返回数组

我想在Excel中做一些数组math,这需要我反转一定数量的一维范围,所以我想为它写一个函数,而不是在电子表格中创build反转。

我在VBA中写了一个reverse()函数,但是它返回#VALUE! 电子表格中的错误。 无论数组大小,也不pipe是input相同大小的数组函数,还是使用SUM()这样的汇总函数,都会发生这种情况。 我证实了倒车逻辑作为一个子。 这导致我相信问题是传递/返回范围/数组,但我不明白什么是错的。

Function reverse(x As Range) As Variant() ' Array formula that reverses a one-dimensional array (1 row, x columns) Dim oldArray() As Variant, newArray() As Variant Dim rows As Long: i = x.rows.Count Dim cols As Long: i = x.Columns.Count ReDim oldArray(1 To rows, 1 To cols), newArray(1 To rows, 1 To cols) oldArray = x.Value newArray = oldArray For i = 1 To cols / 2 Step 1 newArray(1, i) = oldArray(1, cols - i + 1) newArray(1, cols - i + 1) = oldArray(1, i) Next reverse = newArray End Function 

请记住,我可以扩展它来扭转二维数组,但这是微不足道的部分。 我的问题是试图确保函数在(1,N)范围内工作。

谢谢!

find下面的代码….

 Function reverse(x As Range) As Variant() ' Array formula that reverses a one-dimensional array (1 row, x columns) Dim oldArray() As Variant, newArray() As Variant Dim rows As Long rows = x.rows.Count Dim cols As Long cols = x.Columns.Count ReDim oldArray(1 To rows, 1 To cols), newArray(1 To rows, 1 To cols) oldArray = x.Value newArray = oldArray For i = 1 To cols / 2 Step 1 newArray(1, i) = oldArray(1, cols - i + 1) newArray(1, cols - i + 1) = oldArray(1, i) Next reverse = newArray End Function 

下面的代码是更通用的,它使用可选的参数来确定是否应该颠倒行,列或两者(或无)。 默认情况下,它会反转列。

 Function ReverseRange(Source As Range, Optional ReverseRows As Boolean = False, Optional ReverseColumns As Boolean = True) As Variant() Dim SourceArray() As Variant Dim DestArray() As Variant SourceArray = Source.value Dim nRows As Long, nColumns As Long nRows = UBound(SourceArray, 1) nColumns = UBound(SourceArray, 2) ReDim DestArray(1 To nRows, 1 To nColumns) Dim r As Long, r2 As Long, c As Long, c2 As Long For r = 1 To nRows r2 = IIf(ReverseRows, nRows - r + 1, r) For c = 1 To nColumns c2 = IIf(ReverseColumns, nColumns - c + 1, c) DestArray(r2, c2) = SourceArray(r, c) Next c Next r ReverseRange = DestArray End Function 

请注意,范围有效性没有validation。

这将颠倒范围中的列,而不pipe行数。

 Function reverse(Source As Range) As Variant() Dim Data, RevData Dim x As Long, y As Long, y1 As Long Data = Source.Value ReDim RevData(1 to UBound(Data, 1),1 to UBound(Data, 2)) For x = 1 To UBound(Data, 1) y1 = UBound(Data, 2) For y = 1 To UBound(Data, 2) RevData(x, y1) = Data(x, y) y1 = y1 - 1 Next Next reverse = RevData End Function