最近在用VBA排列組合一些數字,做4組數字排列組合的生成,那為何不往5組或更多數字呢!!
因為超過4組數字的1~39高達百萬,要用不同方法堆壘資料阿......
架構:先產生1~39的數字>轉換為陣列>透過迴圈組合數字>檢查有無重複>回寫EXCEL A行
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub 排列組合不重複() | |
For i = 1 To 39 Step 1 | |
If ARR = "" Then | |
ARR = i | |
Else | |
ARR = ARR & "," & i | |
End If | |
Next i | |
ARR = Split(ARR, ",") | |
Set d = CreateObject("scripting.dictionary") | |
For i = LBound(ARR) To UBound(ARR): | |
For J = LBound(ARR) To UBound(ARR): | |
For K = LBound(ARR) To UBound(ARR): | |
OUT = Format(ARR(i), "0#") & Format(ARR(J), "0#") & Format(ARR(K), "0#") | |
If Rept_CHECK(OUT, 2) = False Then d(ARR(i) & ARR(J) & ARR(K)) = "" | |
Next: | |
Next: | |
Next | |
[a1].Resize(d.Count, 1).NumberFormatLocal = "@" | |
[a1].Resize(d.Count, 1) = Application.Transpose(d.keys) | |
End Sub | |
Function Rept_CHECK(SOURCE_str, LEN_STR) As Boolean | |
Rept_CHECK = False | |
For i = 1 To Len(SOURCE_str) - 1 | |
For J = i + 1 To Len(SOURCE_str) | |
If Mid(SOURCE_str, i, LEN_STR) = Mid(SOURCE_str, J, LEN_STR) Then | |
Rept_CHECK = True | |
Exit For | |
End If | |
Next | |
Next | |
End Function |
沒有留言:
張貼留言