View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default delete all spaces in range

I wouldn't use xlpart for this.

Youmayfindthatyouendupwithwordsstucktogetherinally ournonemptycells.

But if there's a chance that the original poster has multiple spaces in the cell
and has to get rid of all of those then maybe something like this:

Option Explicit
Sub testme()
Dim myRng As Range
Dim iCtr As Long
Dim MaxSpacesToFix As Long

MaxSpacesToFix = 10

With Worksheets("Sheet1")
Set myRng = .Range("A1").EntireColumn
End With

For iCtr = 1 To MaxSpacesToFix
myRng.Replace what:=Space(iCtr), _
replacement:="", _
lookat:=xlWhole, _
searchorder:=xlNext, _
MatchCase:=False
Next iCtr

End Sub

Adjust the maxspacestofix to what you know(?) it can't exceed.

Jef Gorbach wrote:

Unsure if its faster but consider:

Sub test()
Cells.Replace What:=" ", Replacement:="", LookAt:=xlPart, SearchOrder
_
:=xlByRows, MatchCase:=False, SearchFormat:=False,
ReplaceFormat:=False
End Sub

Obviously you could speed it even further by changing Cells. to a
specific range.


--

Dave Peterson