|
楼主 |
发表于 2012-4-13 17:00
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
可以求:
从任意序号位置开始,到任意序号位置结束 的组合结果的代码:- Sub GetCombinArray()
- Dim AC&, i&, j%, l%, m%, n%, r&, s&, f&, p%, q&
- tms = Timer
-
- m = [a1].End(4).Row: n = [b1]: AC = Application.Combin(m, n): k = [b2]
- s = IIf([b4] = 0 Or [b4] > AC, 1, [b4]): f = IIf([b5] = 0 Or [b5] > AC, AC, [b5])
- arr = [a1].Resize(m)
-
- ReDim a%(1 To n)
- ReDim b%(1 To n)
-
- r = s
- For i = 1 To n - 1
- For j = 1 To m - n + 1
- q = Application.Combin(m - i - p, m - n - p)
- If r > q Then r = r - q: p = p + 1 Else l = l + j: Exit For
- Next
- a(i) = l 'a(i) = i 'brr(i, 1) = i
- b(i) = i + m - n 'brr(i, 2) = i + m - n
- Next
- a(n) = (r - 1) Mod (m - l + 1) + l + 1
- b(n) = m
-
- ReDim crr(s To f, m)
- For i = s To f
- ' crr(i, j) = a(j) 'crr(i, j) = brr(j, 1)
- For j = 1 To n
- If k = m Then
- crr(i, 0) = crr(i, 0) & "," & arr(a(j), 1)
- crr(i, a(j)) = arr(a(j), 1)
- ElseIf k = n Then
- crr(i, 0) = crr(i, 0) & "," & arr(a(j), 1)
- crr(i, j) = arr(a(j), 1)
- Else
- crr(i, 0) = crr(i, 0) & kc & arr(a(j), 1)
- End If
- Next j
- If k = m Or k = n Then crr(i, 0) = Mid(crr(i, 0), 2) Else crr(i, 0) = Mid(crr(i, 0), Len(kc) + 1)
-
- a(n) = a(n) + 1 'brr(n, 1) = brr(n, 1) + 1
- If a(n) > b(n) Then 'If brr(n, 1) > brr(n, 2) Then
- For j = n - 1 To 1 Step -1
- If a(j) < b(j) Then l = j: Exit For 'If brr(j, 1) < brr(j, 2) Then l = j: Exit For
- Next j
- If l > 0 Then
- a(l) = a(l) + 1 'brr(l, 1) = brr(l, 1) + 1
- For j = l + 1 To n
- a(j) = a(j - 1) + 1 'brr(j, 1) = brr(j - 1, 1) + 1
- Next j
- End If
- End If
- Next
-
- [b3] = AC
- [b6] = Timer - tms
- If f - s > 65535 Then Exit Sub
-
- [a1].EntireColumn.AutoFit
- [d1].CurrentRegion.Clear
- If k = m Then
- [d1].Resize(f - s + 1, m + 1) = crr
- [e1].Resize(1, m).ColumnWidth = [a1].ColumnWidth
- ElseIf k = n Then
- [d1].Resize(f - s + 1, n + 1) = crr
- [e1].Resize(1, n).ColumnWidth = [a1].ColumnWidth
- Else
- [d1].Resize(f - s + 1) = crr
- End If
- [d1].EntireColumn.AutoFit
-
- [b7] = Timer - tms
- [b8] = [b7] - [b6]
-
- End Sub
复制代码 |
|