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
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
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
 
Upvote 0
Thanks but for some reason the trim function doesnt work in this case, However the code I posted earlier does work.

Thanks
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,443
Members
448,898
Latest member
drewmorgan128

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