2012-01-02 89 views
2

在Excel中,我試圖在某些數據上使用查找和替換來刪除雙倍空間後的所有內容。這個數據的一個例子是...Excel刪除雙倍空間後的所有內容

The Apples are Green They are supplied by John 
The Bannanas are Yellow They are supplied by Tom 
The Strawberries are Red They are supplied by Jason 

我想要的數據看起來像這樣...

The Apples are Green 
The Bannanas are Yellow 
The Strawberries are Red 

在OpenOffice的我可以搜索「*」,取而代之的是沒有什麼,這工作,但與Excel它沒有。

我最終想把這個工作變成一個宏,但現在我只是試圖讓它與Find和Replace一起工作,任何人都可以幫忙嗎?

回答

2

這是另一種做你正在嘗試完成的方法。我發現它給你比FindReplace更多的控制權。

' Get the contents of the cell 
Dim s As String 
s = Range("A1").Value 
' Now write back only what precedes the double space 
Range("A1").Value = Left(s, InStr(s, " ") - 1) 

以上只對一個單元格進行操作。做同樣在許多細胞中,你可以這樣做:

Dim cell As Range 
For Each cell In Range("A1:A3") 
    cell.Value = Left(cell.Value, InStr(cell.Value, " ") - 1) 
Next cell 

正如已指出,在其他的答案,則應更換任何麻煩不斷裂空間(Chr(160))由常規的空間,尋找雙前空間:

Dim cell As Range 
For Each cell In Range("A1:A3") 
    cell.Value = Left(cell.Value, _ 
     InStr(Replace(cell.Value, Chr(160), " "), " ") - 1) 
Next cell 

編輯尋址@克里斯尼爾森的評論:

如果一些靶細胞是沒有雙重空間,那麼你應該檢查,使用01前功能以免它引發錯誤:

Dim cell As Range 
Dim i As Long 
For Each cell In Range("A1:A5") 
    i = InStr(Replace(cell.Value, Chr(160), " "), " ") 
    If i > 0 Then 
     cell.Value = Left(cell.Value, i - 1) 
    End If 
Next cell 

現在,在非常偏遠的機會,某些靶細胞的含有包含雙空格公式(例如=A1 & "<space><space>" & A2),這些公式將被值替換。爲避免這種情況,請將條件更改爲If i > 0 And Not cell.HasFormula Then

+1

幾個問題與此:1.任何調用值不包含2空格會導致錯誤。 2.任何公式被替換爲值 – 2012-01-02 22:26:33

+0

當然,好的,這些問題很容易修復。但是這個解決方案解決了問題中指定的問題,並且如果OP決定將該陳述應用於內容偏離問題中指定內容的範圍,將只給出「問題」。 – 2012-01-03 08:13:29

3

搜索' *'(即,<space><space>*

注意,在示例文本提供,所述兩個間隔的序列的第二個「空間」實際上是一個
「沒有中斷空間」(ASCII碼160) 。
要進入這在搜索框中,鍵入Alt-0160(數字鍵盤上)

在代碼中做到這一點,處理一個「無間斷空間」作爲一個空間做

Sub DeleteAfterDoubleSpace() 
    Dim ws As Worksheet 

    Set ws = ActiveSheet 

    ' Replace any non-break spaces 
    ws.Cells.Replace What:=Chr(160), Replacement:=" ", LookAt:=xlPart 
    ' Replace double space* 
    ws.Cells.Replace What:=" *", Replacement:="", LookAt:=xlPart 
End Sub 
+0

我已經嘗試過,儘管它成功地找到了每個項目都沒有代替它。有什麼具體的我需要放在替換盒? – fightstarr20 2012-01-02 18:59:59

+0

離開'替換'框爲我空作品 – 2012-01-02 19:12:25

+1

一個奇怪的是:任何單元格包含一個包含雙空格的公式(例如'=「之前的'')會在使用」編輯「>」替換,但是如果您在執行時錄製宏(這會產生與您的代碼類似的代碼)並重播此完全相同的宏,則會忽略公式並且不會引發錯誤。奇。 – 2012-01-03 08:37:35

3

這是與克里斯的方法略有不同。在搜索space space之前,我刪除所有非休息空間。

你的問題是你的字符串包含非空格。空格爲代碼32.非中斷空格爲代碼160.由於您的字符串不包含space space,因此找不到space space

嘗試以下操作:

Sub DeleteAfterDoubleSpace() 

    Dim Pos As Integer 
    Dim Rng As Range 

    With Sheets("xxxxx") 

    ' Replace any non-break spaces 
    .Cells.Replace What:=Chr(160), Replacement:=" ", LookAt:=xlPart 

    ' Find the first cell containing double space, if any 
    Set Rng = .Cells.Find(" ", .Range("A1"), xlValues, xlPart, xlByRows, xlNext) 
    Do While True 
     If Rng Is Nothing Then 
     ' All double spaces removed, exit. 
     Exit Do 
     End If 
     Pos = InStr(Rng.Value, " ") ' Find position of double space within cell 
     ' Extract first Pos-1 characters from cell and set cell to those characters. 
     Rng.Value = Mid(Rng.Characters, 1, Pos - 1) 
     Set Rng = .Cells.FindNext(Rng) ' Find the next double space 
    Loop 

    End With 

End Sub 

附:

我通過粘貼你的字符串到細胞中,該工作表的A1和調用以下程序,以便發現不斷裂空間:

Call DsplDiag(Range("A1") 

的第一串輸出爲:

T h e  A p p l e s  a r e  G r e e n    T h e 
54 68 65 20 41 70 70 6C 65 73 20 61 72 65 20 47 72 65 65 6E 20 A0 54 68 65 

    y  a r e  s u p p l i e d  b y  J o h n   
79 20 61 72 65 20 73 75 70 70 6C 69 65 64 20 62 79 20 4A 6F 68 6E A0 

注意到兩個A0在Green之後並且在最後。 A0是160的十六進制。

Sub DsplDiag(DsplStg As String) 

    ' Output the string DsplStg to the immediate window in both display and 
    ' hexadecimal formats 

    Dim CharChar As String 
    Dim CharInt As Integer 
    Dim CharStg As String 
    Dim CharWidth As Integer 
    Dim HexStg As String 
    Dim Pos As Integer 
    Dim Printable As Boolean 

    CharStg = "" 
    HexStg = "" 

    For Pos = 1 To Len(DsplStg) 
    CharChar = Mid(DsplStg, Pos, 1) 
    CharInt = AscW(CharChar) 
    Printable = True 
    If CharInt > 255 Then 
     CharWidth = 4 
     ' Assume Unicode character is Printable 
    Else 
     CharWidth = 2 
     If CharInt >= 32 And CharInt <> 127 Then 
     Else 
     Printable = False 
     End If 
    End If 
    HexStg = HexStg & " " & Right(String(CharWidth, "0") & _ 
              Hex(CharInt), CharWidth) 
    If Printable Then 
     CharStg = CharStg & Space(CharWidth) & CharChar 
    Else 
     CharStg = CharStg & Space(CharWidth + 1) 
    End If 
    If Pos Mod 25 = 0 Then 
     Debug.Print CharStg 
     Debug.Print HexStg 
     Debug.Print 
     CharStg = "" 
     HexStg = "" 
    End If 
    Next 

    Debug.Print CharStg 
    Debug.Print HexStg 

End Sub