排列组合我编有通用的模块.
楼主这一个是排列代码.而且是n个元素中,选择1到n个的排列.
如果n过大,excel表要多列内容,当然也可以写到txt文档中,或者access数据库中.速度来说,应该是txt或者access比较快一些.
Sub PaiLieALL()
Dim MyRng As Range, GX As String, X As Integer, DRange As Range
Set MyRng = Range("A1:A4")'可修改目标排列范围
For I1 = 1 To MyRng.Cells.Count
GX = ""
For I2 = I1 To MyRng.Cells.Count
X = Application.WorksheetFunction.CountA([B:B]) + 1‘结果放B列,可修改为其他列
Set DRange = Range("B" & X)’B列可修改
II = II + 1
GX = GX & MyRng(I2, 1)
PermutationsFromString GX, DRange
Next
Next
Set MyRng = Nothing
End Sub
Sub PermutationsFromString(PermString As String, DD As Range)
Dim DestRange As Object
'Dim PermString As String
Dim LenPermString As Integer
Dim NumOfPerm As Double
Dim Factorial(1 To 13)
Dim Counter1 As Long
Dim Counter2 As Long
Dim Rotate As Integer
Dim Dummy
Set DestRange = DD
DestRange.Value = PermString
LenPermString = Len(PermString)
NumOfPerm = Application.Fact(LenPermString)
For Counter1 = 1 To LenPermString
Factorial(Counter1) = Application.Fact(LenPermString - Counter1)
Next Counter1
For Counter1 = 2 To NumOfPerm
If Counter1 / 2 = Int(Counter1 / 2) Then
Rotate = LenPermString - 1
Else
For Counter2 = 1 To LenPermString - 2
If Counter1 Mod Factorial(Counter2) = 1 Then
Rotate = Counter2
Exit For
End If
Next Counter2
End If
For Counter2 = 1 To Int((LenPermString - Rotate + 1) / 2)
Dummy = Mid$(PermString, Rotate + Counter2 - 1, 1)
Mid$(PermString, Rotate + Counter2 - 1, 1) = Mid$(PermString, Len(PermString) - Counter2 + 1, 1)
Mid$(PermString, LenPermString - Counter2 + 1, 1) = Dummy
Next Counter2
DestRange.Offset(Counter1 - 1) = PermString
Next Counter1
Set DestRange = Nothing
End Sub
要是有算法,应该不是太难写。