LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #8   Report Post  
Posted to microsoft.public.excel.programming
GS GS is offline
external usenet poster
 
Posts: 364
Default Please assist. Extract Number from String

Here's some code samples and a formula example:

Sub ExtractPrefixOnly(sRngAddress As String, lOffsetR As Long, _
lOffsetC As Long, Optional sDelimiter As String = "-")
' Extracts the left side of a delimited string.
' If prefix is numeric then it prepends the result with an apostrophe
' so leading zeros aren't lost.
Dim rng As Range
Dim iPos As Integer
Dim i As Integer
Dim sz As Variant

Set rng = ActiveSheet.Range(sRngAddress)
For i = 1 To rng.Cells.Count
iPos = InStr(1, rng.Cells(i).Text, sDelimiter, vbTextCompare)
If iPos 0 Then
sz = Left(rng.Cells(i).Text, iPos - 1)
If IsNumeric(sz) Then sz = "'" & sz
rng.Cells(i).Offset(lOffsetR, lOffsetC) = sz
End If
Next
End Sub 'ExtractPrefixOnly()

'In sheet formula: (Column Absolute, Row relative)
'Assumes list is in columnA, target cell is Row1 of target column.
'ColumnB is what I used, but it could be used in any column (or columns).
'Revise to suit and copy where desired (ie: any row[s] or any column[s}
other than source column)
'=IF(NOT(ISERROR(FIND("-",$A1)0)),LEFT($A1,FIND("-",$A1)-1),"")

'Use example
Sub GetPrefixFromCells()
' Populates target cells according to their Row,Col offset from source cells.
' The source cells are a contiguous selection in ColumnA, though it can be a
single cell.
Const RowOffset As Long = 0 'Stay in the same row
Const ColOffset As Long = 2 'In this case, ColumnC
ExtractPrefixOnly Selection.address, RowOffset, ColOffset
End Sub 'GetPrefixFromCells()

HTH
Kind regards,
Garry
 
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
EXTRACT NUMBER FROM STRING aRJUN SINGH RAWAT SINGH SRAWAT TATDT Charts and Charting in Excel 1 July 24th 09 08:23 AM
Extract 5 digit number from string [email protected] Excel Programming 23 April 15th 07 01:58 AM
Extract number from text/number string.. nastech Excel Discussion (Misc queries) 5 July 5th 06 11:21 PM
Extract Only Number From A String lehainam[_27_] Excel Programming 3 May 26th 06 11:36 AM
How to extract the Number from a String johnbest New Users to Excel 3 December 19th 05 06:23 PM


All times are GMT +1. The time now is 08:33 AM.

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"