Conditional format for removing trailing and leading spaces.

Eamonn100

Board Regular
Joined
Nov 12, 2015
Messages
156
Hi,

I'm using this formula to remove any trailing or leading spaces. =TRIM(CLEAN(A1)) It works fine but is there anyway that I can use it as a conditional format so that when I paste the information onto the page it removes any trailing or leading spaces.

At the moment I have to use this formula on a second page to get the spaces removed. I'm trying to get this on to my original page that I paste onto because other formulas are returning error when reading from the second sheet.

Thanks in advance.
 
Last edited:
Hi i came across your VBA code above. I am still a green horn and have not ventured too far into VBA but does your code apply to all cells on the worksheet? If so could you help me apply it to just a single column. Column C.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Application.EnableEvents = False
For Each cell In Target
cell.Value = Trim(cell.Value)
Next cell
Application.EnableEvents = True
End Sub

Thank you.
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
    
    Set rng = Intersect(Target, Columns("C:C"))
    
    If rng Is Nothing Then Exit Sub

    Application.EnableEvents = False
    For Each cell In rng
        cell.Value = Trim(cell.Value)
    Next cell
    Application.EnableEvents = True

End Sub
 
Upvote 0
Thank you but when I ran the code this popped up. Usually the name of the code is displayed so i can click on.
Capture.PNG
 
Upvote 0
You don't run the code, you make a manual change to a cell in column C of the sheet and the code runs automatically.
 
Upvote 0
If you put the code in the propert Worksheet module (the sheet that you want it to run against), it will run automatically when you make a change to your sheet.
If it does not run automatically, then you either put it in the wrong place, or have not enabled Macros/VBA to run.
 
Upvote 0
Thank you MARK858 and Joe4. I hadn't saved it as a macro enabled file. I really appreciate the help. It has saved me a lot of time.
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,112
Members
452,302
Latest member
TaMere

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