Leading Spaces - something odd ?

stuartgb100

Active Member
Joined
May 10, 2015
Messages
322
Office Version
  1. 2021
Platform
  1. Windows
Hi,

I have some code from this site that removes leading spaces from most cells in my data, but not all.

What sort of thing could cause these 'unknown' spaces and how do i deal with them please ?

Here's the code:

Sub RemoveLeadingTrailingSpaces()
' hiker95, 02/26/2016, ME9242394
With ActiveSheet
With .Range("C8:C24")
.Value = Evaluate("IF(ISTEXT(" & .Address & "),TRIM(" & .Address & "),REPT(" & .Address & ",1))")
End With
End With
End Sub

Thanks.
 

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.
Try
Code:
Sub CleanSpaces()
   Range("C8:C24").Replace Chr(160), "", xlPart, , , , False, False
End Sub
 
Upvote 0
If the data has come from export from other systems, or from web downloads, they often have other "special" characters, such as "non-breaking spaces", which are different from regular spaces.

The first thing I usually like to do is identify exactly what it is I am working with. I do this by identifying a problem records, and returning the ASCII value of the character in that space.
For example, let's say I have some weird character in the first space of an entry in cell A1. I can use this formula to get the ASCII value of that character:
Code:
=CODE(LEFT(A1,1))
Then I can use an ASCII table to look up what character that number represents:
http://www.asciitable.com/
 
Upvote 0
Fluff,

Thanks that worked.
I will do a search on chr(160).

Joe4,
Thanks for the background info.
I will do some reading !

Regards
 
Upvote 0
Fluff,

Thanks that worked.
I will do a search on chr(160).
If your range is fixed (like your post suggests it is), then you can handle the non-breaking spaces and the trim operation all in one macro...
Code:
[table="width: 500"]
[tr]
	[td]Sub RemoveLeadingTrailingSpaces()
  Range("C8:C24") = Evaluate(Replace("IF(@="""","""",TRIM(SUBSTITUTE(@,CHAR(160),"" "")))", "@", "C8:C24"))
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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