Select Text between two points

Posted on 2004-11-17
Last Modified: 2010-05-02
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.


Question by:ADawn
    LVL 28

    Accepted Solution

    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
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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
    LVL 76

    Expert Comment

    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

    Expert Comment


    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)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now