TRIM function not working

Manolocs

Active Member
Joined
Mar 28, 2008
Messages
340
I have no idea why this happens, I already followed threads from this forum and use the following formulas and macros but still can not get ride of the spaces in front of the cells :(
Any help is more than welcome.
:rofl:

=TRIM(A1)
=SUBSTITUTE(A1,CHAR(160),"")
=TRIM(SUBSTITUTE(D1,CHAR(160),CHAR(32)))
Code:
Sub RemoveLeadingSpace()
'Updateby20131129
Dim Rng As Range
Dim WorkRng As Range
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
For Each Rng In WorkRng
    Rng.Value = VBA.LTrim(Rng.Value)
Next
End Sub
 
Last edited by a moderator:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Use:
Code:
Sub RemoveLeadingSpace()
'Updateby20131129
    Dim Rng                   As Range
    Dim WorkRng               As Range
    On Error Resume Next
    xTitleId = "KutoolsforExcel"
    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
    WorkRng.Replace Chr(160), Chr(32), xlPart
    For Each Rng In WorkRng
        Rng.Value = VBA.LTrim(Rng.Value)
    Next
End Sub
 
Upvote 0
I ran that on your sample file and it worked fine for me.
 
Upvote 0
Try selecting just column B, then running it (make sure you're running the version I just posted, not your original) and see what happens.
 
Upvote 0
This worked fine for me:

Code:
Option Explicit
Sub Macro1()

    Dim lngLastRow As Long
    Dim rngMyCell As Range
    
    lngLastRow = Range("A:E").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    Application.ScreenUpdating = False
    
    For Each rngMyCell In Range("A1:E" & lngLastRow)
        If Len(rngMyCell) > 0 And IsNumeric(rngMyCell) = False And rngMyCell.HasFormula = False Then
            rngMyCell = Trim(rngMyCell)
        End If
    Next rngMyCell
    
    Application.ScreenUpdating = True

End Sub

HTH

Robert
 
Upvote 0
Hi Trebor76, does not work as well it remove only the leading spaces of column A, the other remain the same :(
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,109
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