|
发表于 2015-4-23 12:58
|
显示全部楼层
本楼为最佳答案
本帖最后由 wcymiss 于 2015-4-23 13:03 编辑
请楼主测试:结果放在sheet2表中了:- Option Explicit
- Sub Main()
- Dim arrData()
- Dim i As Long
- Dim startRow As Long
- Dim endRow As Long
-
- With Sheet1
- arrData = .Range("D5:I" & .Cells(.Rows.Count, "D").End(xlUp).Row + 1).Value
- End With
-
- startRow = 1
- For i = 2 To UBound(arrData)
- If arrData(i, 1) <> arrData(i - 1, 1) Then
- endRow = i - 1
- Call MySort(arrData, startRow, endRow, 6)
- startRow = i
- End If
- Next
-
- Sheet2.Cells.Clear
- Sheet2.Range("a1").Resize(UBound(arrData), UBound(arrData, 2)).Value = arrData
- End Sub
- Sub MySort(arrData, startRow As Long, endRow As Long, KeyColumn As Long)
- Dim i As Long
- Dim j As Long
- Dim RandNum As Long
-
- '洗牌随机
- Randomize
- For i = endRow To startRow Step -1
- RandNum = Int(Rnd * (i - startRow + 1) + startRow)
- Call Exchange(arrData, RandNum, i)
- Next
-
- '顺序倒序选择交换
- '顺序
- For i = startRow + 1 To endRow - 1
- If arrData(i, KeyColumn) = arrData(i - 1, KeyColumn) Then
- For j = i + 1 To endRow
- If arrData(j, KeyColumn) <> arrData(i, KeyColumn) Then
- Call Exchange(arrData, j, i)
- Exit For
- End If
- Next
- End If
- Next
-
- '倒序
- For i = endRow - 1 To startRow + 1 Step -1
- If arrData(i, KeyColumn) = arrData(i + 1, KeyColumn) Then
- For j = i - 1 To startRow Step -1
- If arrData(j, KeyColumn) <> arrData(i, KeyColumn) Then
- Call Exchange(arrData, j, i)
- Exit For
- End If
- Next
- End If
- Next
- End Sub
- Sub Exchange(arrData, row1 As Long, row2 As Long)
- Dim Temp
- Dim j As Long
- For j = 1 To UBound(arrData, 2)
- Temp = arrData(row1, j)
- arrData(row1, j) = arrData(row2, j)
- arrData(row2, j) = Temp
- Next
- End Sub
复制代码 |
|