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
 

Some videos you may like

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,950
Office Version
  1. 365
Platform
  1. Windows
You have an issue with where you have placed your parends. Try:
Code:
TicketNum = Left(sVal, InStr(1, sVal, " ") - 1)
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,950
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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

keith0528

Active Member
Joined
Apr 23, 2009
Messages
250
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
 

keith0528

Active Member
Joined
Apr 23, 2009
Messages
250

ADVERTISEMENT

Many thanks guys. That did the trick.

Keith
 

iliauk

Board Regular
Joined
Jun 3, 2014
Messages
163
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:

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
or maybe this:
Code:
TicketNum = Split(sval)(0)
 

Watch MrExcel Video

Forum statistics

Threads
1,122,970
Messages
5,599,108
Members
414,288
Latest member
horizon2

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
Top