DELETE the last character in string if is a blank space

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

DELETE the last character in string if is a blank space

Post by sal21 »

I have this string:

AAAAAAA

in need to check if the last caharcter in string is a blank end delete it if exists...

note:
i dont remember if i just oost this question but not found in my old post :grin:

User avatar
HansV
Administrator
Posts: 78530
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: DELETE the last character in string if is a blank space

Post by HansV »

If you want to delete ALL leading and trailing spaces, you can use Trim:

Code: Select all

Dim MyVar As String
MyVar = "  AAA  "
MyVar = Trim(MyVar)
MyVar will then be "AAA"

Otherwise:

Code: Select all

If Right(MyVar, 1) = " " Then
    MyVar = Left(MyVar, Len(MyVar) - 1)
End If
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: DELETE the last character in string if is a blank space

Post by sal21 »

HansV wrote:If you want to delete ALL leading and trailing spaces, you can use Trim:

Code: Select all

Dim MyVar As String
MyVar = "  AAA  "
MyVar = Trim(MyVar)
MyVar will then be "AAA"

Otherwise:

Code: Select all

If Right(MyVar, 1) = " " Then
    MyVar = Left(MyVar, Len(MyVar) - 1)
End If
great! :thankyou:

User avatar
Jay Freedman
Microsoft MVP
Posts: 1318
Joined: 24 May 2013, 15:33
Location: Warminster, PA

Re: DELETE the last character in string if is a blank space

Post by Jay Freedman »

Also look up the functions LTrim (removes all leading spaces) and RTrim (removes all trailing spaces).
http://office.microsoft.com/en-us/excel ... 57211.aspx" onclick="window.open(this.href);return false;