Random Replace Characters in a Word

The_Rock

Board Regular
Joined
Jul 2, 2007
Messages
174
Hi Folks
I've been scouring the net but without success. I'm hoping the genius's here will be able to help me :)

I'm trying to mock up some work for my daughter to help her with Synonyms and Antonyms.
What I would like to do, if possible is:
Synonym.png


Column D looks at Column F (which will be hidden) and will randomly substitute some of the letters with an underscore.
In addition, Col D would have to space out the letters else all the underscores would look like a continuous line.
I'd like for the substitutions to be random, as I may give her the same words again at a later date but would like different letters to be missing.

I hope this makes sense in what I'm trying to achieve.
I assume it maybe a UDF that is needed but if you can help, that'll be very much appreciated.

Thank You
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
The problem with randomising this is that you will end up with no letters and 6 dashes _ _ _ _ _ _ eventually. Not much use to you that i presume? What is the minimum number of letters that need to remain?
 
Upvote 0
Thanks for replying Steve. I understand where you're coming from. I'm not sure if its possible, but I thought I'd check.
I think minimum of 3 letters should be sufficient. It won't be complex long words.
 
Upvote 0
Give this UDF a try. You can adjust the (approximate) proportion of blanks with the 'Const' line in the code.

VBA Code:
Function SubLetters(s As String) As String
  Dim i As Long, x As Long
  Dim Positions As Variant

  Const PercentageReplaced As Double = 0.5 '<- Adjust # of blanks with this between 0 & 1

  Randomize
  Positions = Split("#" & Join(Application.Transpose(Evaluate("row(1:" & Len(s) & ")")), "# #") & "#")
  For i = 1 To Len(s) * PercentageReplaced
    x = Replace(Positions(Int(Rnd * UBound(Positions))), "#", "")
    Mid(s, x, 1) = "_"
    Positions = Filter(Positions, "#" & x & "#", False)
  Next i
  SubLetters = Replace(s, "_", " _ ")
End Function

Example output

Book1
BCDEF
2import _ _ po _ texport
3cellara _ t _ cattic
4simple _ omp _ _ _ _ t _ dcomplicated
Sub letters
Cell Formulas
RangeFormula
D2:D4D2=subletters(F2)
 
Last edited:
Upvote 0
Wow - thank you so much Peter. That is awesome - just what I needed!!
As I said there are real genius's in this forum :)
 
Upvote 0
You're welcome. Thanks for the follow-up - and the kind words. :)
 
Upvote 0

Forum statistics

Threads
1,215,001
Messages
6,122,648
Members
449,092
Latest member
peppernaut

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