Help with textfunction

silentwolf

Well-known Member
Joined
May 14, 2008
Messages
1,216
Office Version
  1. 2016
HI guys,

I am struggling to get the function to work to find me the position of the 4th last space within a string from the right.

String is like.. "Firstname Secondname Surname Myhome 23 8545 Westfield"
but it also could be like "Firstname Surname Myhome 5 4524 Testinghof"

Now I like to get rid of the adresses within the string..

So basically the result need to be like: "Firstname Secondname Surname"
or " Firstname Surname"

I was thinking of finding the 4th last space within the string. Tried with Find and substitute but as the name or better the empty spaces can vary I was out of luck getting it to work.

Could someone give me a hand with this please?

many thanks

Albert
 
Last edited:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
One option with a UDF
Code:
Function GetFullName(Cl As Range) As String
   Dim Ary As Variant, i As Long, Tmp As String
   
   Ary = Split(Cl, " ")
   For i = 0 To UBound(Ary) - 4
      Tmp = Tmp & " " & Ary(i)
   Next i
   GetFullName = Trim(Tmp)
End Function
Excel 2013/2016
AB
8Firstname Secondname Surname Myhome 23 8545 WestfieldFirstname Secondname Surname
9Firstname Surname Myhome 5 4524 TestinghofFirstname Surname
List
Cell Formulas
RangeFormula
B8=GetFullName(A8)
 
Upvote 0
With text in A1
=LEN(A1)-LEN(SUBSTITUTE(A1," ","") is the number of spaces in the string

=(LEN(A1)-LEN(SUBSTITUTE(A1," ",""))-3 is the space after the name part

=SUBSTITUTE(A1," ",REPT(" ",255),(LEN(A1)-LEN(SUBSTITUTE(A1," ",""))-3) pads the key space to 255 spaces.

=LEFT(SUBSTITUTE(A1," ",REPT(" ",255),(LEN(A1)-LEN(SUBSTITUTE(A1," ",""))-3)) returns the first part of that padded string.
Then TRIM can remove the trailing spaces.

=TRIM(LEFT(SUBSTITUTE(A1," ",REPT(" ",255),(LEN(A1)-LEN(SUBSTITUTE(A1," ",""))-3)),255))
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
Hi Fluff,

as always there is one which is out of the norm.. what if there is a a text like.. "Firstname Secondname Surname Myhome 11 5656 Mytown Newtown"
or "Firstname Surname Myhome 12 5656 Mytown Newtown"

how would I need to change it to suit?

Thanks for your help!
 
Upvote 0
Oh no there is another problem..

An address also can be something like that..."My Company and Anothercompany In Mythome 1 5555 Mytown"
so that means "In Myhome 1 5555 Mytown" is the address .(
 
Last edited:
Upvote 0
Unless there is a uniform way of ascertaining the data you need it will be very difficult (if not impossible).
 
Upvote 0
Hi Fluff,
thanks for your input! Yes I know it is difficult but I try find a different way for the rest of the data..

However I do have one more question you might be able to answer.

How can I check if the array has only one item in it ...?
So if there is only one item in the array then exit sub..

function(s as string) as string
dim ary as variant
dim i as long
dim tmp as string

ary=split(s, "")

so hier how to I check if the array has not " " so it can not be split
and if it does then

ary= s

end function
 
Upvote 0
How about
Code:
Function GetFullName(Cl As Range) As String
   Dim Ary As Variant, i As Long, Tmp As String
   
   Ary = Split(Cl, " ")
   If UBound(Ary) <= 3 Then
      GetFullName = Cl.Value
   Else
      For i = 0 To UBound(Ary) - 4
         Tmp = Tmp & " " & Ary(i)
      Next i
      GetFullName = Trim(Tmp)
   End If
End Function
 
Upvote 0

Forum statistics

Threads
1,214,518
Messages
6,119,988
Members
448,935
Latest member
ijat

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
Back
Top