Excel删除双倍空间后的所有内容

在Excel中,我试图在一些数据上使用“查找和replace”来删除双倍空间后的所有内容。 这个数据的一个例子是…

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中,我可以search'。*'并将其replace为无效,这可以工作,但使用Excel它不。

我最终想把这个工作变成一个macros,但现在我只是试图让它与Find和Replace一起工作,任何人都可以帮忙吗?

这是做你正在努力完成的另一种方法。 我发现它给你比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 

正如在其他答案中已经指出的那样,在search双重空格之前,应该用规则的空格来replace任何麻烦的非空格( 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 

编辑解决@chris neilsen的评论:

如果你的一些目标单元没有双重空格,那么你应该在使用Left函数之前检查它,否则会引发一个错误:

 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 )的非常遥远的机会下,这些公式将被replace为值。 为了避免这种情况,将条件改为If i > 0 And Not cell.HasFormula Then

这是对克里斯的一个稍微不同的方法。 在search空间之前删除所有的非space space

你的问题是你的string包含非rest空间。 空格为代码32.非中断空格为代码160.由于string不包含space space因此无法findspace 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 

PS

我通过将string粘贴到工作表的单元格A1并调用以下例程来发现非空格:

 Call DsplDiag(Range("A1") 

第一个string的输出是:

  T he A pplesare G reen  T he 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 yaresuppliedby J ohn 79 20 61 72 65 20 73 75 70 70 6C 69 65 64 20 62 79 20 4A 6F 68 6E A0 

注意绿色之后的两个A0,最后。 A0是160的hex。

 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 

search' *' (即<space><space>*

请注意,在提供的示例文本中,两个空间序列的第二个“空间”实际上是a
'没有rest空间'(ascii code 160)。
要将其input到search框中,请inputAlt-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