Consulting

Results 1 to 4 of 4

Thread: Get characters from string.

  1. #1

    Get characters from string.

    Hello guys,

    I have a lot of strings in this form:

    I will give you an example:

    1.
    12324/2015

    2.
    12/2013

    3.
    143/2014


    The numbers before ”/” character go from 1 to 65421.
    The characters after ”/” is the year.

    I want to be able to extract in a string what is before the character ”/” (in my example 12324, 12, 143) and in another string the last 2 characters of the year (in my example 15, 13, 14).

    I would be greatly appreciative if anyone had any idea about how to do this with a macro in Word 2010. Thanks a lot.

  2. #2
    VBAX Regular
    Joined
    Apr 2009
    Location
    Steamboat Springs
    Posts
    20
    Location
    Use the built in Excel functionality "Text to Columns" with the separator "/".

    OOPs. Ignore this. I didn't notice that this is a Word problem and not an excel issue.

  3. #3
    This is fairly straightforwards, though you haven't indicated what you want to do with the strings. The following just displays them in a message box as they are found.
    Sub Macro1()
    Dim strNum As String
    Dim strYear As String
    Dim orng As Range
        Set orng = ActiveDocument.Range
        With orng.Find
            Do While .Execute(FindText:="[0-9]{2,}\/[0-9]{4}", MatchWildcards:=True)
                strNum = Split(orng.Text, "/")(0)
                strYear = Right(orng.Text, 2)
                'Do something with the strings e.g.
                MsgBox strNum & vbCr & strYear
                orng.Collapse 0
            Loop
        End With
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •