Random word order in Excel, 1 Cell

bushidowarrior

Board Regular
Joined
Jun 27, 2011
Messages
84
Office Version
  1. 2019
Platform
  1. Windows
Hello all.

I think I have a dozy of an issue. i have been looking online with no anvil.

In A1 i have this text.

Hello world, how are you today.

I would like cell A2 to show:

world, / Hello / are / you / how / today.

Effectively a random word order. The space between the word will have a "/".

Is this possible? I would like to keep the sentence in 1 cell.

Thank you
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Is it acceptable to use a macro?

If need be, of course... However, if it were possible without it great.

PS. If there is a use of a macro. It would go down and execute is per a row. e.g.

A1 (text) > A2 (results)
A2 (text) > A2 (results)
A3 (text) > A3 (results)
A4 (text) > A4 (results)
A5 (text) > A5 (results)
A6 (text) > A6 (results)

Thank you very much!!!!!!!
 
Upvote 0
If need be, of course...
Then you could try this user-defined function. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy down.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

Code:
Function RandWords(s As String) As String
  Dim bits As Variant
  Dim tmp As String
  Dim i As Long, r As Long
  
  Randomize
  bits = Split(s)
  For i = 0 To UBound(bits)
    r = i + Int(Rnd * UBound(bits) - i)
    tmp = bits(i)
    bits(i) = bits(r)
    bits(r) = tmp
  Next i
  RandWords = Join(bits, " / ")
End Function

Excel Workbook
AB
1Hello world, how are you today.world, / are / you / Hello / today. / how
2Here is another sentenceanother / sentence / is / Here
3Ron's cat is blackcat / black / is / Ron's
Random Sentence
 
Last edited:
Upvote 0
Give this UDF (user defined function a try)...
Code:
Function RandOrder(S As String) As String
  Dim Cnt As Long, RandomIndex As Long, Tmp As Variant, Arr As Variant
  Randomize
  Arr = Split(S)
  For Cnt = UBound(Arr) To 0 Step -1
    RandomIndex = Int((Cnt + 1) * Rnd)
    Tmp = Arr(RandomIndex)
    Arr(RandomIndex) = Arr(Cnt)
    Arr(Cnt) = Tmp
  Next
  RandOrder = Join(Arr, " / ")
End Function

HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use RandOrder just like it was a built-in Excel function. For example,

=RandOrder(A1)

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Last edited:
Upvote 0
This could theoretically fail on occasions but if you have the TEXTJOIN & CONCAT functions (Office 365) and can have a couple of helper columns (C:D for me & could be hidden) then I think this worksheet formula approach should work for up to 10 words most of the time. If it does fail, it will show an error, not an 'incorrect' mixed sentence.

All formulas copied down. C1 is an array formula so should be entered without the {} but confirmed with Ctrl+Shift+Enter, not just Enter. If confirmed correctly, Excel will insert the {}. The formula can then be copied down.

If you use this and are finding formula failures too often, that risk can be reduced by increasing the number of RANDBETWEENS in column D with a corresponding adjustment to the 56 values in column C. For example, if you double the RANDBETWEENS from 4 to 8, change the 56s in col C to 112 (& don't forget to re-confirm with Ctrl+Shift+Enter before copying down.)


Excel Workbook
ABCD
1Hello world, how are you today.today. / world, / how / are / Hello / you512739046851273729575045225906984890477656001095340382808661850921
2Here is another sentenceis / Here / sentence / another619458037261964658061609855379122877318620061504689777592875974880
3Ron's cat is blackRon's / is / cat / black406872591340468047255902169701013586218926961183941267298756308109
4One two three four five six seven eight nine tennine / One / two / ten / four / seven / six / three / eight / five801936527480191319318865920376036295111885902296123433295097928937
Random Sentence (2)
 
Last edited:
Upvote 0
Thank you very much peter. Works perfectly! I had a look at your code, and I understand the logic. Very good!
 
Upvote 0
After looking that is, a macro is a much easier approach! Even more so, is this function exclusive to office 360 as I only have 2016.

Thank you
 
Upvote 0
Thank you very much peter. Works perfectly! I had a look at your code, and I understand the logic. Very good!
You're welcome.


.. is this function exclusive to office 360 as I only have 2016.
If you purchased stand-alone Excel 2016 you will not have the CONCAT and TEXTJOIN functions. If you purchased Excel 2016 through an Office 365 subscription then you will.
 
Upvote 0

Forum statistics

Threads
1,223,932
Messages
6,175,468
Members
452,646
Latest member
tudou

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