Originally Posted by
LeoLee
...1 of the loop hole in excel is that when u key in 0012, it will naturally display 12 and eliminate the 00.
So when I key in the combination of 0001 with your code, it will show 4 combinations, however, it will eliminate 000 infront. Is there a way to display the '0's? ...
Poorly tested and I am not sure if this gets rid of duplicates with the zeros included. I think you'd just want to change the numberformat of the destination cells to keep preceeding zeros. Also, you'll see that I ditched CLng() which would have also gotten rid of them, as I thought that we were doing numbers.
Again, not sure, but maybe:
Option Explicit
Sub Main()
Dim MyString As String
Dim ary As Variant
Dim aryOutput() As String
Dim i As Long, ii As Long, n As Long
Dim LS As String, RS As String
MyString = Application.InputBox("Input number to return unique permutations:", vbNullString, , , , , , 2)
If Len(MyString) < 2 Or Len(MyString) > 8 Then
MsgBox "The number either has only one digit, or is too great and may error. Now exiting.", 0, vbNullString
Exit Sub
End If
RS = MyString
Call RetUniquePerms(LS, RS, ary)
ReDim aryOutput(1 To (((UBound(ary) - LBound(ary) + 1) / 6) * 2 + 2), 1 To 6)
On Error GoTo ExceededArray
For i = 1 To UBound(aryOutput, 1) Step 2
For ii = 1 To 6
aryOutput(i, ii) = ary(n)
n = n + 1
Next
Next
ExceededArray:
With Range("H8").Resize(UBound(aryOutput, 1), 6)
.NumberFormat = "@"
.Value = aryOutput
End With
Call RetUniquePerms(LS, RS, ary, True)
End Sub
Function RetUniquePerms(LeftSide As String, RightSide As String, ary As Variant, Optional KillDic As Boolean)
Dim lLen As Long, i As Long
Static DIC As Object
If KillDic Then
Set DIC = Nothing
Exit Function
End If
If DIC Is Nothing Then Set DIC = CreateObject("Scripting.Dictionary")
lLen = Len(RightSide)
If lLen < 2 Then
DIC.Item(LeftSide & RightSide) = Empty
Else
For i = 1 To lLen
Call RetUniquePerms(LeftSide & Mid(RightSide, i, 1), _
Left(RightSide, i - 1) & Right(RightSide, lLen - i), ary)
Next
End If
ary = DIC.Keys
End Function
Hope that helps,
Mark