Excel ProperCase来说话

时间:2014-03-04 22:55:08

标签: excel vba excel-vba

是否有一种快速的方式(或任何方式)将正确的案例文本转换为单个单词。我有几千行,并且认为要求SO比手动分割文本更好。谷歌无法提供帮助

例如“”EffectiveFromDate应翻译为“从日期生效”

2 个答案:

答案 0 :(得分:4)

试试这个UDF:

Function ProperCaseToWords(x As String) As String
    Dim i As Byte
    'A=65, Z=90
    For i = 65 To 90
        x = Replace(x, Chr(i), " " & Chr(i))
    Next i
    ProperCaseToWords = Trim(x)
End Function

然后在任何单元格中调用它:=ProperCaseToWords(A1)

enter image description here

或从Sub

调用它
Dim cell As Range
For Each cell In Range("A1:A100")
    cell.Value = ProperCaseToWords(cell.Value)
Next

答案 1 :(得分:1)

试试这段代码:

Sub SplitProper()

Dim rgLoop As Range, lCharLoop As Long, lChar As Long, sTemp As String

'turn off updates to speed up code execution
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
    .DisplayAlerts = False
End With

For Each rgLoop In ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants, 2).Cells

    sTemp = rgLoop.Value
    For lCharLoop = Len(rgLoop.Value) To 2 Step -1

        lChar = Asc(Mid(sTemp, lCharLoop, 1))

        If lChar >= 65 And lChar <= 90 Then
            sTemp = Left(sTemp, lCharLoop - 1) & " " & Mid(sTemp, lCharLoop)
        End If

    Next

    rgLoop.Value = sTemp


Next rgLoop

'turn off updates to speed up code execution
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
    .DisplayAlerts = False
End With


End Sub