• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Code to check quiz answers with answer key... returns false info

lhkittle

New Member
The answers to a quiz are entered in Range("C12:C20").

The answer key is listed in Range("E12:E20") = AA, BB, CC, DD, EE, FF, GG, HH, II.


The quiz answers in Range("C12:C20) = WW, BB, CC, DD, WW, FF, GG, HH, II.

(Two wrong answers to test code)


Run the code:

The code says the first WW is correct, then when it advances to the cell with BB it says that WW is incorrect. That seems to be the reaccuring senario on down the list.

Basicly I just get false info back in the MsgBox replies.


Clearly, I am overlooking some fundamental concept in my coding but I'm at the point where I can't see the forest because of the trees!


The sounds are a positive like tone for a correct answer and negative like tone for an incorrect answer. They are not at issue here.


Thanks.

Howard

[pre]
Code:
Option Explicit
Option Compare Text
Private Declare Function sndPlaySound32 _
Lib "winmm.dll" _
Alias "sndPlaySoundA" ( _
ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long

Sub CheckAnswers()

Dim c As Range

For Each c In Range("C12:C20")

If c.Value = c.Offset(0, 2).Value Then sndPlaySound32 "C:WindowsMediaChimes.wav", 0&
MsgBox "Answer " & c & " is correct.", vbOKOnly
c.Offset(1, 0).Select

If c.Value <> c.Offset(0, 2).Value Then sndPlaySound32 "C:WindowsMediawoohoo.wav", 0&
MsgBox "Sorry, answer " & c & " is incorrect "
c.Offset(1, 0).Select
Next
End Sub
[/pre]
 
Try changing the 2 occurrences of

c.Value = c.Offset(0, 2).Value


To:

c.Text = c.Offset(0, 2).Text


Also make sure none of the answers in either column have leading or trailing spaces

Which can be done as:

Trim(c.Text) = Trim(c.Offset(0, 2).Text)
 
Thanks Hui for taking a look and the suggestion.


I made this change and verified no spaces.


If c.Text = c.Offset(0, 2).Text Then ....


The results are the same missinformation as before.


Seems that both IF statements are coming into play for each answer when the IF should disqualify one or the other depending on the answer.


I appreciate you time.


Howard
 
Hi Howard ,


The fundamental point is this :


The IF statements are being terminated with the call of the sndPlaySound32 executable.


When you use an IF statement , if any statement follows the then , the IF statement terminates on that line ; if you want to have several statements execute if the condition is true or false , then , you need to enclose them as follows :

[pre]
Code:
If c.Value = c.Offset(0, 2).Value Then
sndPlaySound32 "C:WindowsMediaChimes.wav", 0&
MsgBox "Answer " & c & " is correct.", vbOKOnly
c.Offset(1, 0).Select
End If
The correct way is to use it as follows :

If c.Value = c.Offset(0, 2).Value Then
sndPlaySound32 "C:WindowsMediaChimes.wav", 0&
MsgBox "Answer " & c & " is correct.", vbOKOnly
Else
sndPlaySound32 "C:WindowsMediawoohoo.wav", 0&
MsgBox "Sorry, answer " & c & " is incorrect "
End If
c.Offset(1, 0).Select
[/pre]
Narayan
 
Hi Narayan,


Yep, that does it. Thanks a ton.


Frankly I find the sound a bit distracting if not annoying.

I will offer a 'with sound' and a 'no sound' version to the end user.


Thanks again.


Regards,

Howard
 
Back
Top