Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-17-2017, 12:40 AM
puff puff is offline Automatically color the right options in red based on the answers provided Windows 7 64bit Automatically color the right options in red based on the answers provided Office 2013
Advanced Beginner
Automatically color the right options in red based on the answers provided
 
Join Date: Apr 2017
Posts: 52
puff is on a distinguished road
Question Automatically color the right options in red based on the answers provided

I'm preparing an answer key for my students and my document looks like this:
Q1:XXXXXXXX?
1. XXX
2. XXX
3. XXX


4. XXX
2 (option 2. is the correct answer to the 1st question)
(a blank line)
Q2:XXX?
...(repeat)

Is that possible to write a macro to color the respective correct options into red based on the key provided on every 7th line and then delete all those lines? Does anyone have a hint?

Last edited by puff; 12-17-2017 at 02:57 PM.
Reply With Quote
  #2  
Old 12-17-2017, 05:22 AM
gmayor's Avatar
gmayor gmayor is offline Automatically color the right options in red based on the answers provided Windows 10 Automatically color the right options in red based on the answers provided Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,018
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If the document is exactly as you describe then the following should work. It does however rely on there being the same exact number of lines for each question and answer.
Code:
Sub MarkAnswers()
Dim oRng As Range, oAns As Range
Dim iQuest As Integer, iAns As Integer
Const sNumList As String = "0123456789"
    On Error GoTo lbl_Exit
    For iQuest = 1 To ActiveDocument.Paragraphs.Count Step 7 'step through the questions
        Set oRng = ActiveDocument.Paragraphs(iQuest).Range 'get the first paragraph of the question
        oRng.MoveEnd wdParagraph, 6 'Get the last empty paragraph of the block
        Set oAns = oRng.Paragraphs(6).Range 'set a range to the answer line
        oAns.Collapse 1 'collapse the range to its start
        oAns.MoveEndWhile sNumList 'move the end of the range to the end of the answer number
        If Len(oAns.Text) > 0 Then 'if there is a number on that line
            iAns = oAns.Text + 1 'add a 1 to that number - to correspond to the answer number
            oRng.Paragraphs(iAns).Range.Font.ColorIndex = wdRed ' colour that paragraph red
        End If
    Next iQuest 'and process the next question
lbl_Exit: 'clear up
    Set oRng = Nothing
    Set oAns = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 12-17-2017, 02:53 PM
puff puff is offline Automatically color the right options in red based on the answers provided Windows 7 64bit Automatically color the right options in red based on the answers provided Office 2013
Advanced Beginner
Automatically color the right options in red based on the answers provided
 
Join Date: Apr 2017
Posts: 52
puff is on a distinguished road
Default

Thank you Graham! It colors the right options nicely. Yet after the coloring, it does not delete the lines that provide the answers (in the example, the line of "2 (option 2. is the correct answer to the 1st question)" need to be deleted). I tried to do so by adding "oAns.Delete" after "End If" but then it only deletes the number, not the whole line/paragraph.
Reply With Quote
  #4  
Old 12-17-2017, 10:17 PM
gmayor's Avatar
gmayor gmayor is offline Automatically color the right options in red based on the answers provided Windows 10 Automatically color the right options in red based on the answers provided Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,018
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Sorry. I missed that bit

Add the following two lines before Next iQuest i.e.
Code:
        oAns.End = oAns.Paragraphs(1).Range.End - 1
        oAns.Text = ""
    Next iQuest    'and process the next question
This will remove the text from the line with the answer. If you remove the line it will screw up the count. However if you don't want the extra empty lines, use the following:
Code:
Sub ClearBlanks()
Dim oPara As Paragraph
    On Error Resume Next
    For Each oPara In ActiveDocument.Paragraphs
        If Len(oPara.Range) = 1 Then If Len(oPara.Range.Next.Paragraphs(1).Range) = 1 Then oPara.Range.Delete
    Next oPara
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 12-26-2017, 10:36 PM
puff puff is offline Automatically color the right options in red based on the answers provided Windows 7 64bit Automatically color the right options in red based on the answers provided Office 2013
Advanced Beginner
Automatically color the right options in red based on the answers provided
 
Join Date: Apr 2017
Posts: 52
puff is on a distinguished road
Default

They work perfectly! Thank you so much!
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Change Color (Being provided between the two codes) bnyamin Word VBA 4 05-16-2017 10:55 PM
Macro- find and replace from preselected options based on frequency (like an Ad libs) blondie Word VBA 0 02-27-2017 01:41 PM
Automatically color the right options in red based on the answers provided Cell Color based on date jrfoley3 Excel 3 08-26-2016 10:49 PM
Automatically color the right options in red based on the answers provided Need to create a "Yes/NO" Form and based on answers prompt for informatin jaradani Word 4 10-12-2014 09:42 PM
Changing bar color automatically Mahmuz PowerPoint 0 03-28-2012 11:49 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:41 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2023 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft