Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Retrieve numbers that meet criteria

I have a column with text. I'd like to retrieve all of the 3 and 4
digit numbers from it and put it into a new column/columns. For
example:

The 5 kids ate 348 hot dogs - 348
3248 kids ate 4000burgers - 3248, 4000

Is there a way of doing this using Excel commands or VBA?

Jeff
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default Retrieve numbers that meet criteria

On Sat, 30 Jul 2011 21:37:10 -0700 (PDT), jeffrey wrote:

I have a column with text. I'd like to retrieve all of the 3 and 4
digit numbers from it and put it into a new column/columns. For
example:

The 5 kids ate 348 hot dogs - 348
3248 kids ate 4000burgers - 3248, 4000

Is there a way of doing this using Excel commands or VBA?

Jeff


This can be done simply with a VBA User Defined Function using Regular Expressions:

To enter this User Defined Function (UDF), <alt-F11 opens the Visual Basic Editor.
Ensure your project is highlighted in the Project Explorer window.
Then, from the top menu, select Insert/Module and
paste the code below into the window that opens.

To use this User Defined Function (UDF), with your data in column A, enter a formula like

=GetNums($A1,COLUMNS($A:A))

in some cell, and fill right for as many cells as there might be number in the sentence.

The "COLUMNS(.." argument will auto-increment to give a value corresponding to the Index in the function, so as to pull out the desired number from the string.

====================================
Option Explicit
Function GetNums(s As String, Optional I As Long = 1) As Variant
Dim re As Object, mc As Object
Set re = CreateObject("vbscript.regexp")
With re
.Global = True
.MultiLine = True
.Pattern = "(?:^|\D)(\d{3,4})(?=\D|$)"
End With
Set mc = re.Execute(s)
If mc.Count = I Then
GetNums = mc(I - 1)
Else
GetNums = ""
End If
End Function
==================================
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to meet criteria Neall Excel Programming 1 May 30th 09 02:08 PM
EXCEL - Meet 2 criteria, then find next case of third criteria Elaine Excel Worksheet Functions 3 December 1st 08 10:43 PM
add numbers if they meet criteria...? Dan B Excel Worksheet Functions 2 January 31st 07 11:47 PM
how do I count the numbers of row that meet 2 criteria Debi Excel Worksheet Functions 4 November 10th 05 09:56 PM
Formula that only adds numbers that meet specific criteria Elizabeth Excel Discussion (Misc queries) 10 October 12th 05 11:38 PM


All times are GMT +1. The time now is 11:52 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"