PC Review


Reply
Thread Tools Rate Thread

Code to Remove Specific Character at End of String

 
 
=?Utf-8?B?SkhL?=
Guest
Posts: n/a
 
      1st Jun 2005
I have a string which is ALWAYS in the format of, for example:

Atlanta. GA; Orlando. FL; Montego Bay. Jamaica; ....and so on.

The string may have only one entry but could have several. A new "entry"
added to the string will always end in a ";"

Using VBA how can I remove the last ";" from the string?
 
Reply With Quote
 
 
 
 
Brendan Reynolds
Guest
Posts: n/a
 
      1st Jun 2005
Public Function StripTrailingSemiColon(ByVal InputString As String) As
String

If Right$(InputString, 1) = ";" Then
StripTrailingSemiColon = Left$(InputString, Len(InputString) - 1)
Else
StripTrailingSemiColon = InputString
End If

End Function

We could make this more generic and re-usable by passing the character to be
removed as a parameter ...

Public Function StripTrailingChar(ByVal InputString As String, _
ByVal CharToRemove As String) As String

If Right$(InputString, 1) = CharToRemove Then
StripTrailingChar = Left$(InputString, Len(InputString) - 1)
Else
StripTrailingChar = InputString
End If

End Function

--
Brendan Reynolds (MVP)

"JHK" <(E-Mail Removed)> wrote in message
news:68FF3C8F-047F-44C8-A127-(E-Mail Removed)...
>I have a string which is ALWAYS in the format of, for example:
>
> Atlanta. GA; Orlando. FL; Montego Bay. Jamaica; ....and so on.
>
> The string may have only one entry but could have several. A new "entry"
> added to the string will always end in a ";"
>
> Using VBA how can I remove the last ";" from the string?



 
Reply With Quote
 
Nikos Yannacopoulos
Guest
Posts: n/a
 
      1st Jun 2005
MyString = Left(MyString, Len(MyString) - 1)

HTH,
Nikos

JHK wrote:
> I have a string which is ALWAYS in the format of, for example:
>
> Atlanta. GA; Orlando. FL; Montego Bay. Jamaica; ....and so on.
>
> The string may have only one entry but could have several. A new "entry"
> added to the string will always end in a ";"
>
> Using VBA how can I remove the last ";" from the string?

 
Reply With Quote
 
=?Utf-8?B?SkhL?=
Guest
Posts: n/a
 
      1st Jun 2005
I tried your code, and it doesn't work for the desired effect.

I debuged the code and it skips past the If statement and goes to the End If.

My code is:

Private Sub clickFinished_Click()

Dim strtripwhere As String
Dim strnosemicolon As String

strtripwhere = Me.AirportItinerary 'works fine to here

If Right$(strtripwhere, 1) = ";" Then
strnosemicolon = Left(strtripwhere, Len(strtripwhere) - 1)
Else
strnosemicolon = strtripwhere
End If

<do other stuff.....afterwards...>

Using debug, the strtripwhere = "Atlanta. GA; Orlando. FL; Atlanta. GA;"
And this is shown, but it passes right by the If statement. I tried the
Left$ statement by itself, as mentioned in another user's reply...same effect.

Any ideas?

"Brendan Reynolds" wrote:

> Public Function StripTrailingSemiColon(ByVal InputString As String) As
> String
>
> If Right$(InputString, 1) = ";" Then
> StripTrailingSemiColon = Left$(InputString, Len(InputString) - 1)
> Else
> StripTrailingSemiColon = InputString
> End If
>
> End Function
>
> We could make this more generic and re-usable by passing the character to be
> removed as a parameter ...
>
> Public Function StripTrailingChar(ByVal InputString As String, _
> ByVal CharToRemove As String) As String
>
> If Right$(InputString, 1) = CharToRemove Then
> StripTrailingChar = Left$(InputString, Len(InputString) - 1)
> Else
> StripTrailingChar = InputString
> End If
>
> End Function
>
> --
> Brendan Reynolds (MVP)
>
> "JHK" <(E-Mail Removed)> wrote in message
> news:68FF3C8F-047F-44C8-A127-(E-Mail Removed)...
> >I have a string which is ALWAYS in the format of, for example:
> >
> > Atlanta. GA; Orlando. FL; Montego Bay. Jamaica; ....and so on.
> >
> > The string may have only one entry but could have several. A new "entry"
> > added to the string will always end in a ";"
> >
> > Using VBA how can I remove the last ";" from the string?

