Hopefully a Little Help with my TitleCase Function - Postet den (3494 Views)
Senior Member
bobby131313
Innlegg: 1163
1163

So I scrounged this function up many, many years ago and it works great....
Code:

Function TitleCase(stValue)
iPos = 1
Do While InStr(iPos, stValue, " ", 1) <> 0
iSpace = InStr(iPos, stValue, " ", 1)
sTemp = sTemp & UCase(Mid(stValue, iPos, 1))
sTemp = sTemp & LCase(Mid(stValue, iPos + 1, iSpace - iPos))
iPos = iSpace + 1
Loop

sTemp = sTemp & UCase(Mid(stValue, iPos, 1))
sTemp = sTemp & LCase(Mid(stValue, iPos + 1))

TitleCase = sTemp
End Function

This capitalizes every letter after a space.
I would like it to also capitalize letters after a dash. I played with it for several hours yesterday but obviously functions are not my strong suit.
   
 Sidestørrelse 
Postet den
Forum Admin
HuwR
Innlegg: 20611
20611
You will probably need to do the replacement twice, you could try changing your function so that it also accepts a variable that it is to use as the separator, you can then call the same function twice by passing it a space and then a dash

so, declare the function like Function TitleCase(stValue, stSeparator) and the do while would change to Do While InStr(iPos, stValue, stSeparator, 1) <> 0 and the same for the iSpace = InStr(iPos, stValue, " ", 1) change to iSpace = InStr(iPos, stValue, stSeparator, 1)
Postet den
Senior Member
bobby131313
Innlegg: 1163
1163
Thanks Hu, I will play with it today.
Postet den
Advanced Member
Carefree
Innlegg: 4224
4224
Antique topic, but this would work:

Code:

Function TitleCase(fString)
iPos = 1
Do While ((InStr(iPos, fString, " ", 1) <> 0) Or (InStr(iPos, fString, "-", 1) <> 0))
If InStr(iPos, fString, " ", 1) Then
iSpace = InStr(iPos, fString, " ", 1)
ElseIf InStr(iPos, fString, "-", 1) Then
iSpace = InStr(iPos, fString, "-", 1)
End If
sTemp = sTemp & UCase(Mid(fString, iPos, 1))
sTemp = sTemp & LCase(Mid(fString, iPos + 1, iSpace - iPos))
iPos = iSpace + 1
Loop
sTemp = sTemp & UCase(Mid(fString, iPos, 1))
sTemp = sTemp & LCase(Mid(fString, iPos + 1))
TitleCase = sTemp
End Function
 
Du må legge inn en melding