Extracting first 2 words

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have the following line items:

Dobey Scissors XA
Duey Staples DDG
Dobey Paper FFF

I need to turn those values to:

Dobey Scissors
Duey Staples
Dobey Paper

How do I extract the first 2 words for each line item if the character
lengths are different?

Thanks!!
 
Dear Yvette:

The InStr function is your friend. For the second space:

InStr(InStr("Hello there George", " ") + 1, "Hello there George", " ")

This returns 12. Replace both strings "Hello there George" with the column
name.

Tom Ellison
 
If the field always consist of three words, you can try

Left([FieldName],InStrRev([FieldName]," ")-1)
=================================
Or, create a function in a module

Function MySplit(MyStr As String, MyLocation As Integer)
MySplit = Split(MyStr, " ")(MyLocation)
End Function

And then use it as
MySplit([FieldName], 0) & " " & MySplit([FieldName], 1)
 
Fabulous, thank you!

Tom Ellison said:
Dear Yvette:

The InStr function is your friend. For the second space:

InStr(InStr("Hello there George", " ") + 1, "Hello there George", " ")

This returns 12. Replace both strings "Hello there George" with the column
name.

Tom Ellison
 
Dear Yvette:

Don't get totally extatic yet.

Now, if the column has any values in it that don't have the requisite 2
spaces, you're going to get an error. Also, consider what would happen if
there were two consecutive spaces. I assume a user has been typing in these
names. There may not be as much consisitency as you expect.

Also, it may work well now, but fail in the future when odd looking values
are entered. I recommend you consider my words, and the exeptions I
mention. Shall we do a bit more?

Tom Ellison
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top