how to shuffle a Large String in a cell?

jamiguel77

Active Member
Joined
Feb 14, 2006
Messages
378
Office Version
  1. 2016
  2. 2010
  3. 2007
Platform
  1. Windows
  2. Web
hi in cell A1 i have this String

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.

in A2 i want the result shuffled random the string...


any advice?

Thanks
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
How about this?

2Book1.xlsx
AB
1ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567899pAiGHxSDUZV68bRLMqdeuXoNKnEh3TzcW4srfFlw50Bj2YtgaQOkJCP7Ivym1
2ABC123B23A1C
Sheet2
Cell Formulas
RangeFormula
B1:B2B1=SCRAMBLE(A1)


VBA Code:
Function SCRAMBLE(s As String)
Dim LS As Integer:      LS = Len(s)
Dim AL As Object:       Set AL = CreateObject("System.Collections.ArrayList")
Dim RI As Integer

For i = 1 To LS
    AL.Add Mid(s, i, 1)
Next i

Do Until AL.Count = 0
    RI = Int((AL.Count) * Rnd())
    SCRAMBLE = SCRAMBLE & AL.Item(RI)
    AL.removeat RI
Loop

End Function
 
Upvote 0
Solution
Much thanks, if can....

Based in your Sample, in A3 i want write by sample: APPLE
in A4 wich is the result ?

in this case would be: 9RRVG

Thanks
 
Upvote 0
@lrobbo314 Perhaps replace:

VBA Code:
Dim LS As Integer:      LS = Len(s)
Dim AL As Object:       Set AL = CreateObject("System.Collections.ArrayList")

For i = 1 To LS
    AL.Add Mid(s, i, 1)
Next i

with:

VBA Code:
    s = Split(StrConv(s, 64), Chr(0))       ' Create 1D zero based array of characters
    ReDim Preserve s(0 To UBound(s) - 1)    ' Remove the last blank slot from array 's'
 
Upvote 0
VBA Code:
    s = Split(StrConv(s, 64), Chr(0))       ' Create 1D zero based array of characters
    ReDim Preserve s(0 To UBound(s) - 1)    ' Remove the last blank slot from array 's'
I usually do the above this way...
VBA Code:
s = Split(Trim(Replace(StrConv(s, vbUnicode), Chr(0), " ")))
 
Upvote 0
Or try

VBA Code:
Public Function RandomChar(ByVal s As String) As String
Application.Volatile
Dim i As Long, lenS As Long, r As Long, temp As String
Randomize
lenS = Len(s)
For i = 1 To lenS
    r = Int(Rnd() * lenS) + 1
    temp = Mid(s, i, 1)
    Mid(s, i, 1) = Mid(s, r, 1)
    Mid(s, r, 1) = temp
Next i
RandomChar = s

End Function
 
Upvote 0
@lrobbo314 Perhaps replace:

VBA Code:
Dim LS As Integer:      LS = Len(s)
Dim AL As Object:       Set AL = CreateObject("System.Collections.ArrayList")

For i = 1 To LS
    AL.Add Mid(s, i, 1)
Next i

with:

VBA Code:
    s = Split(StrConv(s, 64), Chr(0))       ' Create 1D zero based array of characters
    ReDim Preserve s(0 To UBound(s) - 1)    ' Remove the last blank slot from array 's'

That really wouldn't work for what I am doing. The way I am doing it, it takes a random element from the arraylist, adds it to the result, then removes that element from the arraylist until it's empty.

Looks like I could do what your saying if I were to do something like what @Phouc suggested. Or use a byteArray, as well.

VBA Code:
Function SB(s As String) As String
Dim BA() As Byte:   BA = s
Dim MX As Integer:  MX = UBound(BA)
Dim POS As Integer

For i = 0 To MX Step 2
    POS = VBA.Round(Int(MX * rand) / 2) * 2
    tmp = BA(i)
    BA(i) = BA(POS)
    BA(POS) = tmp
Next i

SB = BA
End Function

But looking at Post#4, I think the OP isn't looking for a cypher or something because I guess APPLE should be converted to 9RRVG. The 2 Rs make me think that it's more about converting the string to an, for lack of a better term, 'encrypted' version of the string.
 
Upvote 0
If I'm correct and the OP is looking for a cypher, or whatever you would call it, this seems to do what he's looking for.

20220707 Stat Pull Venuti Deaths and Rescues II.xlsm
AB
1APPLEH00OT
2APPLEDOO2e
3APPLEqVVOL
4APPLENKKBc
5APPLEFggPD
6APPLEWNNHD
7APPLEdQQVX
8APPLERPPCC
9APPLELFFIK
10APPLEunnAd
11APPLEEQQ2O
12APPLEFEEkG
13APPLEjMMhI
14APPLEgCCEz
15APPLEOGGMY
16APPLEUWWNE
17APPLENMME0
18APPLEGRRNp
19APPLEnCCbj
20APPLEPqqDL
Sheet5
Cell Formulas
RangeFormula
B1:B20B1=ShuffleII(A1)


VBA Code:
Function ShuffleII(s As String) As String
Randomize
Dim ORG() As Byte:  ORG = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
Dim XA() As Byte:   XA = SB("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
Dim SA() As Byte:   SA = s

For i = 0 To UBound(SA) Step 2
    For j = 0 To UBound(ORG) Step 2
        If SA(i) = ORG(j) Then SA(i) = XA(j)
    Next j
Next i

ShuffleII = SA
End Function

Function SB(s As String)
Dim BA() As Byte:   BA = s
Dim MX As Integer:  MX = UBound(BA)
Dim POS As Integer

For i = 0 To MX Step 2
    Randomize
    POS = VBA.Round(Int(MX * Rnd()) / 2) * 2
    tmp = BA(i)
    BA(i) = BA(POS)
    BA(POS) = tmp
Next i

SB = BA
End Function
 
Upvote 0
Looks like I could do what your saying if I were to do something like what @Phouc suggested. Or use a byteArray, as well.

VBA Code:
Function SB(s As String) As String
Dim BA() As Byte:   BA = s
Dim MX As Integer:  MX = UBound(BA)
Dim POS As Integer

For i = 0 To MX Step 2
    POS = VBA.Round(Int(MX * rand) / 2) * 2
    tmp = BA(i)
    BA(i) = BA(POS)
    BA(POS) = tmp
Next i

SB = BA
End Function

That is, by far, the fastest code, thus far in this thread, to 'shuffle' the original string.

0.0000568709384424 Average Seconds on my old laptop. Nearly twice as fast as
Code from @Phuoc post #7 = 0.00009700909167802 Average Seconds

Version I came up with was a seemingly turtle paced 0.00027367855909684 Average Seconds. :(
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,852
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top