Extract text before 1st space

keith0528

Active Member
Joined
Apr 23, 2009
Messages
250
Greetings,

I'm trying to do some string manipulation. I have a string that contains both alpha and numeric data. I want to extract the first number from the string. The number can be anywhere from 4 to 6 digits long so my strategy is to grab everything before the first space.

Ex. "123456 found in WkSht 9999"

I want to extract "123456"

I have the string stored in a variable sVal.

I think it should look something like:
Code:
TicketNum = Left(sVal).Value, InStr(1, sVal), " ") - 1)

but ofcourse my syntax isn't correct. Need a little assistance.

thanks
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
You have an issue with where you have placed your parends. Try:
Code:
TicketNum = Left(sVal, InStr(1, sVal, " ") - 1)
 
Upvote 0
Val(Left(sVal, InStr(sVal, " ") - 1))
Just be careful if you use this method. It does NOT work the same way as the first two.
If you number begins with a leading zero, this last option will drop it, while the first two will not.
So which one you want to use depends on whether or not you want to drop the leading zero off of your number (if one exists).
 
Upvote 0
Just be careful if you use this method. It does NOT work the same way as the first two.
If you number begins with a leading zero, this last option will drop it, while the first two will not.
So which one you want to use depends on whether or not you want to drop the leading zero off of your number (if one exists).


Thanks Joe4 and VBAGeek. That is exactly what i was looking for. I was close but close don't cut it in code.


regards,
Keith
 
Upvote 0
I think all the above more elegant/short but if you wanted RegEx

Code:
Sub TestRegExp()
Dim WRng As Range
Dim cell As Range
Set WRng = Columns("A")
Set WRng = WRng.SpecialCells(Type:=xlCellTypeConstants, Value:=xlNumbers + xlTextValues)

For Each cell In WRng
    cell.Offset(0, 1).Value = RegExp(cell, "[0-9]+")
Next cell
End Sub


Function RegExp(ByVal WhichString As String, _
                        ByVal Pattern As String, _
                        Optional ByVal IsGlobal As Boolean = True, _
                        Optional ByVal IsCaseSensitive As Boolean = True) As String
Dim objRegExp As Object
Dim allMatches As Object
Set objRegExp = CreateObject("vbscript.regexp")
With objRegExp
    .Global = IsGlobal
    .Pattern = Pattern
    .IgnoreCase = Not IsCaseSensitive
End With
Set allMatches = objRegExp.Execute(WhichString)
RegExp = allMatches.Item(0)
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,211,434
Messages
6,101,836
Members
447,758
Latest member
jojo3strada

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top