這是與克里斯的方法略有不同。在搜索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
幾個問題與此:1.任何調用值不包含2空格會導致錯誤。 2.任何公式被替換爲值 – 2012-01-02 22:26:33
當然,好的,這些問題很容易修復。但是這個解決方案解決了問題中指定的問題,並且如果OP決定將該陳述應用於內容偏離問題中指定內容的範圍,將只給出「問題」。 – 2012-01-03 08:13:29