1. 首页 > 科技

实现在excel把J列和K列的数组自动交替组合,组合总长14位,#位置随数字出现而被替代?

实现在excel把J列和K列的数组自动交替组合,组合总长14位,#位置随数字出现而被替代?

ExcelVBA对一组数组进行全组合操作

Sub paixu()

Dim arr(4)

Row = 1 '循环,共495行 ,row为记录行号的变量,每次增大1

For i = 12 To 1 Step -1 '排列组合题,组合不出现重复,为实现从大到小排序,取数时从大开始

For j = i - 1 To 1 Step -1 '最大数为11

For k = j - 1 To 1 Step -1 '最大数为10

For l = k - 1 To 1 Step -1 '9

arr(1) = Sheets("sheet1").Cells(i, 1)

arr(2) = Sheets("sheet1").Cells(j, 1)

arr(3) = Sheets("sheet1").Cells(k, 1)

arr(4) = Sheets("sheet1").Cells(l, 1)

For m = 1 To 3 ‘排序,由大到小

For n = m + 1 To 4

If arr(n) > arr(m) Then

swap_ = arr(m)

arr(m) = arr(n)

arr(n) = swap_

End If

Next

Next ‘排序结束

Sheets("sheet2").Cells(Row, 1) = arr(1) 'sheet2数据表里,row行的第一列显示i的值,后面三行同理

Sheets("sheet2").Cells(Row, 2) = arr(2)

Sheets("sheet2").Cells(Row, 3) = arr(3)

Sheets("sheet2").Cells(Row, 4) = arr(4)

Row = Row + 1

Next

Next

Next

Next

End Sub