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

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,890
Office Version
  1. 365
Platform
  1. Windows
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.
 

shadow12345

Well-known Member
Joined
May 10, 2004
Messages
1,238
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:

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
37,645
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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
 

shadow12345

Well-known Member
Joined
May 10, 2004
Messages
1,238
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)
 

LJREdinburgh

New Member
Joined
May 20, 2014
Messages
46
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,194
Messages
5,768,778
Members
425,495
Latest member
Ragamacam

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
Top