与之前的文章《Excel·VBA螺旋数组函数》将一维数组转为二维螺旋数组
本文将数组转为S形排列的二维数组,类似考场座位S形顺序
Function S形排列(ByVal arr, ByVal num_rows&, ByVal num_cols&, Optional ByVal mode$ = "row")'将数组arr转为num_rows行 * num_cols列的S形排列二维数组(数组从1开始计数)'写入模式mode,row按行写入,col按列写入Dim a, brr, result, rc&, i&, j&, x&, y&rc = num_rows * num_cols: ReDim brr(1 To rc), result(1 To num_rows, 1 To num_cols)For Each a In arr '多行多列的,按列从左往右读取,防止arr元素个数超出rcIf x < rc Then x = x + 1: brr(x) = aNextIf mode = "row" ThenFor i = 1 To num_rowsIf i Mod 2 = 1 ThenFor j = 1 To num_cols '奇数行,从左往右写入y = y + 1: result(i, j) = brr(y)NextElseFor j = num_cols To 1 Step -1 '偶数行,从右往左写入y = y + 1: result(i, j) = brr(y)NextEnd IfNextElseIf mode = "col" ThenFor j = 1 To num_colsIf j Mod 2 = 1 ThenFor i = 1 To num_rows '奇数列,从上往下写入y = y + 1: result(i, j) = brr(y)NextElseFor i = num_rows To 1 Step -1 '偶数列,从下往上写入y = y + 1: result(i, j) = brr(y)NextEnd IfNextEnd IfS形排列 = result
End Function
- 举例
Sub 测试()Dim arr, brrarr = [a1].CurrentRegionbrr = S形排列(arr, 5, 4)[c1].Resize(UBound(brr), UBound(brr, 2)) = brrbrr = S形排列(arr, 4, 5)[c8].Resize(UBound(brr), UBound(brr, 2)) = brr
End Sub
按行写入再使用Transpose
函数转置后的结果,与直接使用按列写入一致
Sub 测试()Dim arr, brrarr = [a1].CurrentRegionbrr = WorksheetFunction.Transpose(S形排列(arr, 5, 4))[c1].Resize(UBound(brr), UBound(brr, 2)) = brrbrr = S形排列(arr, 4, 5, "col")[c8].Resize(UBound(brr), UBound(brr, 2)) = brr
End Sub