Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 93
Default Pharsing string from the right to the first space

I would like to extract data from a cell based on everything after the first
space tothe left of the end of the data. ie Cell 1 contains "Mr. Dean L.
Davis", I would like cell 2 to extract everything from the last space to its
right. In this case "Davis".
I have looked but am missing it somewhere.

Thanks
Dean
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Pharsing string from the right to the first space

here's one way, assuming your data is on sheet1 starting in A1. adjust to your
needs

Option Explicit

Sub test()
Dim ws As Worksheet
Dim lastrow As Long
Dim i As Long
Dim rng As Range
Set ws = Worksheets("Sheet1")
lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row

For i = 1 To lastrow
Set rng = ws.Range("A" & i)
ws.Range("B" & i).Value = Right(rng, Len(rng) - InStrRev(ws.Range("A" &
i).Value, " "))
Next
End Sub


--


Gary


"Dean" wrote in message
...
I would like to extract data from a cell based on everything after the first
space tothe left of the end of the data. ie Cell 1 contains "Mr. Dean L.
Davis", I would like cell 2 to extract everything from the last space to its
right. In this case "Davis".
I have looked but am missing it somewhere.

Thanks
Dean



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Pharsing string from the right to the first space

On Sat, 22 Dec 2007 08:18:01 -0800, Dean
wrote:

I would like to extract data from a cell based on everything after the first
space tothe left of the end of the data. ie Cell 1 contains "Mr. Dean L.
Davis", I would like cell 2 to extract everything from the last space to its
right. In this case "Davis".
I have looked but am missing it somewhere.

Thanks
Dean



As a worksheet formula:

=MID(A1,1+FIND(CHAR(1),SUBSTITUTE(A1," ",CHAR(1),
LEN(A1)-LEN(SUBSTITUTE(A1," ","")))),255)

Since you posted in a programming group, here is a UDF:

===========================
Option Explicit
Function LW(str As String) As String
Dim temp
temp = Split(str, " ")
LW = temp(UBound(temp))
End Function
=============================

One difference between the two is, as written, the worksheet function will
return an error message if there is only one word, whereas the UDF will return
that word.

Not sure what you want to do if there is only a single word, but these
behaviors are easily modified, depending on your requirements.
--ron
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Pharsing string from the right to the first space

As a worksheet formula:

=MID(A1,1+FIND(CHAR(1),SUBSTITUTE(A1," ",CHAR(1),
LEN(A1)-LEN(SUBSTITUTE(A1," ","")))),255)

One difference between the two is, as written, the worksheet function will
return an error message if there is only one word, whereas the UDF will
return
that word.

Not sure what you want to do if there is only a single word, but these
behaviors are easily modified, depending on your requirements.


You can make the worksheet formula do the same by adding a blank space in
front of the A1 references...

=MID(" "&A1,1+FIND(CHAR(1),SUBSTITUTE(" "&A1," ",
CHAR(1),LEN(" "&A1)-LEN(SUBSTITUTE(" "&A1," ","")))),255)

I'm pretty sure your last paragraph indicates you know this, but I figured I
would post the message for those reading this thread in the archives.

Rick

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Pharsing string from the right to the first space

On Sat, 22 Dec 2007 12:49:54 -0500, "Rick Rothstein \(MVP - VB\)"
wrote:

As a worksheet formula:

=MID(A1,1+FIND(CHAR(1),SUBSTITUTE(A1," ",CHAR(1),
LEN(A1)-LEN(SUBSTITUTE(A1," ","")))),255)

One difference between the two is, as written, the worksheet function will
return an error message if there is only one word, whereas the UDF will
return
that word.

Not sure what you want to do if there is only a single word, but these
behaviors are easily modified, depending on your requirements.


You can make the worksheet formula do the same by adding a blank space in
front of the A1 references...

=MID(" "&A1,1+FIND(CHAR(1),SUBSTITUTE(" "&A1," ",
CHAR(1),LEN(" "&A1)-LEN(SUBSTITUTE(" "&A1," ","")))),255)

I'm pretty sure your last paragraph indicates you know this, but I figured I
would post the message for those reading this thread in the archives.

Rick


Yeah, I know. Too many options without enough guidance.

By the way, you can have the worksheet formula return a blank by appending a
<space to the reference:

=MID(A1&" ",1+FIND(CHAR(1),SUBSTITUTE(A1&" "," ",CHAR(1),
LEN(A1&" ")-LEN(SUBSTITUTE(A1&" "," ","")))),255)

And, of course, the UDF can be easily modified to do any of those options.
--ron


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Pharsing string from the right to the first space

try this


=MID(SUBSTITUTE(A1," ","^",LEN(A1)-LEN(SUBSTITUTE(A1,"
",""))),FIND("^",SUBSTITUTE(A1," ","^",LEN(A1)-LEN(SUBSTITUTE(A1,"
",""))))+1,256)

Mike


"Dean" wrote:

I would like to extract data from a cell based on everything after the first
space tothe left of the end of the data. ie Cell 1 contains "Mr. Dean L.
Davis", I would like cell 2 to extract everything from the last space to its
right. In this case "Davis".
I have looked but am missing it somewhere.

Thanks
Dean

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
VBA Runtime error 14: Out of string space... help! smurray444 Excel Programming 7 July 24th 07 09:03 PM
Number of space in a string [email protected] Excel Discussion (Misc queries) 5 November 8th 06 11:15 PM
How do i get the last position of a char (space) in a string? EsPoNjOsO Excel Worksheet Functions 1 October 12th 06 11:55 AM
how to remove a space after a string? elaine Excel Programming 3 June 9th 06 03:00 PM
erase all space characters into string Maileen[_2_] Excel Programming 4 January 16th 05 09:10 PM


All times are GMT +1. The time now is 06:40 AM.

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"