TRIM using VBA

venugosr

New Member
Joined
Jul 9, 2014
Messages
46
Hi Friends,

Good Morning!!

I need your help on the below.

In the Sheet1, Column A, I have the "N" of rows. Consider 24,000 everytime.
In that few has space in the cell.
Example:
"College ", but it should be "College" (without space).
Now i am manually copy all the data's in Sheet2 and using the trim function and again copy in the sheet1.

Someone please help me to automate this.

Note:

I have the data's in the column A only in sheet1.

Thanks.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Code:
Sub CleanCells()

    Dim cll As Range

    For Each cll In Worksheets("Sheet1").Columns(1).SpecialCells(xlCellTypeConstants, 23)
        cll = CleanTrim(cll)
    Next cll

End Sub

uses the following UDF:
Code:
Function CleanTrim(ByVal StrToClean As String, Optional ConvertNonBreakingSpace As Boolean = True) As String
'http://www.excelfox.com/forum/showthread.php/155-Trim-all-Cells-in-a-Worksheet-VBA

  Dim j As Long
  Dim CodesToClean As Variant
  
  CodesToClean = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, _
                       21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 127, 129, 141, 143, 144, 157)
  
  If ConvertNonBreakingSpace Then StrToClean = Replace(StrToClean, Chr(160), " ")
  
  For j = LBound(CodesToClean) To UBound(CodesToClean)
    If InStr(StrToClean, Chr(CodesToClean(j))) Then StrToClean = Replace(StrToClean, Chr(CodesToClean(j)), "")
  Next j
  
  CleanTrim = WorksheetFunction.Trim(StrToClean)

End Function
 
Last edited:
Upvote 0
Try

Code:
Sub TRIMSample()
    [A1:A24000] = [INDEX(TRIM(A1:A240000),)]
End Sub
 
Upvote 0
The code below written by Mark858 works great.

Try

Code:
Sub TRIMSample()
    [A1:A24000] = [INDEX(TRIM(A1:A240000),)]
End Sub

You might also try this simplified version of his code which seems to work also.

Code:
Sub TRIMSample()
    [A1:A24000] = [TRIM(A1:A240000)]
End Sub
 
Upvote 0
Then make the top number past what you expect the maximum number of rows to be, it makes no real difference if some are empty.
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top