Auto Colour based on answer key

bushidowarrior

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

I have this table below. As you can see in the KEY column, is has the correct answer. In this case it is C.

Is there a was to make "WHAT" the correct answer highlight in GREEN?

And for the next row it would be D (They're)




i2amBDn.png



I would really appreciate it.

I have over 100 questions, hence I would like to automate this process a little.

Thank you.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Okay, yes, I am going to ask for something else. I know it is cheeky. However, I thought I can do this now and try to kill 2 birds with one stone:

In column L,M and N would have the other words, which are the wrong answers.

Would this be possible via VBA..

Thank you.. I assure you there is nothing else... You have been a tremendous help!

DEEk9oU.jpg
 
Upvote 0
Okay, yes, I am going to ask for something else. I know it is cheeky. However, I thought I can do this now and try to kill 2 birds with one stone:

In column L,M and N would have the other words, which are the wrong answers.

Would this be possible via VBA..

Thank you.. I assure you there is nothing else... You have been a tremendous help!

Ok, I'll get back to you tomorrow, almost midnight in my part of the world.;)
 
Upvote 0
Okay, yes, I am going to ask for something else. I know it is cheeky. However, I thought I can do this now and try to kill 2 birds with one stone:

In column L,M and N would have the other words, which are the wrong answers.

Would this be possible via VBA..

Thank you.. I assure you there is nothing else... You have been a tremendous help!

OK, try this:

Code:
[B][color=Royalblue]Sub[/color][/B] a1075737c()
[i][color=seagreen]'https://www.mrexcel.com/forum/excel-questions/1075737-auto-colour-based-answer-key.html[/color][/i]
[B][color=Royalblue]Dim[/color][/B] r [B][color=Royalblue]As[/color][/B] Range, s [B][color=Royalblue]As[/color][/B] [B][color=Royalblue]String[/color][/B]
[B][color=Royalblue]Dim[/color][/B] i [B][color=Royalblue]As[/color][/B] [B][color=Royalblue]Long[/color][/B], j [B][color=Royalblue]As[/color][/B] [B][color=Royalblue]Long[/color][/B], x [B][color=Royalblue]As[/color][/B] [B][color=Royalblue]Long[/color][/B]
[B][color=Royalblue]Dim[/color][/B] va, vb, ac
Application.ScreenUpdating = [B][color=Royalblue]False[/color][/B]

Range([color=brown]"F3:I"[/color] & Cells(Rows.count, [color=brown]"E"[/color]).[B][color=Royalblue]End[/color][/B](xlUp).row).Interior.Color = xlNone

[B][color=Royalblue]For[/color][/B] [B][color=Royalblue]Each[/color][/B] r [B][color=Royalblue]In[/color][/B] Range([color=brown]"E3"[/color], Cells(Rows.count, [color=brown]"E"[/color]).[B][color=Royalblue]End[/color][/B](xlUp))
    s = Trim(r)
    [B][color=Royalblue]If[/color][/B] s [B][color=Royalblue]Like[/color][/B] [color=brown]"[A-D]"[/color] [B][color=Royalblue]Then[/color][/B]
    x = r.row
        Range(s & x).Offset([color=crimson]0[/color], [color=crimson]5[/color]).Interior.Color = vbGreen
        Range([color=brown]"K"[/color] & x) = Range(s & x).Offset([color=crimson]0[/color], [color=crimson]5[/color])
        va = Range(Cells(x, [color=brown]"F"[/color]), Cells(x + [color=crimson]1[/color], [color=brown]"I"[/color]))
        [B][color=Royalblue]ReDim[/color][/B] vb([color=crimson]1[/color] [B][color=Royalblue]To[/color][/B] [color=crimson]1[/color], [color=crimson]1[/color] [B][color=Royalblue]To[/color][/B] UBound(va, [color=crimson]2[/color]))
        
        ac = Split([color=brown]"A,B,C,D"[/color], [color=brown]","[/color])
        
        [B][color=Royalblue]For[/color][/B] i = [color=crimson]1[/color] [B][color=Royalblue]To[/color][/B] UBound(va, [color=crimson]2[/color])
            va([color=crimson]2[/color], i) = ac(i - [color=crimson]1[/color])
        [B][color=Royalblue]Next[/color][/B]
        
        j = [color=crimson]2[/color]
        
        [B][color=Royalblue]For[/color][/B] i = [color=crimson]1[/color] [B][color=Royalblue]To[/color][/B] UBound(vb, [color=crimson]2[/color])
            [B][color=Royalblue]If[/color][/B] s = ac(i - [color=crimson]1[/color]) [B][color=Royalblue]Then[/color][/B]
                vb([color=crimson]1[/color], [color=crimson]1[/color]) = va([color=crimson]1[/color], i)
                [B][color=Royalblue]Else[/color][/B]
                vb([color=crimson]1[/color], j) = va([color=crimson]1[/color], i)
                j = j + [color=crimson]1[/color]
            [B][color=Royalblue]End[/color][/B] [B][color=Royalblue]If[/color][/B]
        [B][color=Royalblue]Next[/color][/B]
        Range([color=brown]"K"[/color] & x).Resize([color=crimson]1[/color], [color=crimson]4[/color]) = vb
        
    [B][color=Royalblue]End[/color][/B] [B][color=Royalblue]If[/color][/B]
[B][color=Royalblue]Next[/color][/B]
Application.ScreenUpdating = [B][color=Royalblue]True[/color][/B]
[B][color=Royalblue]End[/color][/B] [B][color=Royalblue]Sub[/color][/B]

RESULT

Excel 2007 32 bit
[Table="width:, class:head"][tr=bgcolor:#888888][th] [/th][th]
E
[/th][th]
F
[/th][th]
G
[/th][th]
H
[/th][th]
I
[/th][th]
J
[/th][th]
K
[/th][th]
L
[/th][th]
M
[/th][th]
N
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#888888]
1
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#888888]
2
[/td][td=bgcolor:#FFFF00]KEY[/td][td=bgcolor:#FFFF00]A[/td][td=bgcolor:#FFFF00]B[/td][td=bgcolor:#FFFF00]C[/td][td=bgcolor:#FFFF00]D[/td][td=bgcolor:#FFFF00][/td][td=bgcolor:#FFFF00]f[/td][td=bgcolor:#FFFF00]fr[/td][td=bgcolor:#FFFF00]t[/td][td=bgcolor:#FFFF00]g[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#888888]
3
[/td][td]B[/td][td]sgf[/td][td=bgcolor:#00FF00]gg[/td][td]h[/td][td]f[/td][td][/td][td]gg[/td][td]sgf[/td][td]h[/td][td]f[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#888888]
4
[/td][td]D[/td][td]g[/td][td][/td][td]d[/td][td=bgcolor:#00FF00]h[/td][td][/td][td]h[/td][td]g[/td][td][/td][td]d[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#888888]
5
[/td][td]A[/td][td=bgcolor:#00FF00]G[/td][td]rf[/td][td]hy[/td][td]r[/td][td][/td][td]G[/td][td]rf[/td][td]hy[/td][td]r[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#888888]
6
[/td][td]C[/td][td]sdfr[/td][td]gg[/td][td=bgcolor:#00FF00]f[/td][td]df[/td][td][/td][td]f[/td][td]sdfr[/td][td]gg[/td][td]df[/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Sheet1[/td][/tr][/table]
 
Upvote 0
I modified the above code a little to make it simpler:

Code:
[B][color=Royalblue]Sub[/color][/B] a1075737d()
[i][color=seagreen]'https://www.mrexcel.com/forum/excel-questions/1075737-auto-colour-based-answer-key.html[/color][/i]
[B][color=Royalblue]Dim[/color][/B] r [B][color=Royalblue]As[/color][/B] Range, s [B][color=Royalblue]As[/color][/B] [B][color=Royalblue]String[/color][/B]
[B][color=Royalblue]Dim[/color][/B] i [B][color=Royalblue]As[/color][/B] [B][color=Royalblue]Long[/color][/B], j [B][color=Royalblue]As[/color][/B] [B][color=Royalblue]Long[/color][/B], x [B][color=Royalblue]As[/color][/B] [B][color=Royalblue]Long[/color][/B], z [B][color=Royalblue]As[/color][/B] [B][color=Royalblue]Long[/color][/B]
[B][color=Royalblue]Dim[/color][/B] va, vb, ac
Application.ScreenUpdating = [B][color=Royalblue]False[/color][/B]

Range([color=brown]"F3:I"[/color] & Cells(Rows.count, [color=brown]"E"[/color]).[B][color=Royalblue]End[/color][/B](xlUp).row).Interior.Color = xlNone

[B][color=Royalblue]For[/color][/B] [B][color=Royalblue]Each[/color][/B] r [B][color=Royalblue]In[/color][/B] Range([color=brown]"E3"[/color], Cells(Rows.count, [color=brown]"E"[/color]).[B][color=Royalblue]End[/color][/B](xlUp))
    s = Trim(r)
    [B][color=Royalblue]If[/color][/B] s [B][color=Royalblue]Like[/color][/B] [color=brown]"[A-D]"[/color] [B][color=Royalblue]Then[/color][/B]
    x = r.row
        Range(s & x).Offset([color=crimson]0[/color], [color=crimson]5[/color]).Interior.Color = vbGreen
        va = Range(Cells(x, [color=brown]"F"[/color]), Cells(x, [color=brown]"I"[/color]))
        [B][color=Royalblue]ReDim[/color][/B] vb([color=crimson]1[/color] [B][color=Royalblue]To[/color][/B] [color=crimson]1[/color], [color=crimson]1[/color] [B][color=Royalblue]To[/color][/B] UBound(va, [color=crimson]2[/color]))
        
        j = [color=crimson]2[/color]
        [B][color=Royalblue]For[/color][/B] i = [color=crimson]1[/color] [B][color=Royalblue]To[/color][/B] UBound(vb, [color=crimson]2[/color])
        z = Range(s & [color=brown]"1"[/color]).Column
            [B][color=Royalblue]If[/color][/B] z = i [B][color=Royalblue]Then[/color][/B]
                vb([color=crimson]1[/color], [color=crimson]1[/color]) = va([color=crimson]1[/color], i)
                [B][color=Royalblue]Else[/color][/B]
                vb([color=crimson]1[/color], j) = va([color=crimson]1[/color], i)
                j = j + [color=crimson]1[/color]
            [B][color=Royalblue]End[/color][/B] [B][color=Royalblue]If[/color][/B]
        [B][color=Royalblue]Next[/color][/B]
        Range([color=brown]"K"[/color] & x).Resize([color=crimson]1[/color], [color=crimson]4[/color]) = vb
        
    [B][color=Royalblue]End[/color][/B] [B][color=Royalblue]If[/color][/B]
[B][color=Royalblue]Next[/color][/B]
Application.ScreenUpdating = [B][color=Royalblue]True[/color][/B]
[B][color=Royalblue]End[/color][/B] [B][color=Royalblue]Sub[/color][/B]
 
Upvote 0
Sorry for the delay.

Works Perfectly. THANK YOU VERY MUCH!!!!!!!!!!!!!!!!!!!!!!!!!!!

I modified the above code a little to make it simpler:

Code:
[B][COLOR=Royalblue]Sub[/COLOR][/B] a1075737d()
[I][COLOR=seagreen]'https://www.mrexcel.com/forum/excel-questions/1075737-auto-colour-based-answer-key.html[/COLOR][/I]
[B][COLOR=Royalblue]Dim[/COLOR][/B] r [B][COLOR=Royalblue]As[/COLOR][/B] Range, s [B][COLOR=Royalblue]As[/COLOR][/B] [B][COLOR=Royalblue]String[/COLOR][/B]
[B][COLOR=Royalblue]Dim[/COLOR][/B] i [B][COLOR=Royalblue]As[/COLOR][/B] [B][COLOR=Royalblue]Long[/COLOR][/B], j [B][COLOR=Royalblue]As[/COLOR][/B] [B][COLOR=Royalblue]Long[/COLOR][/B], x [B][COLOR=Royalblue]As[/COLOR][/B] [B][COLOR=Royalblue]Long[/COLOR][/B], z [B][COLOR=Royalblue]As[/COLOR][/B] [B][COLOR=Royalblue]Long[/COLOR][/B]
[B][COLOR=Royalblue]Dim[/COLOR][/B] va, vb, ac
Application.ScreenUpdating = [B][COLOR=Royalblue]False[/COLOR][/B]

Range([COLOR=brown]"F3:I"[/COLOR] & Cells(Rows.count, [COLOR=brown]"E"[/COLOR]).[B][COLOR=Royalblue]End[/COLOR][/B](xlUp).row).Interior.Color = xlNone

[B][COLOR=Royalblue]For[/COLOR][/B] [B][COLOR=Royalblue]Each[/COLOR][/B] r [B][COLOR=Royalblue]In[/COLOR][/B] Range([COLOR=brown]"E3"[/COLOR], Cells(Rows.count, [COLOR=brown]"E"[/COLOR]).[B][COLOR=Royalblue]End[/COLOR][/B](xlUp))
    s = Trim(r)
    [B][COLOR=Royalblue]If[/COLOR][/B] s [B][COLOR=Royalblue]Like[/COLOR][/B] [COLOR=brown]"[A-D]"[/COLOR] [B][COLOR=Royalblue]Then[/COLOR][/B]
    x = r.row
        Range(s & x).Offset([COLOR=crimson]0[/COLOR], [COLOR=crimson]5[/COLOR]).Interior.Color = vbGreen
        va = Range(Cells(x, [COLOR=brown]"F"[/COLOR]), Cells(x, [COLOR=brown]"I"[/COLOR]))
        [B][COLOR=Royalblue]ReDim[/COLOR][/B] vb([COLOR=crimson]1[/COLOR] [B][COLOR=Royalblue]To[/COLOR][/B] [COLOR=crimson]1[/COLOR], [COLOR=crimson]1[/COLOR] [B][COLOR=Royalblue]To[/COLOR][/B] UBound(va, [COLOR=crimson]2[/COLOR]))
        
        j = [COLOR=crimson]2[/COLOR]
        [B][COLOR=Royalblue]For[/COLOR][/B] i = [COLOR=crimson]1[/COLOR] [B][COLOR=Royalblue]To[/COLOR][/B] UBound(vb, [COLOR=crimson]2[/COLOR])
        z = Range(s & [COLOR=brown]"1"[/COLOR]).Column
            [B][COLOR=Royalblue]If[/COLOR][/B] z = i [B][COLOR=Royalblue]Then[/COLOR][/B]
                vb([COLOR=crimson]1[/COLOR], [COLOR=crimson]1[/COLOR]) = va([COLOR=crimson]1[/COLOR], i)
                [B][COLOR=Royalblue]Else[/COLOR][/B]
                vb([COLOR=crimson]1[/COLOR], j) = va([COLOR=crimson]1[/COLOR], i)
                j = j + [COLOR=crimson]1[/COLOR]
            [B][COLOR=Royalblue]End[/COLOR][/B] [B][COLOR=Royalblue]If[/COLOR][/B]
        [B][COLOR=Royalblue]Next[/COLOR][/B]
        Range([COLOR=brown]"K"[/COLOR] & x).Resize([COLOR=crimson]1[/COLOR], [COLOR=crimson]4[/COLOR]) = vb
        
    [B][COLOR=Royalblue]End[/COLOR][/B] [B][COLOR=Royalblue]If[/COLOR][/B]
[B][COLOR=Royalblue]Next[/COLOR][/B]
Application.ScreenUpdating = [B][COLOR=Royalblue]True[/COLOR][/B]
[B][COLOR=Royalblue]End[/COLOR][/B] [B][COLOR=Royalblue]Sub[/COLOR][/B]
 
Upvote 0
You're welcome.:)

Hello again, thank you once again.

I have been playing with the code and i would like to add one more thing please which I cannot seem to get working. I have added some other small things, but this one I cannot figure out.

This is what you code does at the moment:

YGqCNBA.png


I would like it to be like this please:
5ZeQCVD.png


Thank you
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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