excel vba,实现数字排列组合.
假设从A列数字选2个,B列数字选4个,共6个数字每组按从小到大的顺序排列在C列,数字用,号隔开,求vba公式和C列显示公式。或者有什么别的简单方法也行。每组数字不相同,不...
假设从A列数字选2个,B列数字选4个,共6个数字每组按从小到大的顺序排列在C列,数字用,号隔开,求vba公式和C列显示公式。或者有什么别的简单方法也行。每组数字不相同,不重复。
展开
1个回答
展开全部
下载运行VBA程序即可得到结果,共150个组合。
Sub 宏1()
k = 1
For a = 1 To 4
For b = a + 1 To 5
For c = 1 To 3
For d = c + 1 To 4
For e = d + 1 To 5
For f = e + 1 To 6
Cells(1, 4) = Cells(a, 1)
Cells(2, 4) = Cells(b, 1)
Cells(3, 4) = Cells(c, 2)
Cells(4, 4) = Cells(d, 2)
Cells(5, 4) = Cells(e, 2)
Cells(6, 4) = Cells(f, 2)
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("D1"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("D1:D6")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Cells(k, 3) = Cells(1, 4) & "," & Cells(2, 4) & "," & Cells(3, 4) & "," & Cells(4, 4) & "," & Cells(5, 4) & "," & Cells(6, 4)
k = k + 1
Next
Next
Next
Next
Next
Next
Range("D1:D6").Select
Selection.ClearContents
Range("C1").Select
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询