Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Extracting numbers greater than 5 digits from a string (variable position)

Hi. I've got a macro that imports the filenames of a users
investigation log into column A of a workbook and also hyperlinks
those files. Works well. I have a need to extract the product number
from each filename and put it in column B. Here is an example
filename:

1234001000-234156-357248 Screw (loose)

This breaks down to:
Product ID-investigation#-inquiry# Component (issue)

So, I need to extract that first number. But, it's not just that
simple. Here are the few complications:

1) That larger number may be anywhere in the string.
2) That number may have a letter suffix (e.g., 1234001000E)
3) Most people use dashes, but some use commas or spaces

The other numbers in the string are currently 6 digits, but at some
point it will go to 7 and maybe 8 (but not for a long time for 8).

I've found formulas and even VBA (and UDF) to extract a number, but
it's the variables that are complicating this for me. I'm thinking
maybe a a case structure or something along those lines, but how to
keep this flexible is eluding me. Any ideas? Thanks!

Frank
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Extracting numbers greater than 5 digits from a string (variable position)

Hi Frank,

Am Tue, 21 Jul 2015 13:53:07 +0200 schrieb Claus Busch:

Sub Extract()


or try it this way:

Sub Extract()
Dim rngC As Range
Dim LRow As Long, i As Long
Dim varTmp As Variant, varSign As Variant
Dim myStr As String

LRow = Cells(Rows.Count, 1).End(xlUp).Row
'Here you can add other characters to be replaced
'But keep comma with space at position #0
varSign = Array(", ", ",", "-", " ")

For Each rngC In Range("A1:A" & LRow)
myStr = rngC
For i = LBound(varSign) To UBound(varSign)
myStr = Replace(myStr, varSign(i), "|")
Next

varTmp = Split(myStr, "|")

For i = LBound(varTmp) To UBound(varTmp)
If IsNumeric(Left(varTmp(i), Len(varTmp(i)) - 1)) _
And Len(varTmp(i)) 8 Then
rngC.Offset(, 1) = varTmp(i)
Exit For
End If
Next
Next
End Sub


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Extracting numbers greater than 5 digits from a string (variable position)

That works amazingly well! (and I do have you credited and documented
in my code for all the help you have given me!) It did present an
offshoot puzzle though. The filenames with all the numbers are going
in column A. This code will pull out the product number in column B.
Column C will be the Product Description, and I've got a simple
vlookup formula for that. The vlookup formula looks up the Product
Number in column B and compares to a list on Sheet2. Sheet2 is simply
a list of all the product numbers and the corresponding product
descriptions.

So, it seems it would be a straight forward solution. But there's
something with the formatting of the Product Numbers after being
extracted via the code below that errors out (#NA) the vlookup
formula. I've tried copy/pasting the column as text, as general, as
number, and doing the same to the numbers on Sheet2, to no avail. If
I take the exact same number on Sheet2 and paste it over the number on
Sheet1, then the formula works. The Product Number doesn't APPEAR to
change at all. But it does. Any thoughts on that? If I can't get
the product description, it's not the end of the world - this code
below gets me to where the users most basically need to be. But it
would be helpful to be able to add the description.

Thanks again Clause!

Frank

On Tue, 21 Jul 2015 16:43:20 +0200, Claus Busch
wrote:

Hi Frank,

Am Tue, 21 Jul 2015 13:53:07 +0200 schrieb Claus Busch:

Sub Extract()


or try it this way:

Sub Extract()
Dim rngC As Range
Dim LRow As Long, i As Long
Dim varTmp As Variant, varSign As Variant
Dim myStr As String

LRow = Cells(Rows.Count, 1).End(xlUp).Row
'Here you can add other characters to be replaced
'But keep comma with space at position #0
varSign = Array(", ", ",", "-", " ")

For Each rngC In Range("A1:A" & LRow)
myStr = rngC
For i = LBound(varSign) To UBound(varSign)
myStr = Replace(myStr, varSign(i), "|")
Next

varTmp = Split(myStr, "|")

For i = LBound(varTmp) To UBound(varTmp)
If IsNumeric(Left(varTmp(i), Len(varTmp(i)) - 1)) _
And Len(varTmp(i)) 8 Then
rngC.Offset(, 1) = varTmp(i)
Exit For
End If
Next
Next
End Sub


Regards
Claus B.

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
Extracting Numbers from string Keyrookie Excel Worksheet Functions 4 October 4th 07 11:47 PM
Why does excel round off numbers greater than 16 digits? Dave Excel Discussion (Misc queries) 1 September 1st 07 08:25 PM
How do I find a specific characters position in a string variable [email protected] Excel Programming 3 March 11th 06 02:26 PM
extracting numbers from string Chris Dowell via OfficeKB.com Excel Discussion (Misc queries) 1 January 12th 05 09:37 PM
extracting numbers with no more than 8-digits using advanced filtering Gauthier Excel Worksheet Functions 10 October 29th 04 10:06 PM


All times are GMT +1. The time now is 10:05 PM.

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

About Us

"It's about Microsoft Excel"