Clean and Trim data from a column

Deepk

Board Regular
Joined
Mar 21, 2018
Messages
105
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I am trying to clean or trim my data in a column. My objective is to remove all non-printable characters and also the leading and tailing extra spaces or the ALT-enters. The code I have written also removes ALT-enters in the middle of the data :(. Please find my code below:

Sub CleanTrim()


Dim cell As Range

On Error GoTo ErrHandling


Selection.Replace What:=Chr(160), Replacement:=Chr(32), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
On Error Resume Next
For Each cell In Intersect(Selection, _
Selection.SpecialCells(xlConstants, xlTextValues))
cell.Value = Application.Trim(cell.Value)
cell.Value = Application.WorksheetFunction.Clean(cell.Value)
Next cell

ErrHandling:
If Selection.Columns.Count > 1 Or Selection.Rows.Count > 10000 Then
MsgBox Prompt:="Please verify the data you have selected", Title:="Wrong selection!"
Else
MsgBox "Process Completed Successfully."
End If

End Sub

Request you to kindly update this code. Thank you.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
hey,

please help me with this. I want clean function to replace chr(10) or alt-enter at the start and end of the string not in the middle of the string.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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