请VBA高手写一个宏代码,执行宏后
例如在A列单元格中有“黑,黄,青,蓝,红”等词,执行宏后,这五种颜色的120二十种排列都在表格里列出来。第二就是,A列单元格中有“黑,黄,青,蓝,红,B列单元格中有“黑,...
例如在A列单元格中有“黑,黄,青,蓝,红”等词 ,执行宏后,这五种颜色的120二十种排列都在表格里列出来。
第二就是,A列单元格中有“黑,黄,青,蓝,红,B列单元格中有“黑,黄,青,蓝,红,C列单元格中有“黑,黄,青,蓝,红,D列单元格中有“黑,黄,青,蓝,红,E列单元格中有“黑,黄,青,蓝,红。执行宏后,3125种排列可能都在表格里列出来。谢谢! 展开
第二就是,A列单元格中有“黑,黄,青,蓝,红,B列单元格中有“黑,黄,青,蓝,红,C列单元格中有“黑,黄,青,蓝,红,D列单元格中有“黑,黄,青,蓝,红,E列单元格中有“黑,黄,青,蓝,红。执行宏后,3125种排列可能都在表格里列出来。谢谢! 展开
2个回答
展开全部
第一题是全排列组合么??没说抽几个元素。。按照5选2的全排列,应该是20种 来做的话如下:
sheet1 存你的数据 A1-A5的矩阵
sheet2 用来输出结果哈
Sub test1()
dim x,n,m as integer
x=1
n=1
do
for m= 1 to 5
if m<> x then
Sheets(2).Cells(n, 1) = Sheets(1).Cells(x, 1)
Sheets(2).Cells(n, 2) = Sheets(1).Cells(m, 1)
n=n+1
end if
next m
x=x+1
loop until x>5
end sub
第二个 就是5^5种排列组合呗
sheet1 存你的数据 A1至E5的矩阵
sheet2 用来输出结果哈
Sub test2()
Dim x1, x2, x3, x4, x5 As Integer
Dim n, m As Long
n = 1
For x1 = 1 To 5
For x2 = 1 To 5
For x3 = 1 To 5
For x4 = 1 To 5
For x5 = 1 To 5
Sheets(2).Cells(n, 1) = Sheets(1).Cells(x1, 1)
Sheets(2).Cells(n, 2) = Sheets(1).Cells(x2, 2)
Sheets(2).Cells(n, 3) = Sheets(1).Cells(x3, 3)
Sheets(2).Cells(n, 4) = Sheets(1).Cells(x4, 4)
Sheets(2).Cells(n, 5) = Sheets(1).Cells(x5, 5)
n = n + 1
Next x5
Next x4
Next x3
Next x2
Next x1
End Sub
sheet1 存你的数据 A1-A5的矩阵
sheet2 用来输出结果哈
Sub test1()
dim x,n,m as integer
x=1
n=1
do
for m= 1 to 5
if m<> x then
Sheets(2).Cells(n, 1) = Sheets(1).Cells(x, 1)
Sheets(2).Cells(n, 2) = Sheets(1).Cells(m, 1)
n=n+1
end if
next m
x=x+1
loop until x>5
end sub
第二个 就是5^5种排列组合呗
sheet1 存你的数据 A1至E5的矩阵
sheet2 用来输出结果哈
Sub test2()
Dim x1, x2, x3, x4, x5 As Integer
Dim n, m As Long
n = 1
For x1 = 1 To 5
For x2 = 1 To 5
For x3 = 1 To 5
For x4 = 1 To 5
For x5 = 1 To 5
Sheets(2).Cells(n, 1) = Sheets(1).Cells(x1, 1)
Sheets(2).Cells(n, 2) = Sheets(1).Cells(x2, 2)
Sheets(2).Cells(n, 3) = Sheets(1).Cells(x3, 3)
Sheets(2).Cells(n, 4) = Sheets(1).Cells(x4, 4)
Sheets(2).Cells(n, 5) = Sheets(1).Cells(x5, 5)
n = n + 1
Next x5
Next x4
Next x3
Next x2
Next x1
End Sub
展开全部
高手提供了第二条的代码,我也来凑下趣,把第一个要求的代码补上,供参考。
Sub 排列()
'A1~A5单元格输入要进行排列的内容,如:黑、黄、青、蓝、红
'程序会从第7行开始输出5选5的排列结果
Dim a, b, c, d, e, x As Integer
x = 7
For a = 1 To 5
For b = 1 To 5
For c = 1 To 5
For d = 1 To 5
For e = 1 To 5
If a <> b And a <> c And a <> d And a <> e And _
b <> c And b <> d And b <> e And _
c <> d And c <> e And _
d <> e Then
Cells(x, 1) = Cells(a, 1)
Cells(x, 2) = Cells(b, 1)
Cells(x, 3) = Cells(c, 1)
Cells(x, 4) = Cells(d, 1)
Cells(x, 5) = Cells(e, 1)
x = x + 1
End If
Next e
Next d
Next c
Next b
Next a
End Sub
Sub 排列()
'A1~A5单元格输入要进行排列的内容,如:黑、黄、青、蓝、红
'程序会从第7行开始输出5选5的排列结果
Dim a, b, c, d, e, x As Integer
x = 7
For a = 1 To 5
For b = 1 To 5
For c = 1 To 5
For d = 1 To 5
For e = 1 To 5
If a <> b And a <> c And a <> d And a <> e And _
b <> c And b <> d And b <> e And _
c <> d And c <> e And _
d <> e Then
Cells(x, 1) = Cells(a, 1)
Cells(x, 2) = Cells(b, 1)
Cells(x, 3) = Cells(c, 1)
Cells(x, 4) = Cells(d, 1)
Cells(x, 5) = Cells(e, 1)
x = x + 1
End If
Next e
Next d
Next c
Next b
Next a
End Sub
追问
谢谢,我复制过来,开始不得,后来一看差这个Sub test2()
,补上就行了。谢谢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询