Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 4
Default return second or third word in a cell

If you have several words entered into a cell is there a formula that I can
use in another cell that will return the third word in the orignial cell. The
words are always capitalized and separated with a space.
  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 1,231
Default return second or third word in a cell

birdgirl31 wrote...
If you have several words entered into a cell is there a formula that I can
use in another cell that will return the third word in the orignial cell. The
words are always capitalized and separated with a space.


If you could use add-ins, Laurent Longre's free MOREFUNC.XLL add-in
provides a function named WMID which is the simplest way to do this.

If you can't use add-ins, to find the k_th space-separated word in x
try

=REPLACE(LEFT(TRIM(x),FIND(CHAR(127),SUBSTITUTE(TR IM(x)&" "," ",CHAR
(127),k))-1),
1,IF(k1,FIND(CHAR(127),SUBSTITUTE(TRIM(x)," ",CHAR(127),k-1)),0),"")
  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 8,520
Default return second or third word in a cell

=MID(D17,SEARCH(" ",D17&" ",SEARCH(" ",D17&" ",1)+1)+1,SEARCH(" ",D17 & "
",SEARCH(" ",D17&" ",SEARCH(" ",D17&" ",1)+1)+1)-SEARCH(" ",D17&" ",SEARCH("
",D17&" ",1)+1)-1)

If this post helps click Yes
---------------
Jacob Skaria


"birdgirl31" wrote:

If you have several words entered into a cell is there a formula that I can
use in another cell that will return the third word in the orignial cell. The
words are always capitalized and separated with a space.

  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 8,520
Default return second or third word in a cell

The below forumla will only return the third word. If you are looking for a
user defined function please find the below. To use this launch VBE using
ALT+F11 . Insert Module and paste the below function. Save and get back to
workbook.
A1 = "This is a test"

B1 = SecondorThirdWord(A1)

Function SecondorThirdWord(varTemp)
If InStr(varTemp, " ") = 0 Then Exit Function
arrTemp = Split(varTemp, " ")
If UBound(arrTemp) 1 Then
SecondorThirdWord = arrTemp(2)
Else
SecondorThirdWord = arrTemp(1)
End If
End Function

--
If this post helps click Yes
---------------
Jacob Skaria


"Jacob Skaria" wrote:

=MID(D17,SEARCH(" ",D17&" ",SEARCH(" ",D17&" ",1)+1)+1,SEARCH(" ",D17 & "
",SEARCH(" ",D17&" ",SEARCH(" ",D17&" ",1)+1)+1)-SEARCH(" ",D17&" ",SEARCH("
",D17&" ",1)+1)-1)

If this post helps click Yes
---------------
Jacob Skaria


"birdgirl31" wrote:

If you have several words entered into a cell is there a formula that I can
use in another cell that will return the third word in the orignial cell. The
words are always capitalized and separated with a space.

  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 1,231
Default return second or third word in a cell

Jacob Skaria wrote...
....
Function SecondorThirdWord(varTemp)
If InStr(varTemp, " ") = 0 Then Exit Function
arrTemp = Split(varTemp, " ")
If UBound(arrTemp) 1 Then
SecondorThirdWord = arrTemp(2)
Else
SecondorThirdWord = arrTemp(1)
End If
End Function

....

What a nearly useless udf!

All it takes to make this general is adding a second argument to the
UDF to allow the user to specify which word to return.


Function w(s As String, k As Long) As Variant
Dim v As Variant

k = k - 1 'Split returns 0-based arrays, but this udf uses 1-based
v = Split(Application.WorksheetFunction.Trim(s), " ")

If LBound(v) <= k And k <= UBound(v) _
Then w = v(k) _
Else w = CVErr(xlErrValue)

Erase v
End Function


  #6   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 8,520
Default return second or third word in a cell

Dear Harlan

You are right. I have modified to return the word specified.

Just one thing TRIM() function is a VBScript function which is also a
worksheet function. So you can directly call TRIM..

Function GetWord(varTemp As String, lngPos As Long)
If InStr(Trim(varTemp), " ") = 0 Then Exit Function
lngPos = lngPos - 1
arrTemp = Split(Trim(varTemp), " ")
If lngPos UBound(arrTemp) Or lngPos < 0 Then Exit Function
GetWord = arrTemp(lngPos)
End Function

If this post helps click Yes
---------------
Jacob Skaria


"Harlan Grove" wrote:

Jacob Skaria wrote...
....
Function SecondorThirdWord(varTemp)
If InStr(varTemp, " ") = 0 Then Exit Function
arrTemp = Split(varTemp, " ")
If UBound(arrTemp) 1 Then
SecondorThirdWord = arrTemp(2)
Else
SecondorThirdWord = arrTemp(1)
End If
End Function

....

What a nearly useless udf!

All it takes to make this general is adding a second argument to the
UDF to allow the user to specify which word to return.


Function w(s As String, k As Long) As Variant
Dim v As Variant

k = k - 1 'Split returns 0-based arrays, but this udf uses 1-based
v = Split(Application.WorksheetFunction.Trim(s), " ")

If LBound(v) <= k And k <= UBound(v) _
Then w = v(k) _
Else w = CVErr(xlErrValue)

Erase v
End Function

  #7   Report Post  
Posted to microsoft.public.excel.worksheet.functions
external usenet poster
 
Posts: 5,651
Default return second or third word in a cell

On Sat, 2 May 2009 21:02:01 -0700, Jacob Skaria
wrote:

Just one thing TRIM() function is a VBScript function which is also a
worksheet function. So you can directly call TRIM..


If you examine these functions, you will see that they are NOT the same. In
particular, and most likely why Harlan used the worksheet function and NOT the
VBA function, is the difference in how spaces within the string are treated.
--ron
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
make cell equal a word if a data validation cell has a word in it Robzz Excel Discussion (Misc queries) 1 February 6th 09 06:20 PM
return 1st word in string EngelseBoer Excel Discussion (Misc queries) 6 September 7th 08 11:25 AM
Find a cell with a word and then return the next cell down pgarcia Excel Discussion (Misc queries) 5 August 12th 08 07:48 PM
Return Date using two dates and a word pblenis Excel Discussion (Misc queries) 4 May 22nd 07 06:46 PM
need formula to search column for a word and return another word Skyline Excel Discussion (Misc queries) 5 November 18th 05 10:00 PM


All times are GMT +1. The time now is 09:28 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"