views:

1149

answers:

3
+1  Q: 

vba string tokens

I have around 100 rows of such texts that I want to tokenize:

"<word>    <unknown number of spaces and tabs> <number>"

I am having trouble finding tokenize functions with VBA. What would be the easiest method to token such strings in VBA?

Thanks in advance.

+3  A: 

You could read line by line and use the split function to split the word and number by space. I vaguely remeber VBA has the split function.

I got the following link by searching in google. Not sure which version of office you are using.

http://msdn.microsoft.com/en-us/library/aa155763(office.10).aspx

This link has the split function.

msvcyc
I have managed to split the row into an array of string, let's say it's called hello(). The valid contents are in the first and last entries of hello(). How would I figure out the sizes of the array for me to address the first and last entry of the array, as the array's size is variable.
stanigator
shahkalpesh
+2  A: 

You can use the Split() method or for more complex matches, you can use the "vbscript.regexp" object:

Sub NewRegex()
    Dim reg
    Dim matches, match, tmpStr As String

    Set reg = CreateObject("vbscript.regexp")
    tmpStr = "blah bla ...."

    With reg
        .IgnoreCase = True
        .MultiLine = False
        .Pattern = "your regex pattern goes here"
        .Global = True
    End With

    Set matches = reg.Execute(tmpStr)

    For Each match In matches
        MsgBox match
    Next mt

End Sub

Here's a tutorial on using regex from VBA: Using Regular Expressions (RegExp) in Excel

Mitch Wheat
A: 

VBA split function right from MS's page

http://msdn.microsoft.com/en-us/library/aa155763(office.10).aspx

Cuervo's Laugh