Cleaning strings

fredrerik84

Active Member
Joined
Feb 26, 2017
Messages
383
Hi Excel community.

I need a little more help cleaning some strings.

Basically I have a variable called Ateam which gets its info from a webpage.

Originally the text was in all uppercase, I have made some changes so that every word gets capital letter like this:

Ateam = "Ayr Utd" or Ateam = "Utc"

However I need a little help compiling a code so that is the first word has either 2 or 3 letters its should capitalize all words in first words. So the desired reults get ruturned like this:

Ateam "AYR Utd" / "UTC"

and if first word exceeds 3 words like hull city this code should do nothing ,

Ateam = "Hull City"

Hope some brilliant excel wizard could help a little
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Excel 2016 (Windows) 32 bit
AB
1Ayr UtdAYR Utd
2UtcUTC
3Hull CityHull City
Sheet1
Cell Formulas
RangeFormula
B1=CleanString(A1)


Code:
Public Function CleanString(inputString As String) As String

Dim spacePos As Long

CleanString = inputString
If Len(Split(inputString, " ")(0)) > 3 Then Exit Function
spacePos = InStr(1, CleanString, " ")
If spacePos > 0 Then
    CleanString = UCase$(Left$(CleanString, spacePos - 1)) & Mid$(CleanString, spacePos)
Else
    CleanString = UCase$(CleanString)
End If

End Function

WBD
 
Upvote 0
Another function:
Code:
Function foo(ByVal s As String) As String
    Dim sFirstWord As String

    s = WorksheetFunction.Proper(s)
    sFirstWord = Left(s, InStr(s, " ") - 1)
    
    If Len(sFirstWord) = 2 Or Len(sFirstWord) = 3 Then
       s = Replace(s, sFirstWord, UCase(sFirstWord))
    End If
    foo = s

End Function
 
Last edited:
Upvote 0
Hi thanks to the both of you. for your help. While I was waiting I built this code:
but your solution is probably better :)

Code:
            Result = Split(Ateam, " ")(0)
            Resultcount = Len(Result)
            If Resultcount < 4 Then
               Ateam = UCase(Left(Ateam, 3)) & LCase(Mid(Ateam, 4))
            End If
 
Last edited:
Upvote 0
A bit shorter:

Code:
Public Function CleanString(inputString As String) As String

Dim spacePos As Long

CleanString = inputString
If Len(Split(CleanString, " ")(0)) > 3 Then Exit Function
spacePos = InStr(1, CleanString, " ")
If spacePos = 0 Then spacePos = Len(CleanString) + 1
CleanString = UCase$(Left$(CleanString, spacePos - 1)) & Mid$(CleanString, spacePos)

End Function

WBD
 
Upvote 0
Perhaps you could do the whole conversion from the original upper case to the desired result with this slight variation snippet:

Code:
Dim pos As Long

Ateam = Application.Proper(Ateam)
pos = InStr(1, Ateam & " ", " ")
If pos < 5 Then Ateam = UCase(Left(Ateam, pos - 1)) & Mid(Ateam, pos)
 
Upvote 0
Wouldn't your code produce "AYR utd" not the desired "AYR Utd" as you stated in post #1?

- You are indeed correct sir, I solved it with a function to get uppercase for 2nd word. but my code is very messy. maybe ill try this as you have sugested:
Code:
Dim pos As Long

Ateam = Application.Proper(Ateam)
pos = InStr(1, Ateam & " ", " ")
If pos < 5 Then Ateam = UCase(Left(Ateam, pos - 1)) & Mid(Ateam, pos)
 
Last edited:
Upvote 0
Code:
public Function CleanString(inputString As String) As String

Dim spacePos As Long

CleanString = inputString
If Len(Split(CleanString, " ")(0)) > 3 Then Exit Function
spacePos = InStr(1, CleanString, " ")
If spacePos = 0 Then spacePos = Len(CleanString) + 1
CleanString = UCase$(Left$(CleanString, spacePos - 1)) & Mid$(CleanString, spacePos)

End Function
This function is also a good option

Thanks to help for your time and effort to giving me new ideas and better options as my own codes are not the best :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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