|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub 按顺序组合()
- Dim i, j, k, m, n, p, q, s&, sr
- Dim arr, brr, crr, drr
- n = 3 '给定的任意个数字中取 n个组合
- drr = Range("A1").CurrentRegion.Value
- m = UBound(drr, 2) - n + 2
- p = Application.Combin(m, 2)
- ReDim arr(1 To p)
- For i = 1 To m - 1
- For j = i + 1 To m
- k = k + 1
- arr(k) = i & "," & j
- Next j
- Next i
- For k = 3 To n
- q = 0 '计数器
- m = m + 1
- p = Application.Combin(m, k)
- ReDim brr(1 To p)
- For i = 1 To UBound(arr)
- s = Mid(arr(i), InStrRev(arr(i), ",") + 1)
- For j = s + 1 To m
- q = q + 1
- brr(q) = arr(i) & "," & j
- Next j
- Next i
- If k < n Then arr = brr
- Next k
- ReDim arr(1 To p, 1 To 3)
- q = 0 '计数器
- For i = 1 To p
- s = 0
- sr = ""
- crr = Split(brr(i), ",") '索引从0开始
- For j = 0 To n - 2
- If drr(1, crr(j)) - drr(1, crr(j + 1)) > 0 Then s = s + 1 Else s = s - 1
- sr = sr & drr(1, crr(j)) & ","
- Next j
- sr = sr & drr(1, crr(n - 1))
- If Abs(s) = n - 1 Then q = q + 1: arr(q, 1) = sr
- Next i
- Range("B12").CurrentRegion = "" '.ClearContents
- Range("B12").Resize(q, 3) = arr
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|