ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 4535|回复: 5

列出最多 13 個數字或文字的排列

[复制链接]

TA的精华主题

TA的得分主题

发表于 2004-11-20 12:59 | 显示全部楼层 |阅读模式

以下的編碼可列出最多 13 個數字或文字的排列 (Permutations)

Sub PermutationsFromRange() Dim DestRange As Object Dim PermString As Variant Dim NewPerm As String Dim SepChar As String Dim NumOfElements 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

' 最多 13 個元素

PermString = Range("A1:A3") SepChar = ","

NumOfElements = UBound(PermString) NumOfPerm = Application.Fact(NumOfElements) For Counter1 = 1 To NumOfElements Factorial(Counter1) = Application.Fact(NumOfElements - Counter1) Next Counter1

Worksheets.Add Set DestRange = Range("a1")

For Counter1 = 1 To NumOfElements NewPerm = NewPerm & PermString(Counter1, 1) & SepChar Next Counter1 DestRange.Value = Left(NewPerm, Len(NewPerm) - Len(SepChar)) For Counter1 = 2 To NumOfPerm NewPerm = "" If Counter1 / 2 = Int(Counter1 / 2) Then Rotate = NumOfElements - 1 Else For Counter2 = 1 To NumOfElements - 2 If Counter1 Mod Factorial(Counter2) = 1 Then Rotate = Counter2 Exit For End If Next Counter2 End If For Counter2 = 1 To Int((NumOfElements - Rotate + 1) / 2) Dummy = PermString(Rotate + Counter2 - 1, 1) PermString(Rotate + Counter2 - 1, 1) = PermString(NumOfElements - Counter2 + 1, 1) PermString(NumOfElements - Counter2 + 1, 1) = Dummy Next Counter2 For Counter2 = 1 To NumOfElements NewPerm = NewPerm & PermString(Counter2, 1) & SepChar Next Counter2 DestRange.Offset(Counter1 - 1) = Left(NewPerm, Len(NewPerm) - Len(SepChar)) Next Counter1 Set DestRange = Nothing End Sub

EUfbkR3W.zip (8.46 KB, 下载次数: 97)
[此贴子已经被作者于2004-12-22 14:01:28编辑过]

TA的精华主题

TA的得分主题

 楼主| 发表于 2004-11-20 18:12 | 显示全部楼层

以下是字串方式

Sub PermutationsFromString() 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

' 最多 13 個元素 PermString = "123ABC"

LenPermString = Len(PermString) NumOfPerm = Application.Fact(LenPermString) For Counter1 = 1 To LenPermString Factorial(Counter1) = Application.Fact(LenPermString - Counter1) Next Counter1

Worksheets.Add Set DestRange = Range("a1")

DestRange.Value = PermString 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

TA的精华主题

TA的得分主题

 楼主| 发表于 2004-12-22 14:01 | 显示全部楼层

以下是字串方式 Combination

Sub CombinationsFromString() Dim DestRange As Object Dim CountOff() Dim MaxOff() Dim CombString As String Dim NewComb As String Dim NumOfComb As Long Dim Dummy Dim SubSet As Long Dim LenCombString As Long Dim Counter1 As Long Dim Counter2 As Long

CombString = "1234567890" ' Or Cells(1,1) ' 最多 13 個元素 SubSet = 5

LenCombString = Len(CombString) NumOfComb = Application.Combin(LenCombString, SubSet)

ReDim CountOff(SubSet) ReDim MaxOff(SubSet)

For Counter1 = 1 To SubSet CountOff(Counter1) = Counter1 MaxOff(Counter1) = LenCombString - SubSet + Counter1 Next Counter1

Worksheets.Add Set DestRange = Range("a1")

For Counter1 = 1 To NumOfComb NewComb = "" For Counter2 = 1 To SubSet NewComb = NewComb & Mid$(CombString, CountOff(Counter2), 1) Next Counter2 DestRange.Offset(Counter1 - 1) = NewComb CountOff(SubSet) = CountOff(SubSet) + 1 Dummy = SubSet While Dummy > 1 If CountOff(Dummy) > MaxOff(Dummy) Then CountOff(Dummy - 1) = CountOff(Dummy - 1) + 1 For Counter2 = Dummy To SubSet CountOff(Counter2) = CountOff(Counter2 - 1) + 1 Next Counter2 End If Dummy = Dummy - 1 Wend Next Counter1 End Sub

TA的精华主题

TA的得分主题

发表于 2005-5-13 11:09 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

先收藏,谢谢了!

我慢慢看!

TA的精华主题

TA的得分主题

发表于 2008-7-5 00:15 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2009-2-17 02:45 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-24 05:58 , Processed in 0.030574 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表