# Thread: Scramble a string with conditions Thanks: 0 Likes: 0

1. ## Re: Scramble a string with conditions

And these two:
Code:
```comb = Left(comb, Len(comb) - 1)
comb = Right(comb, Len(comb) - Len(str) - 1)```
Could be replaced with:
Code:
`comb = Replace(Left(comb, Len(comb) - 1), str & ",", "")`

2. ## Re: Scramble a string with conditions

Alright.

Very comprehensive.

I am gradually updating my Intel

3. ## Re: Scramble a string with conditions

Originally Posted by footoo
And these two:
Code:
```comb = Left(comb, Len(comb) - 1)
comb = Right(comb, Len(comb) - Len(str) - 1)```
Could be replaced with:
Code:
`comb = Replace(Left(comb, Len(comb) - 1), str & ",", "")`
The code is return only one scrambled item after I updated the versions of code you suggested I replace.

Even after I returned to original it's still returning just one

4. ## Re: Scramble a string with conditions

The code just worked again mysteriously.

I was then using the i variable as Long. Could that be a reason?

5. ## Re: Scramble a string with conditions

Originally Posted by footoo
Code:
```Sub Set_String()
Dim com\$, ray As Variant, i%, comb\$
Dim str\$: str = "ABCD"
com = Get_Comb("", "", str)
ray = Remove_Dup(Split(com, ","))
For i = LBound(ray) To UBound(ray) - 1
comb = comb & ray(i) & ","
Next
comb = Left(comb, Len(comb) - 1)
comb = Right(comb, Len(comb) - Len(str) - 1)
End Sub

Function Get_Comb(comb As String, s1 As String, s2 As String)
Dim i%, sLen%
sLen = Len(s2)
If sLen < 2 Then
comb = comb & s1 & s2 & ","
Else
For i = 1 To sLen
Call Get_Comb(comb, s1 + Mid(s2, i, 1), Left(s2, i - 1) + Right(s2, sLen - i))
Next
End If
Get_Comb = comb
End Function

Function Remove_Dup(ray As Variant) As Variant
Dim i%, d As Object
Set d = CreateObject("Scripting.Dictionary")
For i = LBound(ray) To UBound(ray)
If IsMissing(ray(i)) = False Then d.Item(ray(i)) = 1
Next
Remove_Dup = d.Keys
End Function```

So I tried to store the items in a vertical list by doing:

Code:
```Code:
Sub Set_String()
Dim com\$, ray As Variant, i%, comb\$
Dim str\$: str = "ABCD"
com = Get_Comb("", "", str)
ray = Remove_Dup(Split(com, ","))
For i = LBound(ray) To UBound(ray) - 1
comb = comb & " " & i  + 1 & " " & ray(i) & vbCr
Next
comb = Left(comb, Len(comb) - 1)
comb = Right(comb, Len(comb) - Len(str) - 1)
End Sub

Function Get_Comb(comb As String, s1 As String, s2 As String)
Dim i%, sLen%
sLen = Len(s2)
If sLen < 2 Then
comb = comb & s1 & s2 & ","
Else
For i = 1 To sLen
Call Get_Comb(comb, s1 + Mid(s2, i, 1), Left(s2, i - 1) + Right(s2, sLen - i))
Next
End If
Get_Comb = comb
End Function

Function Remove_Dup(ray As Variant) As Variant
Dim i%, d As Object
Set d = CreateObject("Scripting.Dictionary")
For i = LBound(ray) To UBound(ray)
If IsMissing(ray(i)) = False Then d.Item(ray(i)) = 1
Next
Remove_Dup = d.Keys
End Function```
But the numbering is not looking good. Can someone help fix it for me?

6. ## Re: Scramble a string with conditions

Code:
```Sub Set_String()
Dim com\$, ray As Variant, i%, comb\$
Dim str\$: str = "ABBA"
com = Get_Comb("", "", str)
ray = Remove_Dup(Split(com, ","))
For i = 1 To UBound(ray) - 1
comb = comb & " " & i & " " & ray(i) & vbCrLf
Next
End Sub```

7. ## Re: Scramble a string with conditions

Originally Posted by footoo
Code:
```Sub Set_String()
Dim com\$, ray As Variant, i%, comb\$
Dim str\$: str = "ABBA"
com = Get_Comb("", "", str)
ray = Remove_Dup(Split(com, ","))
For i = 1 To UBound(ray) - 1
comb = comb & " " & i & " " & ray(i) & vbCrLf
Next
End Sub```
Wow!!!

This is unbelievable. Why did the vbcr give me some issues but not the vbcrlf ?