I created this function in VBA
for you:
It will check that each letter in the name is capitalized. If so, it will add a space before the letter in question.
Code:
Function separa_nomes(str As String) As String
Dim i As Integer, temp As String
For i = 1 To Len(str)
If i = 1 Then
temp = Mid(str, i, 1)
ElseIf Mid(str, i, 1) = UCase(Mid(str, i, 1)) Then
temp = temp + " " + Mid(str, i, 1)
Else
temp = temp + Mid(str, i, 1)
End If
Next i
separa_nomes = temp
End Function
To enable it, simply open the VBE
, inserir um novo módulo
and colar esse código
in the open window.
After entering the code, the function =separa_nomes()
can be used in your spreadsheets.
Ex.:
EDIT: How this code works?
The idea is to create a function that counts how many characters each word has
Make a loop going letter by letter
If the letter found is the first letter of the word: save the letter in a variable called temp
and I don’t make room before.
If i = 1 Then temp = Mid(str, i, 1)
If the letter found is uppercase: this means I need to put a space before it when storing it in the variable temp
.
ElseIf Mid(str, i, 1) = UCase(Mid(str, i, 1)) Then temp = temp + " " + Mid(str, i, 1)
Otherwise: I just copy the letter to the variable temp
, without adding any space.
Else temp = temp + Mid(str, i, 1)
Finally, I show the result accumulated in the variable temp
in the cell where the formula was called.
That is, to change the position of the space when you find an uppercase letter (from before to after the uppercase letter), you must change the term:
Of:
ElseIf Mid(str, i, 1) = UCase(Mid(str, i, 1)) Then
temp = temp + " " + Mid(str, i, 1)
To:
ElseIf Mid(str, i, 1) = UCase(Mid(str, i, 1)) Then
temp = temp + Mid(str, i, 1) + " "
Very cool! It worked right!
– J. L. Muller
Glad I could help! :-)
– dot.Py
I tried to dissect the code but could not deduce which adaptations I could make if I wanted to add the space AFTER the capital letter. You could add that function to your answer?
– J. L. Muller
@Tash_riser edited my post and explained the function. See if it improved your understanding...
– dot.Py