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
 

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,334
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
35,507
Office Version
2010
Platform
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
 

Forum statistics

Threads
1,082,253
Messages
5,364,052
Members
400,776
Latest member
JimmyLee

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top