>
>
>

 
Reply With Quote
 
=?Utf-8?B?SkhL?=
Guest
Posts: n/a
 
      1st Jun 2005
I missed typed the strnosemicolon = Left$(strtripwhere, Len(strtripwhere)-1)

(forgot the $ in the reply post...but it's in my code.)

"JHK" wrote:

> I tried your code, and it doesn't work for the desired effect.
>
> I debuged the code and it skips past the If statement and goes to the End If.
>
> My code is:
>
> Private Sub clickFinished_Click()
>
> Dim strtripwhere As String
> Dim strnosemicolon As String
>
> strtripwhere = Me.AirportItinerary 'works fine to here
>
> If Right$(strtripwhere, 1) = ";" Then
> strnosemicolon = Left(strtripwhere, Len(strtripwhere) - 1)
> Else
> strnosemicolon = strtripwhere
> End If
>
> <do other stuff.....afterwards...>
>
> Using debug, the strtripwhere = "Atlanta. GA; Orlando. FL; Atlanta. GA;"
> And this is shown, but it passes right by the If statement. I tried the
> Left$ statement by itself, as mentioned in another user's reply...same effect.
>
> Any ideas?
>
> "Brendan Reynolds" wrote:
>
> > Public Function StripTrailingSemiColon(ByVal InputString As String) As
> > String
> >
> > If Right$(InputString, 1) = ";" Then
> > StripTrailingSemiColon = Left$(InputString, Len(InputString) - 1)
> > Else
> > StripTrailingSemiColon = InputString
> > End If
> >
> > End Function
> >
> > We could make this more generic and re-usable by passing the character to be
> > removed as a parameter ...
> >
> > Public Function StripTrailingChar(ByVal InputString As String, _
> > ByVal CharToRemove As String) As String
> >
> > If Right$(InputString, 1) = CharToRemove Then
> > StripTrailingChar = Left$(InputString, Len(InputString) - 1)
> > Else
> > StripTrailingChar = InputString
> > End If
> >
> > End Function
> >
> > --
> > Brendan Reynolds (MVP)
> >
> > "JHK" <(E-Mail Removed)> wrote in message
> > news:68FF3C8F-047F-44C8-A127-(E-Mail Removed)...
> > >I have a string which is ALWAYS in the format of, for example:
> > >
> > > Atlanta. GA; Orlando. FL; Montego Bay. Jamaica; ....and so on.
> > >
> > > The string may have only one entry but could have several. A new "entry"
> > > added to the string will always end in a ";"
> > >
> > > Using VBA how can I remove the last ";" from the string?

> >
> >
> >

 
Reply With Quote
 
=?Utf-8?B?SkhL?=
Guest
Posts: n/a
 
      2nd Jun 2005
Aaargh!

Disregard. I finally figured out that my string always had a space added to
the end. I RTrim the string and the code works fine.

Thanks.

"JHK" wrote:

> I have a string which is ALWAYS in the format of, for example:
>
> Atlanta. GA; Orlando. FL; Montego Bay. Jamaica; ....and so on.
>
> The string may have only one entry but could have several. A new "entry"
> added to the string will always end in a ";"
>
> Using VBA how can I remove the last ";" from the string?

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
code to remove rows containing a character string kevin Microsoft Excel Misc 4 20th Nov 2008 03:06 PM
Remove Specific Text/Character in Specific Place Gary Dolliver Microsoft Access Queries 7 19th Dec 2007 10:13 PM
How do I test the value of a specific character within a string? =?Utf-8?B?RGF2aWQgQW5kZXJzb24=?= Microsoft Access Form Coding 17 23rd Jun 2007 04:56 PM
Code to Remove Specific Characters in the middle of a String Arthur Zulu via AccessMonster.com Microsoft Access VBA Modules 8 2nd Jun 2005 10:05 AM
last specific character in string Mark Peereboom Microsoft Excel Worksheet Functions 7 23rd Dec 2003 10:28 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:20 AM.