如何用EXCEL将"1、2、3、4、5、6"这六个数的所有组合排列出来?
3个回答
展开全部
按ALT+F11打开VBA编辑器,插入一个模块,把如下代码放入,把光标停在代码中间,按F5运行。
Public Sub lwy()
For a = 1 To 6
For b = 1 To 6
For c = 1 To 6
For d = 1 To 6
For e = 1 To 6
For f = 1 To 6
If a <> b And a <> c And a <> d And a <> e And a <> f _
And b <> c And b <> d And b <> e And b <> f _
And c <> d And c <> e And c <> f _
And d <> e And d <> f _
And e <> f Then
n = n + 1
Cells(n, 1) = a: Cells(n, 2) = b: Cells(n, 3) = c: _
Cells(n, 4) = d: Cells(n, 5) = e: Cells(n, 6) = f
End If
Next
Next
Next
Next
Next
Next
End Sub
Public Sub lwy()
For a = 1 To 6
For b = 1 To 6
For c = 1 To 6
For d = 1 To 6
For e = 1 To 6
For f = 1 To 6
If a <> b And a <> c And a <> d And a <> e And a <> f _
And b <> c And b <> d And b <> e And b <> f _
And c <> d And c <> e And c <> f _
And d <> e And d <> f _
And e <> f Then
n = n + 1
Cells(n, 1) = a: Cells(n, 2) = b: Cells(n, 3) = c: _
Cells(n, 4) = d: Cells(n, 5) = e: Cells(n, 6) = f
End If
Next
Next
Next
Next
Next
Next
End Sub
展开全部
用VBA
这是我能想到的办法了,希望有更好的代码。我的代码只在最后一次写入单元格,执行效率高。
Sub aa()
Dim x1(1 To 720, 1 To 6)
a = 1
For i1 = 1 To 6
For i2 = 1 To 6
For i3 = 1 To 6
For i4 = 1 To 6
For i5 = 1 To 6
For i6 = 1 To 6
If i1 <> i2 And i1 <> i3 And i1 <> i4 And i1 <> i5 And i1 <> i6 Then
If i2 <> i3 And i2 <> i4 And i2 <> i5 And i2 <> i6 Then
If i3 <> i4 And i3 <> i5 And i3 <> i6 Then
If i4 <> i5 And i4 <> i6 Then
If i5 <> i6 Then
x1(a, 1) = i1
x1(a, 2) = i2
x1(a, 3) = i3
x1(a, 4) = i4
x1(a, 5) = i5
x1(a, 6) = i6
a = a + 1
End If
End If
End If
End If
End If
Next i6
Next i5
Next i4
Next i3
Next i2
Next i1
Sheet2.Range("A1:F720") = x1
End Sub
这是我能想到的办法了,希望有更好的代码。我的代码只在最后一次写入单元格,执行效率高。
Sub aa()
Dim x1(1 To 720, 1 To 6)
a = 1
For i1 = 1 To 6
For i2 = 1 To 6
For i3 = 1 To 6
For i4 = 1 To 6
For i5 = 1 To 6
For i6 = 1 To 6
If i1 <> i2 And i1 <> i3 And i1 <> i4 And i1 <> i5 And i1 <> i6 Then
If i2 <> i3 And i2 <> i4 And i2 <> i5 And i2 <> i6 Then
If i3 <> i4 And i3 <> i5 And i3 <> i6 Then
If i4 <> i5 And i4 <> i6 Then
If i5 <> i6 Then
x1(a, 1) = i1
x1(a, 2) = i2
x1(a, 3) = i3
x1(a, 4) = i4
x1(a, 5) = i5
x1(a, 6) = i6
a = a + 1
End If
End If
End If
End If
End If
Next i6
Next i5
Next i4
Next i3
Next i2
Next i1
Sheet2.Range("A1:F720") = x1
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
路过。。。。我还真不会
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询