Help writing this code correctly

P

Preschool Mike

I need some help writing this code correctly. I want it to check each If
statment but it obviously stops after the first. How can I correctly write
this so all of them are ran? I'd like it to work as follows: If the first
If statement is true then RightOrder runs, if not true then WrongOrder runs.
Then it should proceed to the next If statement and checks those conditions
and so on.

Sub MoveBlocksTo(theAnswerBox As Shape)
MyBlockAnswers.Fill.ForeColor.RGB = vbYellow
MyBlockAnswers.Top = theAnswerBox.Top + 15
MyBlockAnswers.Left = theAnswerBox.Left + 15
If MyBlockAnswers.Name = "Block1" And theAnswerBox.Name = "Block1Box" Then
RightOrder
Else
WrongOrder
End If
If MyBlockAnswers.Name = "Block2" And theAnswerBox.Name = "Block2Box" Then
RightOrder
Else
WrongOrder
End If

If MyBlockAnswers.Name = "Block3" And theAnswerBox.Name = "Block3Box" Then
RightOrder
Else
WrongOrder
End If

If MyBlockAnswers.Name = "Block4" And theAnswerBox.Name = "Block4Box" Then
RightOrder
Else
WrongOrder
End If

If MyBlockAnswers.Name = "Block5" And theAnswerBox.Name = "Block5Box" Then
RightOrder
Else
WrongOrder
End If

End If
End Sub

Thanks,
Mike
 
B

Bill Dilworth

Personally, I might try to simplify the code a bit.

Sub MoveBlocksTo(theAnswerBox As Shape)

With MyBlockAnswers
.Fill.ForeColor.RGB = vbYellow
.Top = theAnswerBox.Top + 15
.Left = theAnswerBox.Left + 15
End With

If Left(UCase(MyBlockAnswers.Name), 6) = _
Left(UCase(theAnswerBox.Name), 6) Then
RightOrder
Else
WrongOrder
End If

End Sub

But that's just me. Either works the same.
Bill Dilworth
 
P

Preschool Mike

Actually I was able to solve my problem with the below code, but your code is
much neater. I now have a follow up question. What if I want to know which
BlockAnswers they put in theAnswerBoxes so I can display them later on a
printable slide. I've already set up the code for the printable slide and
the code to display the answer (kind of) just need to know how to gather the
information. Again, here is the code I'm currently using, however if you
have a much nicer/neater suggestion that's great.


Sub MoveBlocksTo(theAnswerBox As Shape)
MyBlockAnswers.Fill.ForeColor.RGB = vbYellow
MyBlockAnswers.Top = theAnswerBox.Top + 15
MyBlockAnswers.Left = theAnswerBox.Left + 15
If MyBlockAnswers.Name = "Block1" And theAnswerBox.Name = "Block1Box" Then
RightOrder

ElseIf MyBlockAnswers.Name = "Block2" And theAnswerBox.Name = "Block2Box" Then
RightOrder

ElseIf MyBlockAnswers.Name = "Block3" And theAnswerBox.Name = "Block3Box" Then
RightOrder
ElseIf MyBlockAnswers.Name = "Block4" And theAnswerBox.Name = "Block4Box" Then
RightOrder
ElseIf MyBlockAnswers.Name = "Block5" And theAnswerBox.Name = "Block5Box" Then
RightOrder
Else
WrongOrder
End If
End Sub

Sub CheckAnswer5()
If numRightOrder = 5 Then
numCorrect = numCorrect + 1
Ques5Answer = "Correct order" 'This answer displays on my printable slide.
Since they got the order correct (i.e., 1, 2, 3, 4, 5) I don't need to know
the order.
Else
NotRtOrder
Ques5Answer = 'This is where I want to put the order of the boxes they
choose which will later show up on my printable slide. Since they got the
question wrong I'd like to know which numbers they are putting in the wrong
order. I have a Dim statement set up for this already. Just need to get the
info to it.
End If
CleanWrongOrder
CleanNumOrder
ActivePresentation.SlideShowWindow.View.Next
End Sub

Thanks,

Mike
 
D

David Marcovitz

OK. I'm sure there are some slick ways to do this with two lines of code,
but I usually settle for the ways that I can get my mind around (using lots
of If statements):

If theAnswerBox.Name = "Block1Box" Then
answerBlock1Box = MyBlockAnswers.Name
ElseIf theAnswerBox.Name = "Block2Box" Then
answerBlock2Box = myBlockAnswers.Name
ElseIf theAnswerBox.Name = "Block3Box" Then
answerBlock3Box = myBlockAnswers.Name
ElseIf theAnswerBox.Name = "Block4Box" Then
answerBlock4Box = myBlockAnswers.Name
ElseIf theAnswerBox.Name = "Block5Box" Then
answerBlock5Box = myBlockAnswers.Name
End If

You'll have global variables for answerBlock1Box, answerBlock2Box, ....
These will be set to store the answers of what is in each of the answer
boxes. This could be done in a couple of lines of code with arrays and text
manipulation of the theAnswerBox.Name, but the multiple If statements should
work just fine.

--David

Actually I was able to solve my problem with the below code, but your code is
much neater. I now have a follow up question. What if I want to know which
BlockAnswers they put in theAnswerBoxes so I can display them later on a
printable slide. I've already set up the code for the printable slide and
the code to display the answer (kind of) just need to know how to gather the
information. Again, here is the code I'm currently using, however if you
have a much nicer/neater suggestion that's great.


Sub MoveBlocksTo(theAnswerBox As Shape)
MyBlockAnswers.Fill.ForeColor.RGB = vbYellow
MyBlockAnswers.Top = theAnswerBox.Top + 15
MyBlockAnswers.Left = theAnswerBox.Left + 15
If MyBlockAnswers.Name = "Block1" And theAnswerBox.Name = "Block1Box" Then
RightOrder

ElseIf MyBlockAnswers.Name = "Block2" And theAnswerBox.Name = "Block2Box" Then
RightOrder

ElseIf MyBlockAnswers.Name = "Block3" And theAnswerBox.Name = "Block3Box" Then
RightOrder
ElseIf MyBlockAnswers.Name = "Block4" And theAnswerBox.Name = "Block4Box" Then
RightOrder
ElseIf MyBlockAnswers.Name = "Block5" And theAnswerBox.Name = "Block5Box" Then
RightOrder
Else
WrongOrder
End If
End Sub

Sub CheckAnswer5()
If numRightOrder = 5 Then
numCorrect = numCorrect + 1
Ques5Answer = "Correct order" 'This answer displays on my printable slide.
Since they got the order correct (i.e., 1, 2, 3, 4, 5) I don't need to know
the order.
Else
NotRtOrder
Ques5Answer = 'This is where I want to put the order of the boxes they
choose which will later show up on my printable slide. Since they got the
question wrong I'd like to know which numbers they are putting in the wrong
order. I have a Dim statement set up for this already. Just need to get the
info to it.
End If
CleanWrongOrder
CleanNumOrder
ActivePresentation.SlideShowWindow.View.Next
End Sub

Thanks,

Mike

--
David M. Marcovitz
Author of _Powerful PowerPoint for Educators_
http://www.PowerfulPowerPoint.com/
Microsoft PowerPoint MVP
Associate Professor, Loyola University Maryland
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top