remove blank space and set number of characters in cell

jek40

Active Member
Joined
Jan 17, 2005
Messages
317
When I download a file there is a space at the beginning of the text.
I am using the code below to remove the blank space.
The length of the text will always be 5 characters.

How can I protectect against the macro being run a second time and removing an additional character?

Sub RemoveSpaceAtFront()
Dim cell As Range
For Each cell In Range("a5:a36")
If Not IsEmpty(cell) Then cell.Value = Right(cell, Len(cell) - 1)
Next cell
End Sub

Thanks,

John
 

Some videos you may like

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

btadams

Well-known Member
Joined
Jan 6, 2003
Messages
1,941
Use the Trim function, which removes leading or trailing spaces only. If the macro is run again it will not change anything.

Sub RemoveSpaceAtFront()
Dim cell As Range
For Each cell In Range("a5:a36")
If Not IsEmpty(cell) Then cell.Value = Trim(cell)
Next cell
End Sub
 

jek40

Active Member
Joined
Jan 17, 2005
Messages
317
Thanks but for some reason the trim function doesnt work in this case, However the code I posted earlier does work.

Thanks
 

btadams

Well-known Member
Joined
Jan 6, 2003
Messages
1,941
Maybe it's not really a space character. You could check it out using the code below and then use the Replace function instead of the Trim function, replace whatever character it is with a null string "".

Code:
Sub GetChar()

    For i = 1 To Len(ActiveCell.Value)
        Char = Mid(ActiveCell.Value, i, 1)
        MsgBox AscW(Char)
    Next i

End Sub

Or since you say the length is always 5 you could check the length of the string in each cell:

Code:
Sub RemoveSpaceAtFront()
Dim cell As Range
    For Each cell In Range("a5:a36")
    If Not IsEmpty(cell) Then
        If Len(cell.Value) <> 5 Then cell.Value = Right(cell, Len(cell) - 1)
    End If
    Next cell
End Sub
 

phive_

New Member
Joined
Mar 3, 2005
Messages
38
Saved me serious time

Just wanted to reply that the code posted by btadams worked like a charm for removing the space (spaces) at the start and end of my text strings.

Thank you! :biggrin:

The snipet I used:
Code:
Sub RemoveSpace()
Dim cell As Range
For Each cell In Range("a5:a36")
    If Not IsEmpty(cell) Then cell.Value = Trim(cell)
Next cell
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,123,271
Messages
5,600,651
Members
414,399
Latest member
Ninjee

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