PDA

View Full Version : VBA to randomly replace an option in multiple-answer questions with a specific text



mpeterson
06-15-2015, 01:47 AM
I have a text file that contains over 6000 questions. I'm attaching a sample text file that contains 4 questions before editing, and then the same four after editing to give an idea about the desired outcome.

I need to edit the alternative answers by randomly including the sentence "No right answer exists" as an alternative answer in place of any answer that already exists from five answers for each question, as per the attached sample file, keeping the asterisks (*) and equation marks (=) as they exist.

As per attached file, all questions have exactly five options as alternative answers.

For a quick glance, here's the first question before and after editing:

Type: MC
1) The 'boot' shape of Italy is an example of what geographical feature?
~ pen·in·su·la Spelled Pronunciation[puh-nin-suh-luh, -nins-yuh-luh] –noun 1. an area of land almost completely surrounded by water except for an isthmus connecting it with the mainland.
@ pen·in·su·la Spelled Pronunciation[puh-nin-suh-luh, -nins-yuh-luh] –noun 1. an area of land almost completely surrounded by water except for an isthmus connecting it with the mainland.
*a. Peninsula
b. Ridge
c. Archipelago
d. Plateau
e. Continent

----------------------------------------------------------------

1) The 'boot' shape of Italy is an example of what geographical feature?
~ pen•in•su•la Spelled Pronunciation[puh-nin-suh-luh, -nins-yuh-luh] –noun 1. an area of land almost completely surrounded by water except for an isthmus connecting it with the mainland.
@ pen•in•su•la Spelled Pronunciation[puh-nin-suh-luh, -nins-yuh-luh] –noun 1. an area of land almost completely surrounded by water except for an isthmus connecting it with the mainland.
*a. No right answer exists …………………………………………………….. > (please note that this sentence replaced the original answer randomly, and kept the asterisk at the beginning)
b. Ridge
c. Archipelago
d. Plateau
e. Continent

Is there a way to make the sentence "No right answer exists" randomly replaces one options of five options in each question of those 6000+ questions?
Any assistance with this issue is always very much appreciated.
Thanks in advance.

gmayor
06-15-2015, 06:35 AM
The following will do what you ask IN THE EXAMPLE DOCUMENT, but surely it will also randomly replace the correct answers with the 'no right answer exists' text?



Sub NoRightAnswer()
Dim orng As Range
Dim oParas As Range
Dim i As Long
Dim j As Single
Dim vText As Variant
ActiveDocument.Range.InsertParagraphAfter
Set orng = ActiveDocument.Range
With orng.Find
Do While .Execute(FindText:="Type: ")
Set oParas = orng
Do Until oParas.Next.Text = Chr(13)
oParas.MoveEnd wdParagraph
Loop
i = oParas.Paragraphs.Count
oParas.Start = oParas.Paragraphs(i - 4).Range.Start
j = Int((5 * Rnd) + 1)
vText = Split(oParas.Paragraphs(j).Range.Text, Chr(46))
oParas.Paragraphs(j).Range.Text = _
vText(0) & Chr(46) & _
" No right answer exists" & vbCr
orng.Collapse 0
Loop
End With
ActiveDocument.Range.Paragraphs.Last.Range.Delete
lbl_Exit:
Exit Sub
End Sub

mpeterson
06-15-2015, 09:33 AM
Marvelous GMayor, really stunning. I ran the code and it did as exactly as desired. Thank you very much for your such invaluable input. All the best.