Select Text between two points

Hello All,


I need to select text between two points.


The information between the underscore charactor ( _ ) and the period ( . ) charactor. The information between these points (HHHH-YY) are never the same length.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Private Sub Command1_Click()
Dim strExample As String, NewStr As String
strExample = "112344_123456.htm"

Dim pos1 As Integer, pos2 As Integer
pos1 = InStr(strExample, "_") + 1
pos2 = InStr(strExample, ".")

NewStr = Mid$(strExample, pos1, pos2 - pos1)

MsgBox NewStr

End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonMiddle School Assistant TeacherCommented:
Option Explicit

Private Sub Command1_Click()
    Dim strInput As String
    Dim underscore As Integer
    Dim period As Integer
    Dim valueA As String
    Dim valueB As String
    strInput = "112344_123456.htm"
    underscore = InStr(strInput, "_")
    period = InStrRev(strInput, ".")
    If underscore > 0 And period > 0 And period > underscore Then
        valueA = Left(strInput, underscore - 1)
        valueB = Mid(strInput, underscore + 1, (period - underscore) - 1)
        MsgBox "valueA = " & valueA & vbCrLf & "valueB = " & valueB
        MsgBox "Invalid Input"
    End If
End Sub
Another way:

Private Sub Command1_Click()
Dim strExample As String, NewStr As String
strExample = "112344_123456.htm"

NewStr = split(split(strExample,"_")(1),".")(0)
MsgBox NewStr

End Sub

Hello Brother!!

Private Function GetWanted(ByVal StrSrc as String) as String
     GetWanted=Mid$(strSrc,InStr(strSrc, "_") + 1,InStr(strSrc, ".") - InStr(strSrc, "_") + 1)
End function

Copy this GetWanted in your code as it is and make a call for the same as :
strResult = GetWanted(112344_123456.htm)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.