# Extract text before 1st space

#### keith0528

##### Active Member
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.
try

TicketNum = Left(sVal, InStr(1, sVal, " ") - 1)

You have an issue with where you have placed your parends. Try:
Code:
``TicketNum = Left(sVal, InStr(1, sVal, " ") - 1)``

Try this:
Code:
``Val(Left(sVal, InStr(sVal, " ") - 1))``

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).

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

Many thanks guys. That did the trick.

Keith

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:
or maybe this:
Code:
``TicketNum = Split(sval)(0)``

Replies
9
Views
631
Replies
6
Views
224
Replies
6
Views
945
Replies
1
Views
1K
Replies
4
Views
924

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

### 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.

### Which adblocker are you using?

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

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