You are here:

- Home
- Computing/Technology
- Business Software
- Excel
- randomising letters

Advertisement

QUESTION: Hi Tom

In cell A2 I have a string of letters e.g "adkfjrushyeifhgjry". I wish to write a formula or VBA code that will shuffle these letters and place them in cell B2.

If this is possible, could you show me how?

Thanks in advance

Chris

ANSWER: Christopher Mitchell,

It would be tough to do with a single formula. Here is some VBA that will do it.

Sub randomizeletters()

Dim r As Range, s As String, cnt As Long

Dim i As Long, j As Long, v As Variant

Dim swp1 As Variant,

Dim swp2 As Variant

Dim s1 As String

' set r to refer to the cell with the string (target cell)

Set r = Range("A2")

s = r.Value

cnt = Len(s)

ReDim v(1 To cnt, 1 To 2)

' load each character into an element of a

' two dimensional array. Add a random number

' to each character in the second dimension

For i = 1 To cnt

v(i, 1) = Mid(s, i, 1)

v(i, 2) = Rnd()

Next

' do a bubble sort on the second dimension (random numbers)

' to produce a randomized sequence of letters

For i = 1 To cnt - 1

For j = i + 1 To cnt

If v(i, 2) > v(j, 2) Then

swp1 = v(i, 1)

swp2 = v(i, 2)

v(i, 1) = v(j, 1)

v(i, 2) = v(j, 2)

v(j, 1) = swp1

v(j, 2) = swp2

End If

Next

Next

' rebuild the randomized string from the array

s1 = ""

For i = 1 To cnt

s1 = s1 & v(i, 1)

Next

' this places the results in the cell to the right

' of the original target cell.

r.Offset(0, 1).Value = s1

' you could hard code a location like

' Range("B2").value = s1

End Sub

--

Regards,

Tom Ogilvy

---------- FOLLOW-UP ----------

QUESTION: Hi Tom

Thank you for this response. When I paste this in to a new module the following line appears red:

Dim swp1 As Variant,

Could explain why this might be the case?

Thanks in advance

Chris Mitchell

ANSWER: Christopher Mitchell,

I wrote the code and tested it. It worked fine. I was cleaning it up/making it pretty to make sure all variables were declared and so forth and adding some comments in the code.

This was after I copied it to this posting. At some point lost focus on the cursor (hit a stray key) and the line

Dim swp1 as string, swp2 as string

got separated into

Dim swp1 as string,

swp2 as string

I noticed the missing dim statement and thought I had inadvertently deleted it so I put in

Dim swp1 as string,

Dim swp2 as string

but didn't notice the comma on the end. So the comma needs to be removed. It may not been exactly like that, but something close.

Bottom line, even though I tested the code and it worked perfectly it got a little broken in the posting.

Here a copy of the tested code:

Sub randomizeletters()

Dim r As Range, s As String, cnt As Long

Dim i As Long, j As Long, v As Variant

Dim swp1 As Variant, swp2 As Variant

Dim s1 As String

Set r = Range("A2")

s = r.Value

cnt = Len(s)

ReDim v(1 To cnt, 1 To 2)

For i = 1 To cnt

v(i, 1) = Mid(s, i, 1)

v(i, 2) = Rnd()

Next

For i = 1 To cnt - 1

For j = i + 1 To cnt

If v(i, 2) > v(j, 2) Then

swp1 = v(i, 1)

swp2 = v(i, 2)

v(i, 1) = v(j, 1)

v(i, 2) = v(j, 2)

v(j, 1) = swp1

v(j, 2) = swp2

End If

Next

Next

s1 = ""

For i = 1 To cnt

s1 = s1 & v(i, 1)

Next

r.Offset(0, 1).Value = s1

End Sub

or if i just remove the comma:

Sub randomizeletters()

Dim r As Range, s As String, cnt As Long

Dim i As Long, j As Long, v As Variant

Dim swp1 As Variant

Dim swp2 As Variant

Dim s1 As String

Set r = Range("A2")

s = r.Value

cnt = Len(s)

ReDim v(1 To cnt, 1 To 2)

For i = 1 To cnt

v(i, 1) = Mid(s, i, 1)

v(i, 2) = Rnd()

Next

For i = 1 To cnt - 1

For j = i + 1 To cnt

If v(i, 2) > v(j, 2) Then

swp1 = v(i, 1)

swp2 = v(i, 2)

v(i, 1) = v(j, 1)

v(i, 2) = v(j, 2)

v(j, 1) = swp1

v(j, 2) = swp2

End If

Next

Next

s1 = ""

For i = 1 To cnt

s1 = s1 & v(i, 1)

Next

r.Offset(0, 1).Value = s1

End Sub

--

Regards,

Tom Ogilvy

---------- FOLLOW-UP ----------

QUESTION: Just fantastic. Thank you so much.

One very lasat question. is it possible to change the range from "A2:A500 so that whatever is in the new range will be shuffled in range "B2:B500?

Thanks again in advance

Chris

Christopher Mitchell,

I assume you mean A2 will be shuffled to B2, A3 shuffled to B3 and so forth. If so paste this code into the same module and run the subroutine Main:

Sub Main()

Dim cell As Range

For Each cell In Range("A2:A500")

randomizeletters cell

Next

End Sub

Sub randomizeletters(r As Range)

Dim s As String, cnt As Long

Dim i As Long, j As Long, v As Variant

Dim swp1 As Variant

Dim swp2 As Variant

Dim s1 As String

s = r.Value

cnt = Len(s)

ReDim v(1 To cnt, 1 To 2)

For i = 1 To cnt

v(i, 1) = Mid(s, i, 1)

v(i, 2) = Rnd()

Next

For i = 1 To cnt - 1

For j = i + 1 To cnt

If v(i, 2) > v(j, 2) Then

swp1 = v(i, 1)

swp2 = v(i, 2)

v(i, 1) = v(j, 1)

v(i, 2) = v(j, 2)

v(j, 1) = swp1

v(j, 2) = swp2

End If

Next

Next

s1 = ""

For i = 1 To cnt

s1 = s1 & v(i, 1)

Next

r.Offset(0, 1).Value = s1

End Sub

--

Regards,

Tom Ogilvy

- Add to this Answer
- Ask a Question

Rating(1-10) | Knowledgeability = 10 | Clarity of Response = 10 | Politeness = 10 |

Comment | Brilliant. Very brilliant. Thank you. |

This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

Answers by Expert:

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Extensive experience. **Education/Credentials**

Master of Science (MS) degree Operations Research (ORSA)**Awards and Honors**

Microsoft MVP in Excel.