Formula to Replace Words in a Cell

joewell

New Member
Joined
Sep 6, 2016
Messages
8
Hi, I'm making an English teacher making quizzes based on lists of sentences with translations.

I want to make one formula that takes the text string of another cell and randomly replaces one word with an underscore ____.

I'd also like to make a formula that replaces all the words with underscores.

Examples below.

[I know it's possible to find words by counting spaces, my bigger challenge replace the found words.]


Input SentenceOutput Question
I am happy.I ____ happy.
We all enjoy our work.We all _____ our work.
The team is very good.The team is ______ good.
It is my favorite.____ ____ ____ ____
They didn't come in yet.____ _____ ____ ____ _____.
Why are you going there with them?_____ ______ _____ ____ ____ ____ ____?

<colgroup><col><col></colgroup><tbody>
</tbody>
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Welcome to the forum!

This might help. Copy C2 done as far as necessary.

ABC
1Input Sentencereplace this one word (no spaces)Output Question
2I am happy.amI _____ happy.
3We all enjoy our work.enjoyWe all _____ our work.
4The team is very good.veryThe team is _____ good.
5It is my favorite._____ _____ _____ _____ .
6They didn't come in yet._____ _____ _____ _____ _____ .
7Why are you going there with them?_____ _____ _____ _____ _____ _____ _____ ?

<colgroup><col style="******* 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet55

Worksheet Formulas
CellFormula
C2=IF(ISBLANK(B2),REPT("_____ ",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))+1)&RIGHT(A2),REPLACE(A2,SEARCH(B2,A2),LEN(B2)+1,"_____ "))

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>
 
Last edited:
Upvote 0
I realise that you want a randomiser, so I set about the task. This was irritating because two things complicate matters. Single-letter words (such as 'I' and 'a') are difficult to isolate. And the punctuation at the end of the sentence is difficult to handle. You can see from these formulas that text operations in Excel are seldom easy.

Try this. Copy B9 and C9 down as far as necessary. Press f9 to re-calculate the workbook and the B9 formula will randomly pick one of the words in A9 or a blank. The blank will force C9 to replace all the words with underscores.

I created some additional text data so I could conduct experiments. I would suggest that you omit sentences that have punctuation in the middle. All sentences in Column A must end with punctuation.

reference:
https://exceljet.net/formula/extract-nth-word-from-text-string

<tbody>
</tbody>


ABC
9The teacher is English.EnglishThe teacher is _____.
10The teacher is a good teacher of English!EnglishThe teacher is a good teacher of _____!
11Am I not a good teacher?_____ _____ _____ _____ _____ _____ ?
12a e i u o y.a_____ e i u o y.
13They didn't come in yet._____ _____ _____ _____ _____ .

<tbody>
</tbody>
Sheet55

Worksheet Formulas
CellFormula
B9=IFERROR(TRIM(MID(SUBSTITUTE(LEFT(A9,LEN(A9)-1)," ",REPT(" ",LEN(LEFT(A9,LEN(A9)-1)))), (RANDBETWEEN(0,LEN(A9)-LEN(SUBSTITUTE(A9," ",""))+1)-1)*LEN(LEFT(A9,LEN(A9)-1))+1, LEN(LEFT(A9,LEN(A9)-1)))),"")
C9=IF(B9="",REPT("_____ ",LEN(A9)-LEN(SUBSTITUTE(A9," ",""))+1)&RIGHT(A9),LEFT(REPLACE(LEFT(A9,LEN(A9)-1)&" ",SEARCH(IF(LEN(B9)=1,B9&" ",B9),LEFT(A9,LEN(A9)-1)&" "),LEN(B9)+1,"_____ "),LEN(REPLACE(LEFT(A9,LEN(A9)-1)&" ",SEARCH(IF(LEN(B9)=1,B9&" ",B9),LEFT(A9,LEN(A9)-1)&" "),LEN(B9)+1,"_____ "))-1)&RIGHT(A9))

<tbody>
</tbody>

<tbody>
</tbody>
 
Last edited:
Upvote 0
Wow, this is amazing. I'm speechless.

Just one question while I digest this: in column B does it *randomly* leave it blank 50% of the time, truly randomly, or only a certain %? Because when I've played with this it is only blank a small % of the time, maybe under 25% just eyeing it.
 
Upvote 0
You're welcome.

I'm quite sure that it is random. The function RANDBETWEEN here selects a number between 0 and a figure dictated by the number of words in the sentence (a figure whose value is determined by adding 1 to the number of spaces in the sentence). When a 0 is encountered, an error condition is reported by the formula, which is useful as an input to function IFERROR. When such an error arises, the formula in C9 will simply mete out the same number of 'underscores+space' as there are words in the sentence and then append it all with whatever punctuation concluded the original sentence.

So a sentence with five words will result in blanks one-sixth of the time. A sentence with four words will result in blanks one-fifth of the time.
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

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