Utilizo esta pequeña macro para convertir la primera letra aMayúsculasen un rango específico:
Sub TitleCase()
Dim rng As Range, cell As Range
Set rng = Range("D4:D999")
For Each cell In rng
If (cell.Value <> "") Then
cell.Value = UCase(Left(cell.Value, 1)) & Right(cell.Value, Len(cell.Value) - 1)
End If
Next cell
End Sub
Ahora necesito convertir cualquier letra después de una coma ( ,
) aMayúsculas. ¿Cómo se puede hacer esto?
Respuesta1
Reemplazar:
cell.Value = UCase(Left(cell.Value, 1)) & Right(cell.Value, Len(cell.Value) - 1)
Con:
comma = InStr(cell.Value, ",")
If comma <> 0 Then
cell.Value = Left(cell.Value, comma) & UCase(Mid(cell.Value, comma + 1, 1)) & Right(cell.Value, Len(cell.Value) - comma - 1)
End If
Se convertirá amayúsculala letra después de la primera coma de la cadena.
Si usted tienemás de una comaen la celda luego reemplace la macro con esta:
Sub TitleCase()
Dim rng As Range, cell As Range
Dim buffer() As String 'Creates an empty array
Set rng = Range("D4:D99")
For Each cell In rng
If (cell.Value <> "") Then
buffer = Split(StrConv(cell.Value, vbUnicode), Chr$(0)) 'split characters into array
ReDim Preserve buffer(UBound(buffer) - 1)
iscomma = False
changedstring = ""
For i = LBound(buffer) To UBound(buffer)
If iscomma = True Then
buffer(i) = UCase(buffer(i))
iscomma = False
End If
If buffer(i) = "," Then
iscomma = True
End If
changedstring = changedstring & buffer(i)
Next i
cell.Value = changedstring
End If
Next cell
End Sub
Respuesta2
Utilizar esta:
Private Function convertTitleToUppercase(s As String)
Dim sSplit() As String: sSplit = Split(s, ",")
Dim tmp As String
Dim v As Variant: For Each v In sSplit
tmp = IIf(tmp = "", tmp, tmp & ",") & StrConv(v, vbProperCase)
Next v
convertTitleToUppercase = tmp
End Function
Ejemplo:
Debug.Print convertTitleToUppercase("LANTERNE, FHIA, ANTIK MESSING")
Devoluciones:
Lanterne, Fhia, Antik Messing