Take the left part of a math question, and cut off the right par after the = sign.
Hi there! I am working on a math review game for teachers. Basically, they come up with a list of math questions ex: 1+1=2. What I would like to do, is have the program cut off the question and put it in QuestionLBL.caption
In this case would be 1+1, and the answerLBL.caption would be 2. So I am looking for a way to cut off at the equal sign. :)
Would anyone know how to do that?
Thanks!
Re: Take the left part of a math question, and cut off the right par after the = sign
You can use the InStr function to return the position of the equal sign, then use Left$ to get the leftmost portion, and Mid$ to get the rightmost portion.
For example:
Code:
Public Sub SplitEquation(ByVal p_Equation As String)
Dim l_EqualPos As Long
l_EqualPos = InStr(p_Equation, 1, "=")
If l_EqualPos < 1 Then Err.Raise 5, , "Not an equation!"
Me.questionLBL.Caption = Left$(p_Equation, l_EqualPos)
Me.answerLBL.Caption = Mid$(p_Equation, l_EqualPos + 1)
End Sub
Note the above was written off the top of my head (untested in IDE), so there may be errors. It should point you in the right direction though.
Re: Take the left part of a math question, and cut off the right par after the = sign
The Split function could be your friend here.
Code:
Dim strEquation As String
Dim strQuestion As String
Dim strAnswer As String
Dim strParts() As String
strEquation = "1 + 1 = 2"
strParts = Split(strEquation, "=")
strQuestion = Trim(strParts(0))
strAnswer = Trim(strParts(1))
MsgBox "Question: " & strQuestion & vbCrLf & "Answer: " & strAnswer
Re: Take the left part of a math question, and cut off the right par after the = sign
Even better! Should remember not to post before my first coffee :)