Skip to main content

如何在 Excel 中將文字字串轉換為首字母大寫並排除例外情況?

Author: Xiaoyang Last Modified: 2025-05-12

在 Excel 中,您可以使用 Proper 函數輕鬆地將文字字串轉換為首字母大寫,但有時您需要在轉換文字字串為首字母大寫時排除一些特定的單詞,如下方截圖所示。本文中,我將介紹一些快速解決此問題的小技巧。

convert text string to proper case with exceptions

使用公式將文字字串轉換為首字母大寫並排除例外情況

使用 VBA 程式碼將文字字串轉換為首字母大寫並排除例外情況


使用公式將文字字串轉換為首字母大寫並排除例外情況

以下公式可能可以幫助您快速處理這個任務,請按照以下步驟操作:

輸入此公式:

=UPPER(LEFT(A2))&MID(TRIM(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(" "&PROPER(A2)&" "," Of "," of ")," A "," a "),"Is "," is ")," Usa "," USA ")),2,LEN(A2)) 到您想要獲取結果的單元格中,然後拖動填充柄以填滿該公式,文字字串已轉換為首字母大寫,但包含特定例外情況,請參見截圖:

convert text string to proper case with exceptions by using formula

注意:在上述公式中,A2 是您要轉換的單元格, “Of ”、“A”、“Is”、“Usa” 是轉換後保持正常首字母大寫的單詞,“of ”、“a”、“is”、“USA” 是您希望從首字母大寫中排除的單詞。您可以根據需要更改它們或使用 SUBSTITUTE 函數添加其他單詞。



使用 VBA 程式碼將文字字串轉換為首字母大寫並排除例外情況

如果上述公式有些難以理解且無法根據您的需求進行調整,這裡您也可以應用 VBA 程式碼來完成此任務。請按照以下步驟逐一操作。

1. 按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications 視窗。

2. 點擊 插入 > 模組,並將以下程式碼粘貼到模組視窗中。

VBA 程式碼:將文字字串轉換為首字母大寫並排除例外情況:

Sub CellsValueChange()
'Updateby Extendoffice
    Dim xSRg As Range
    Dim xDRg As Range
    Dim xPRg As Range
    Dim xSRgArea As Range
    Dim xRgVal As String
    Dim xAddress As String
    Dim I As Long
    Dim K As Long
    Dim KK As Long
    On Error Resume Next
    xAddress = Application.ActiveWindow.RangeSelection.Address
    Set xSRg = Application.InputBox("Original cells:", "KuTools For Excel", xAddress, , , , , 8)
    If xSRg Is Nothing Then Exit Sub
    Set xDRg = Application.InputBox("Output cells:", "KuTools For Excel", , , , , , 8)
    If xDRg Is Nothing Then Exit Sub
    Set xPRg = Application.InputBox("Cells to exclude:", "KuTools For Excel", , , , , , 8)
    If xPRg Is Nothing Then Exit Sub
    Set xDRg = xDRg(1)
    For I = 1 To xSRg.Areas.Count
        Set xSRgArea = xSRg.Areas.Item(I)
        For K = 1 To xSRgArea.Count
            xRgVal = xSRgArea(K).Value
            If Not IsNumeric(xRgVal) Then
                xRgVal = CorrectCase(xRgVal, xPRg)
                xDRg.Offset(KK).Value = xRgVal
            End If
            KK = KK + 1
        Next
    Next
End Sub
Function CorrectCase(ByVal xRgVal As String, ByVal xPRg As Range) As String
    Dim xArrWords As Variant
    Dim I As Integer
    Dim xPointer As Integer
    Dim xVal As String
    xPointer = 1
    xVal = xRgVal
    xArrWords = WordsOf(xRgVal)
    For I = 0 To UBound(xArrWords)
        xPointer = InStr(xPointer, " " & xVal, " " & xArrWords(I))
        Debug.Print xPointer
        Mid(xVal, xPointer) = CorrectCaseOneWord(CStr(xArrWords(I)), xPRg)
    Next I
    CorrectCase = xVal
End Function
Function WordsOf(xRgVal As String) As Variant
    Dim xDelimiters As Variant
    Dim xArrRtn As Variant
    xDelimiters = Array(",", ".", ";", ":", Chr(34), vbCr, vbLf)
    For Each xEachDelimiter In xDelimiters
        xRgVal = Application.WorksheetFunction.Substitute(xRgVal, xEachDelimiter, " ")
    Next xEachDelimiter
    xArrRtn = Split(Trim(xRgVal), " ")
    WordsOf = xArrRtn
End Function
Function CorrectCaseOneWord(xArrWord As String, xERg As Range) As String
    With xERg
        If IsError(Application.Match(xArrWord, .Cells, 0)) Then
            CorrectCaseOneWord = Application.Proper(xArrWord)
        Else
            CorrectCaseOneWord = Application.VLookup(xArrWord, .Cells, 1, 0)
        End If
    End With
End Function

3. 然後按 F5 鍵運行程式碼,會彈出一個提示框提醒您選擇要轉換的原始單元格,請參見截圖:

vba code to select data range

4. 然後點擊 確定,在彈出的對話框中選擇您要輸出結果的單元格,請參見截圖:

vba code to desitination cell

5. 繼續點擊 確定,在彈出的對話框中選擇您要排除的文字,請參見截圖:

vba code to select excluded text

6. 然後點擊 確定 退出對話框,所有文字字串都已轉換為首字母大寫,但排除了指定的單詞,請參見截圖:

result of converting text string to proper case with exceptions

最佳辦公效率工具

🤖 Kutools AI 助手:基於智能執行方式革新數據分析:智能執行   |  生成代碼  |  創建自訂公式  |  分析數據並生成圖表  |  調用 Kutools 函數
熱門功能查找、標記重複值或識別重複項   |  刪除空行   |  合併列或單元格而不丟失數據   |   四捨五入無需公式 ...
高級 LOOKUP多條件 VLookup    多值 VLookup  |   多表查找   |   模糊查找 ....
高級下拉列表快速創建下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
列管理器添加特定數量的列  |  移動列  |  切換隱藏列的可見狀態  |  比較區域和列 ...
特色功能網格聚焦   |  設計檢視   |   增強編輯欄    工作簿與工作表管理器   |  資源庫(自動文本)   |  日期提取器   |  合併資料   |  加密/解密儲存格    按列表發送電子郵件   |  超級篩選   |   特殊篩選(篩選粗體/斜體/刪除線...) ...
頂級 15 種工具集12 個文本工具添加文本刪除特定字符、...)   |   50+ 圖表 類型甘特圖、...)   |   40+ 實用 公式基於生日計算年齡、...)   |   19 個插入工具插入QR碼根據路徑插入圖片、...)   |   12 個轉換工具金額轉大寫匯率轉換、...)   |   7 個合併與分割工具高級合併行分割儲存格、...)   |   ... 還有更多

使用 Kutools for Excel 提升您的 Excel 技巧,體驗前所未有的高效。 Kutools for Excel 提供超過 300 種高級功能來提高生產力並節省時間。  點擊這裡獲取您最需要的功能...


Office Tab 將標籤式界面帶到 Office,讓您的工作更加輕鬆

  • 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中啟用標籤式編輯和閱讀。
  • 在同一窗口的新標籤中打開和創建多個文檔,而不是在新窗口中。
  • 將您的生產力提高 50%,每天為您減少數百次鼠標點擊!