|
楼主 |
发表于 2012-2-1 09:59
|
显示全部楼层
直接一次性输出组合结果的代码:- Sub GetCombinArray()
- tms = Timer
-
- m = [a1].End(4).Row
- n = [b1]
- kc = [b2]
- arr = [a1].Resize(m)
-
-
- ReDim brr(1 To n, 1 To 2)
- For i = 1 To n
- brr(i, 1) = i
- brr(i, 2) = i + m - n
- Next i
-
- AC = WorksheetFunction.Combin(m, n)
-
- ' Dim crr() As Long
- ReDim crr(1 To AC, 1 To m)
- For i = 1 To AC + 0.1
- ' crr(i, j) = brr(j, 1)
- If kc = m Then
- crr(i, brr(1, 1)) = arr(brr(1, 1), 1)
- ElseIf kc = n Then
- crr(i, 1) = arr(brr(1, 1), 1)
- Else
- crr(i, 1) = "'" & arr(brr(1, 1), 1)
- End If
-
- For j = 2 To n
- If kc = m Then
- crr(i, brr(j, 1)) = arr(brr(j, 1), 1)
- ElseIf kc = n Then
- crr(i, j) = arr(brr(j, 1), 1)
- Else
- crr(i, 1) = crr(i, 1) & kc & arr(brr(j, 1), 1)
- End If
- Next j
-
- brr(n, 1) = brr(n, 1) + 1
- If brr(n, 1) > brr(n, 2) Then
- For j = n - 1 To 1 Step -1
- If brr(j, 1) < brr(j, 2) Then
- l = j
- Exit For
- End If
- Next j
- If l > 0 Then
- brr(l, 1) = brr(l, 1) + 1
- For j = l + 1 To n
- brr(j, 1) = brr(j - 1, 1) + 1
- Next j
- End If
- End If
- Next
-
- [b3] = AC
- [b4] = Timer - tms
- If AC > 65536 Then Exit Sub
-
- [a1].EntireColumn.AutoFit
- [d1].CurrentRegion.Clear
- If kc = m Or kc = n Then
- [d1].Resize(AC, m) = crr
- [d1].Resize(1, m).ColumnWidth = [a1].ColumnWidth
- ElseIf kc = n Then
- [d1].Resize(AC, n) = crr
- [d1].Resize(1, n).ColumnWidth = [a1].ColumnWidth
- Else
- [d1].Resize(AC) = crr
- [d1].EntireColumn.AutoFit
- End If
-
- [b5] = Timer - tms
- [b6] = [b5] - [b4]
-
- End Sub
复制代码 |
|