Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Please assist. Extract Number from String

How can I use a macro to extract only the first batch of numbers from
the following type of info?

Examples:

01458-MODE
1548-JUNE
1245-NOD
01054-MORNING
00154-JUNE
55145-55145

Result should be:

01458
1548
1245
01054
00154
55145
--------------------

I only need the first batch of numbers before the "-" sign. There will
always be this sign "-" separating the two batch of data.

Thanks!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 114
Default Please assist. Extract Number from String


firstBit = Split(theValue,"-")(0)

Tim

On Dec 16, 8:39*am, Damil4real wrote:
How can I use a macro to extract only the first batch of numbers from
the following type of info?

Examples:

01458-MODE
1548-JUNE
1245-NOD
01054-MORNING
00154-JUNE
55145-55145

Result should be:

01458
1548
1245
01054
00154
55145
--------------------

I only need the first batch of numbers before the "-" sign. There will
always be this sign "-" separating the two batch of data.

Thanks!


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Please assist. Extract Number from String

You don't specify

a) ranges
b) overwrite original or adjacent columns

Assuming say a range of A1:A6 and overwrite

With Range("A1:A6")
.Value = Evaluate("IF(ROW(" & .Address & "),LEFT(" & .Address &
",FIND(""-""," & .Address & ")-1))")
End With

that would however return the numbers as numbers - if you wish to return as
strings use ""'""&LEFT(...)
"Damil4real" wrote:

How can I use a macro to extract only the first batch of numbers from
the following type of info?

Examples:

01458-MODE
1548-JUNE
1245-NOD
01054-MORNING
00154-JUNE
55145-55145

Result should be:

01458
1548
1245
01054
00154
55145
--------------------

I only need the first batch of numbers before the "-" sign. There will
always be this sign "-" separating the two batch of data.

Thanks!
.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Please assist. Extract Number from String


Assume the data is in column A. The code below extracts the digits left of
the hyphen and puts the results in column B, same row. Change colujhs to
suit.

Sub dk()
Dim lr As Long
lr = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To lr
Range("B" & i) = Left(Range("A" & i), InStr(Range("A" & i), "-") - 1)
Left
End Sub



"Damil4real" wrote in message
...
How can I use a macro to extract only the first batch of numbers from
the following type of info?

Examples:

01458-MODE
1548-JUNE
1245-NOD
01054-MORNING
00154-JUNE
55145-55145

Result should be:

01458
1548
1245
01054
00154
55145
--------------------

I only need the first batch of numbers before the "-" sign. There will
always be this sign "-" separating the two batch of data.

Thanks!



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 38
Default Please assist. Extract Number from String

I'd just use the Text to Columns function and set "-" as the Delimiter.
--
Mickey


"DonkeyOte" wrote:

You don't specify

a) ranges
b) overwrite original or adjacent columns

Assuming say a range of A1:A6 and overwrite

With Range("A1:A6")
.Value = Evaluate("IF(ROW(" & .Address & "),LEFT(" & .Address &
",FIND(""-""," & .Address & ")-1))")
End With

that would however return the numbers as numbers - if you wish to return as
strings use ""'""&LEFT(...)
"Damil4real" wrote:

How can I use a macro to extract only the first batch of numbers from
the following type of info?

Examples:

01458-MODE
1548-JUNE
1245-NOD
01054-MORNING
00154-JUNE
55145-55145

Result should be:

01458
1548
1245
01054
00154
55145
--------------------

I only need the first batch of numbers before the "-" sign. There will
always be this sign "-" separating the two batch of data.

Thanks!
.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 489
Default Please assist. Extract Number from String

This ought to work for you. Just adjust the range.

Option Explicit

Sub GetNumbers()

Dim rng As Range

For Each rng In Range("A1:A10")
If Not IsEmpty(rng.Value) Then
rng.Offset(0, 1).Value = Left(rng.Value, InStr(rng.Value, "-") -
1)
End If
Next rng

End Sub

Hope this helps! If so, let me know and click "YES" below.
--
Cheers,
Ryan


"Damil4real" wrote:

How can I use a macro to extract only the first batch of numbers from
the following type of info?

Examples:

01458-MODE
1548-JUNE
1245-NOD
01054-MORNING
00154-JUNE
55145-55145

Result should be:

01458
1548
1245
01054
00154
55145
--------------------

I only need the first batch of numbers before the "-" sign. There will
always be this sign "-" separating the two batch of data.

Thanks!
.

  #7   Report Post  
Posted to microsoft.public.excel.programming
GS GS is offline
external usenet poster
 
Posts: 364
Default Please assist. Extract Number from String

FWIW
Some of these replies are good solutions, but unless you extract the left
part as TEXT and/or populate the target as cells(r?,c?).Text, Excel will
remove all leading zeros if the target cells aren't formatted as TEXT.

You could also do the same thing with a worksheet formula if you didn't want
to use VBA. Simply copy the formula to target cells as suits your need.

Kind regards,
Garry
  #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
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 690
Default Please assist. Extract Number from String

On 12/16/2009 11:39 AM, Damil4real wrote:
How can I use a macro to extract only the first batch of numbers from
the following type of info?

Examples:

01458-MODE
1548-JUNE
1245-NOD
01054-MORNING
00154-JUNE
55145-55145

Result should be:

01458
1548
1245
01054
00154
55145
--------------------

I only need the first batch of numbers before the "-" sign. There will
always be this sign "-" separating the two batch of data.

Thanks!


If you don't need the leading zero's...

Sub Demo()
Debug.Print Val("01054-MORNING")
Debug.Print Val("00154-JUNE")
Debug.Print Val("55145-55145")
End Sub

Returns:
1054
154
55145

Perhaps format with leading zero's if you need to.

= = = = = = =
HTH :)
Dana DeLouis
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default Please assist. Extract Number from String

=left(a1,find("-",a1,1)-1)

assuming:
text to be extracted in A1
your letters start in column 2 (they do)

Good Luck!

Gabor Sebo


"Dana DeLouis" wrote in message
...
On 12/16/2009 11:39 AM, Damil4real wrote:
How can I use a macro to extract only the first batch of numbers from
the following type of info?

Examples:

01458-MODE
1548-JUNE
1245-NOD
01054-MORNING
00154-JUNE
55145-55145

Result should be:

01458
1548
1245
01054
00154
55145
--------------------

I only need the first batch of numbers before the "-" sign. There will
always be this sign "-" separating the two batch of data.

Thanks!


If you don't need the leading zero's...

Sub Demo()
Debug.Print Val("01054-MORNING")
Debug.Print Val("00154-JUNE")
Debug.Print Val("55145-55145")
End Sub

Returns:
1054
154
55145

Perhaps format with leading zero's if you need to.

= = = = = = =
HTH :)
Dana DeLouis


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
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 06:52 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"