Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / December 2007

Tip: Looking for answers? Try searching our database.

extract numbers from text string

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
thomsonpa - 16 Dec 2007 17:31 GMT
I have a column of cells with information in some of the cells only. the
information is text with numbers (the text can be of varying length), for
example: C3 could contain: 1 avml 12 chml 1 special occasion.
There could be as many as 12 variations in the string of text.
I need to extract all the information into other cells, seperating the
numbers from the text so H3 = 1 I3 = avml, etc.
How do I do this with visual basic?
Rick Rothstein (MVP - VB) - 16 Dec 2007 18:16 GMT
We need a little more information... what do you want to happen when
multiple words occur without an intervening number? For example, how did you
want the words "special occasion" from your example handled... "special" in
one cell, "occasion" in another, or both in one cell? If both in one cell,
will that be the case if multiple words occur somewhere other than at the
end of the text?

Rick

>I have a column of cells with information in some of the cells only. the
> information is text with numbers (the text can be of varying length), for
[quoted text clipped - 3 lines]
> numbers from the text so H3 = 1 I3 = avml, etc.
> How do I do this with visual basic?
Ron Rosenfeld - 16 Dec 2007 21:20 GMT
>I have a column of cells with information in some of the cells only. the
>information is text with numbers (the text can be of varying length), for
[quoted text clipped - 3 lines]
>numbers from the text so H3 = 1 I3 = avml, etc.
>How do I do this with visual basic?

Here's a routine that might work if I understand your pattern correctly.

<alt-F11> opens the VB Editor.  Ensure your project is highlighted in the
Project Explorer window, then Insert/Module and paste the code below into the
window that opens.

Be sure to define Dest and Src appropriately in the VBA Code.

Then <alt-F8> opens the macro dialog box.  Select the macro and <Run>.

The logic is (or should be <g>), that the routine views the data as a sequence
of words.

It looks for sub-sequences which consist of a word that consists only of
digits, followed by a sequence of words none of which consist of only digits.

It then splits them.

That should take care of issues such as multiple word descriptors, as you have
with "special occasion", as well as descriptors that might include a digit.

But this should give you a start, and you can post back with how it works.

=================================================
Option Explicit
Sub ParseData()
Dim Src As Range
Dim Dest As Range
Dim c As Range
Dim i As Long, j As Long
Dim re As Object, mc As Object, m As Object
Const sPat As String = "(\d+)\s+(.*?)(?=(\b\d+\b)|$)"

Set Src = Range("A3:a100") 'or wherever your data is
Set Dest = Range("H3:H100") 'your destination range

Dest.ClearContents
Set re = CreateObject("vbscript.regexp")
   re.Global = True
   re.Pattern = sPat
j = 1
For Each c In Src
   If re.test(c.Text) = True Then
       Set mc = re.Execute(c.Text)
           For i = 0 To mc.Count - 1
               Dest(j, i * 2 + 1).Value = mc(i).submatches(0)
               Dest(j, i * 2 + 2).Value = mc(i).submatches(1)
           Next i
   End If
j = j + 1
Next c
End Sub
=============================================
--ron
thomsonpa - 17 Dec 2007 05:51 GMT
Thanks very much Ron,

works exactly as I wanted. I used the call function to get it to run when I
wanted.

I haven't tried yet, but is it possible to set the destination to another
worksheet?

> >I have a column of cells with information in some of the cells only. the
> >information is text with numbers (the text can be of varying length), for
[quoted text clipped - 58 lines]
> =============================================
> --ron
Ron Rosenfeld - 17 Dec 2007 11:02 GMT
>Thanks very much Ron,
>
>works exactly as I wanted. I used the call function to get it to run when I
>wanted.

You're welcome.  Glad to help.  Thanks for the feedback.

>I haven't tried yet, but is it possible to set the destination to another
>worksheet?

You should be able to specify the worksheet in the Set Dest statement.  e.g:

Set Dest = Worksheets("Sheet2").Range("A1:F100") 'your destination range

So far as writing to the destination range, it is only the cell in the upper
left corner that is critical:

e.g. Set Dest = Worksheets("Sheet2").Range("A1") 'your destination range

would write to the same range as above.

However, before I write to that area, I also like to clear the Destination
Range; so for that, several columns are required as above -- just make sure
it's large enough to encompass all your data.

This is very helpful if you run the routine more than once, to clear out the
old data.  Depending on your source, you may need more than six columns.
--ron

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.