PDA

View Full Version : [SOLVED:] Get characters from string.



staicumihai
01-03-2016, 09:14 PM
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.

alansidman
01-03-2016, 09:56 PM
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.

gmayor
01-03-2016, 11:53 PM
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

staicumihai
01-05-2016, 06:10 AM
Thanks.