この小さなマクロを使って最初の文字を大文字特定の範囲内で:
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
カンマ(,
)の後の文字を次のように変換する必要があります。大文字どうすればこれができるのでしょうか?
答え1
交換する:
cell.Value = UCase(Left(cell.Value, 1)) & Right(cell.Value, Len(cell.Value) - 1)
と:
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
それは次のように変換されます大文字文字列の最初のカンマの後の文字。
あなたが持っている場合複数のカンマセルのマクロを次のものに置き換えます。
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
答え2
これを使って:
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
例:
Debug.Print convertTitleToUppercase("LANTERNE, FHIA, ANTIK MESSING")
戻り値:
Lanterne, Fhia, Antik Messing