Excel formula that spots space as last character

LJREdinburgh

New Member
Joined
May 20, 2014
Messages
46
Hi All,

I've got a huge dataset that I need to export out of Excel into a statistical analysis programme. Annoyingly, the stats programme can't deal with the dataset if one of the cells contains a space as a last character (eg, "12345 " instead of "12345".)

Is there a way to highlight cells that contain a space as a last character (eg, through conditional formatting or any formula)?

Thank you for your thoughts,

LJ
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Welcome to the board.

For conditional formatting you could use the formula:
Code:
=RIGHT(A1,1)=" "
Which will evaluate to either TRUE or FALSE and apply the conditional formatting if true.
 
Upvote 0
Hi,
Welcome to the board... are you looking to just highlight it, or do you want to remove it if its a space? seems like removing it is what you want to do if it breaks the stats program.


If you do want to remove them.... this would take them out of the selected cells (if you need a range then change the in selection.cells part )

Code:
Sub NoSpaces()
Dim c As Range
For Each c In Selection.Cells
c = Trim(c)
Next
End Sub
 
Last edited:
Upvote 0
If you do want to remove them.... this would take them out of the selected cells (if you need a range then change the in selection.cells part )
Code:
Sub NoSpaces()
Dim c As Range
For Each c In Selection.Cells
c = Trim(c)
Next
End Sub
Or without using a loop...
Code:
Sub NoSpaces()
  Selection = Evaluate("IF(ROW(),TRIM(" & Selection.Address & "))")
End Sub
 
Upvote 0
Or without using a loop...
Code:
Sub NoSpaces()
  Selection = Evaluate("IF(ROW(),TRIM(" & Selection.Address & "))")
End Sub

and now if off to read about evaluate ... do love to find new stuff (although this seems like one i should of seen before)
 
Upvote 0
Stunning replies!

Shadow12345's Code is working perfectly. I have tried it on a small data set around 150 entries, felt like it took a while, so am wondering how it will hold up with around 15,000 entries. Will let you know!

Rick, can you explain what the "loop" is?

JackDanIce, thanks for the formula, will be of use for other purposes.

Cheers
 
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,607
Members
449,037
Latest member
Arbind kumar

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