Joel, the Trim() only removes leading/trailing whitespace, but wouldn't get
rid of any in the middle such as would be in an entry like "abc def ghi ".
BB, Joel has shown one way to restrict the area you test for, here are a
couple of others with the use of Split() to remove all spaces within the
entry; those on either end and any in the middle of it:
First, if you need to just examine 1 cell (change $A$1 as needed):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim tmpResult As Variant
Dim finalResult As String
Dim sCount As Long
If Target.Address <> "$A$1" Then
Exit Sub ' not a change in A1
End If
'just fix it
If InStr(Target, " ") Then
tmpResult = Split(Target, " ")
For sCount = LBound(tmpResult) To UBound(tmpResult)
finalResult = finalResult & tmpResult(sCount)
Next
Target.Value = finalResult
End If
End Sub
Or if you need a whole column tested (A in this example, adjust as needed):
Private Sub Worksheet_Change(ByVal Target As Range)
Dim tmpResult As Variant
Dim finalResult As String
Dim sCount As Long
'for any cell in column A
If Application.Intersect(Range("A:A"), Target) Is Nothing Then
Exit Sub ' not a change in A1
End If
'just fix it
If InStr(Target, " ") Then
tmpResult = Split(Target, " ")
For sCount = LBound(tmpResult) To UBound(tmpResult)
finalResult = finalResult & tmpResult(sCount)
Next
Target.Value = finalResult
End If
End Sub
To explain Split() a little, it returns an array of the various pieces it
found and you have to put that back together. By declaring tmpResult as a
Variant, it allows the code to set the dimension it needs for the array at
run time.