
如何刪除Excel電子表格中每列的第四個單字?例如,我有“史密斯先生和夫人”。我想保留先生和夫人,但刪除史密斯。有沒有辦法找到並刪除第四個單字?非常感謝您的幫忙...
答案1
這是一種解決方法,但如果您只處理常規文字(而不是公式),它就會起作用。
假設您的包含資料的工作表稱為「Sheet1」。在電子表格上建立新工作表(假設它名為“Sheet2”),然後在 A1 中鍵入:
=IFERROR(LEFT(Sheet1!A1,FIND(" ",Sheet1!A1,FIND(" ",Sheet1!A1,FIND(" ",Sheet1!A1)+1)+1)-1),"")
如果你想刪除僅有的第四字,輸入:
=IFERROR(LEFT(Sheet1!A1,FIND(" ",Sheet1!A1,FIND(" ",Sheet1!A1,FIND(" ",Sheet1!A1)+1)+1))&RIGHT(Sheet1!A1,LEN(Sheet1!A1)-FIND(" ",Sheet1!A1,FIND(" ",Sheet1!A1,FIND(" ",Sheet1!A1,FIND(" ",Sheet1!A1)+1)+1)+1)),IFERROR(LEFT(Sheet1!A1,FIND(" ",Sheet1!A1,FIND(" ",Sheet1!A1,FIND(" ",Sheet1!A1)+1)+1)-1),""))
複製儲存格 A1 並將其貼上到 Sheet2 上的儲存格中,該儲存格與 Sheet1 上包含您要使用的資料的儲存格相符。 (因此,如果 Sheet1 上的單元格 A1 到 D5 中有數據,請將其貼上到 Sheet2 的單元格 A1 到 D5 中。)複製貼上的所有單元格,切換到 Sheet1,然後貼上值在匹配的點上。現在您可以刪除 Sheet2。
答案2
這可能是一個奇怪的想法,但請嘗試將文件製作成空格分隔的文件,然後使用 Excel 打開它。現在刪除第四列。這在紙上聽起來不錯......除此之外,我不知道。
答案3
如果您可以貼上 Visual Basic 巨集,那麼這裡有一個更優雅/更有效的方法:
假設您的包含資料的工作表稱為「Sheet1」。
- 上開發商選項卡,按一下宏。 (如果沒有開發商選項卡,點選大圓微軟Office按鈕在視窗的左上角,點擊Excel 選項,然後按一下受歡迎的,然後按一下在功能區中顯示「開發人員」標籤複選框)。
- 將彈出一個對話框。在裡面巨集名稱:文字框,為你的巨集命名;就像是刪除第四字將工作。
- 點選創造按鈕。 (如果創造按鈕仍然處於停用狀態,請從巨集名稱中刪除空格和標點符號。
將以下內容貼到出現的視窗中「Sub DeleteFourthWord()」和「End Sub」之間的空間:
For Each c In ActiveCell.CurrentRegion.Cells If c.HasFormula = False Then Original_Cell_Text = c.Value Text_To_Parse = Original_Cell_Text Word1 = Left(Text_To_Parse, InStr(1, Text_To_Parse, " ", vbTextCompare)) If Len(Word1) = 0 Then Word1 = Text_To_Parse Text_To_Parse = "" Else Text_To_Parse = Right(Text_To_Parse, Len(Text_To_Parse) - Len(Word1)) End If Word2 = Left(Text_To_Parse, InStr(1, Text_To_Parse, " ", vbTextCompare)) If Len(Word2) = 0 Then Word2 = Text_To_Parse Text_To_Parse = "" Else Text_To_Parse = Right(Text_To_Parse, Len(Text_To_Parse) - Len(Word2)) End If Word3 = Left(Text_To_Parse, InStr(1, Text_To_Parse, " ", vbTextCompare)) If Len(Word3) = 0 Then Word3 = Text_To_Parse Text_To_Parse = "" Else Text_To_Parse = Right(Text_To_Parse, Len(Text_To_Parse) - Len(Word3)) End If Word4 = Left(Text_To_Parse, InStr(1, Text_To_Parse, " ", vbTextCompare)) If Len(Word4) = 0 Then Word4 = Text_To_Parse Text_To_Parse = "" Else Text_To_Parse = Right(Text_To_Parse, Len(Text_To_Parse) - Len(Word4)) End If Remaining_Text = Text_To_Parse If (Len(Word1) > 0 And Len(Word2) > 0 And Len(Word3) > 0 And Len(Word4) > 0) Then If Len(Remaining_Text) > 0 Then c.Value = Word1 + Word2 + Word3 + Remaining_Text Else c.Value = Word1 + Word2 + Word3 End If End If End If Next
關上微軟視覺基礎視窗並單擊宏於開發商標籤。
- 選擇剛剛建立的宏,然後按一下選項...按鈕。
- 在下面快速鍵,選擇用於此功能的快捷鍵(在本範例中我將使用“t”鍵)並在其中鍵入它。
- 推好的按鈕,然後關閉宏對話框。
- 選取要從中刪除第四個單字的儲存格,然後按快速鍵(如 Ctrl+t)。您可以根據需要重複此步驟多次。
筆記:如果您想刪除第三個單字之後的所有內容,請貼上以下內容:
For Each c In ActiveCell.CurrentRegion.Cells
If c.HasFormula = False Then
Original_Cell_Text = c.Value
Text_To_Parse = Original_Cell_Text
Word1 = Left(Text_To_Parse, InStr(1, Text_To_Parse, " ", vbTextCompare))
If Len(Word1) = 0 Then
Word1 = Text_To_Parse
Text_To_Parse = ""
Else
Text_To_Parse = Right(Text_To_Parse, Len(Text_To_Parse) - Len(Word1))
End If
Word2 = Left(Text_To_Parse, InStr(1, Text_To_Parse, " ", vbTextCompare))
If Len(Word2) = 0 Then
Word2 = Text_To_Parse
Text_To_Parse = ""
Else
Text_To_Parse = Right(Text_To_Parse, Len(Text_To_Parse) - Len(Word2))
End If
Word3 = Left(Text_To_Parse, InStr(1, Text_To_Parse, " ", vbTextCompare))
If Len(Word3) = 0 Then
Word3 = Text_To_Parse
Text_To_Parse = ""
Else
Text_To_Parse = Right(Text_To_Parse, Len(Text_To_Parse) - Len(Word3))
End If
Remaining_Text = Text_To_Parse
If (Len(Word1) > 0 And Len(Word2) > 0 And Len(Word3) > 0) Then
c.Value = Word1 + Word2 + Word3
End If
End If
Next
(資料來源註:我修改了有關如何從 Excel 說明文件取得「開發人員」標籤的說明。)