Excel在双倍空格后删除所有内容

时间:2012-01-02 18:18:29

标签: excel excel-vba vba

在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一起工作,有人可以帮忙吗?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

<强> P.S。

我通过将字符串粘贴到工作表的单元格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&#39。对于160,A0是十六进制。

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

答案 2 :(得分: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

编辑致电@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)的极端可能性,这些公式将被值替换。要避免这种情况,请将条件更改为If i > 0 And Not cell.HasFormula Then