Searching for specific set of characters in a label caption?
Hi everyone! I am working on a review game for students, and the beauty of this one is the students come up with the review questions, so they really want to add a lot of questions. Anyway I have two label captions.
Label1.caption and Label2.caption.
The question goes in Label1.caption and might be something like (1). What county is the Nile River located in?
What I am wondering is, how do I search for the number that is in the brackets? In this case 1 and just copy that number, and put it in label2.caption? The questions themselves will never have any brackets, so they won't confuse any search code, but has anyone tried this before? I would really appreciate it!
Thanks.
Re: Searching for specific set of characters in a label caption?
What you need to use is Mid$, Instr and (optionally) InstrRev functions:
Code:
Option Explicit
Private Sub Form_Load()
Label1.Caption = "(1). What county is the Nile River located in?"
End Sub
Private Sub Command1_Click()
Label2.Caption = VBA.Mid$(Label1.Caption, _
InStr(1, Label1.Caption, "(") + 1, _
InStrRev(Label1.Caption, ")") - 1 - InStr(1, Label1.Caption, "("))
End Sub
Re: Searching for specific set of characters in a label caption?
Try this
Code:
Private Sub Command1_Click()
MsgBox GetQuestionNumber("(1). What county is the Nile River located in?")
End Sub
Private Function GetQuestionNumber(ByVal strText As String) As String
Dim p1 As Long
Dim p2 As Long
Dim r As String
p1 = InStr(1, strText, "(")
p2 = InStr(1, strText, ")")
If p1 And p2 Then
r = Mid$(strText, p1 + 1, p2 - p1 - 1)
End If
GetQuestionNumber = r
End Function
BTW: the answer is my country Egypt :)