跳到主要內容

如何在Excel中的大寫字母之前插入空格?

假設您有一個文本字符串列表,這些單詞之間的所有空格均被意外刪除,如下所示: 在數據之間插入空白行,現在您要在每個字母前添加大寫字母以將單詞分隔為 在數據之間插入空白行。 您如何快速在大寫字母前添加空格,而不是在Excel中一一鍵入空格?


使用用戶定義的功能在大寫字母之前插入空格

不幸的是,您沒有直接方法可以在Excel中的大寫字母前添加空格,但是,您可以創建用戶定義函數來解決此任務。

1。 激活包含要添加空格的文本字符串的工作表。

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

3。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊窗口.

VBA代碼:在大寫字母之前插入空格

Function AddSpaces(pValue As String) As String
'Update 20140723
Dim xOut As String
xOut = VBA.Left(pValue, 1)
For i = 2 To VBA.Len(pValue)
   xAsc = VBA.Asc(VBA.Mid(pValue, i, 1))
   If xAsc >= 65 And xAsc <= 90 Then
      xOut = xOut & " " & VBA.Mid(pValue, i, 1)
   Else
      xOut = xOut & VBA.Mid(pValue, i, 1)
   End If
Next
AddSpaces = xOut
End Function

4。 然後保存並關閉此代碼,返回到工作表,然後輸入此公式 = addspaces(A1) 除數據之外,將其插入空白單元格,請參見屏幕截圖:
文檔-在大寫字母1之前添加空格

5。 然後將填充手柄拖到要包含此公式的範圍內,您將在每個大寫字母之前插入空格。
文檔-在大寫字母1之前添加空格

輕鬆刪除單元格中的前導/後綴/多餘空格

Kutools for Excel的 刪除空間 該實用程序使Excel用戶可以輕鬆地快速刪除所選單元格中的所有前導空格,尾隨空格,多餘空格或所有空格。


廣告刪除空間1

使用VBA代碼在大寫字母前插入空格

這是可以幫助您的另一個VBA代碼,請執行以下操作:

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

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊窗口.

VBA代碼:在大寫字母之前插入空格

Sub AddSpacesRange()
'Update 20140723
Dim Rng As Range
Dim WorkRng As Range
Dim xOut As String
Dim xValue As String
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Application.ScreenUpdating = False
For Each Rng In WorkRng
    xValue = Rng.Value
    xOut = VBA.Left(xValue, 1)
    For i = 2 To VBA.Len(xValue)
       xAsc = VBA.Asc(VBA.Mid(xValue, i, 1))
       If xAsc >= 65 And xAsc <= 90 Then
          xOut = xOut & " " & VBA.Mid(xValue, i, 1)
       Else
          xOut = xOut & VBA.Mid(xValue, i, 1)
       End If
    Next
    Rng.Value = xOut
Next
Application.ScreenUpdating = True
End Sub

3。 然後按 F5 鍵執行此代碼,將彈出一個提示框,讓您選擇要使用的數據范圍。
文檔-在大寫字母1之前添加空格

4。 然後點擊 OK 要關閉此提示框,請立即在大寫字母之前插入空格,請參見屏幕截圖:
文檔-在大寫字母1之前添加空格


使用Kutools for Excel在每個大寫字母前插入空格

Kutools for Excel的 添加文本 實用程序可以幫助您繞過VBA宏並在Excel中輕鬆在每個大寫字母之前插入空格。

Excel的Kutools - 包含 300 多個 Excel 基本工具。 享受全功能 30 天免費試用,無需信用卡! 現在下載!

1。 選擇您要在大寫字母之前插入空格的範圍,然後單擊 庫工具 > 文本 > 添加文本。 看截圖:

2。 在打開的“添加文本”對話框中,在 文本 框,選中 僅添加到選項 並選擇 第一個字母為大寫 來自 僅添加到 下拉列表。

3。 點擊 Ok 按鈕,在每個大寫字母前插入空格,如下圖所示:
doc在大寫字母7之前添加空格

Excel的Kutools - 使用 300 多種基本工具增強 Excel 功能。 享受全功能 30 天免費試用,無需信用卡! 立即行動吧!

筆記:如果第一個字母為大寫字母,則此方法還將在單元格的開頭添加空格。 你可以申請 庫工具 > 文本 > 刪除空間 刪除所選單元格中的所有前導空格。


相關文章:

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
Comments (9)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
This function handles two problems that the given solution doesn't cover:
1. non-English text (with diacritical marks)
2. successive capitals that should not have spaces after them

` Private Function AddSpaces(sText As String)As String
' Inserts a space immediately before a capital letter, except when successive characters are each capitalized.
' Last Updated: 2022-07-04
'
Dim sRet As String, i As Integer, sChar As String, bPrevWasUCase As Boolean

sRet = Left(sText, 1)
For i = 2 To Len(sText)
sChar = Mid(sText, i, 1)
If sChar = UCase(sChar) And Not bPrevWasUCase Then
sRet = sRet & " " & sChar
bPrevWasUCase = True
Else
sRet = sRet & sChar
bPrevWasUCase = False
End If
Next i

AddSpaces= sRet
End Function`
This comment was minimized by the moderator on the site
Hello friend,

Thanks for your share. I tried your VBA code, but it doesn't work. Our VBA code can handle the non-english text. I have test the french text and spaces are successfully inserted before the capital letters.

Sincerely,
Mandy
This comment was minimized by the moderator on the site
Hello

First thanks a lot for your code example and the explanations. It works very well for my case.

I just have 1 Problem, some of the Strings contain words like URL, which should not be seperated.
Do you see a way to except for example "URL" from adding spaces?

Would be very nice if you can help. I tried already a while, but i dont know how to solve this..


Best regards
luca
This comment was minimized by the moderator on the site
hi wanna ask you if you found solution for you case, if you found solution please provide me with that
This comment was minimized by the moderator on the site
Hi,
Thanks for your comment and advice. I have sent your suggestion to the Kutools project team, I’m sure they’ll improve it soon.
This comment was minimized by the moderator on the site
Found a solution?
This comment was minimized by the moderator on the site
Hi there, your code works fantastic. Do you know how to adapt code 20140723 to work on multiple sheets? I would like to add spaces between capital letters throughout the entire workbook. Thanks!
This comment was minimized by the moderator on the site
i need a help in excel i want to find Uppercase in a cell. for example: Sagar Paul MBA 16:04 i want MBA to be highlighted
This comment was minimized by the moderator on the site
I hope you can see it and give it a trial. Sub Test() Dim Rg As Range, xCell As Range Dim i As Long Dim xChar As String Set Rg = Application.Selection For Each xCell In Rg For i = 1 To xCell.Characters.Count xChar = xCell.Characters(i, 1).Text If Asc(xChar) > 64 And Asc(xChar) < 91 Then xCell.Characters(i, 1).Font.Color = vbRed End If Next Next End Sub
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations