Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / May 2008

Tip: Looking for answers? Try searching our database.

Going through a string, a char at a time.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
NateBuckley - 30 May 2008 12:15 GMT
Hello is it possible to go through a String checking for certain characters?

Basically want to somehow convert a String to a character array and go
through it checking for the ' character. Or failing that, is there a function
that disposes of certain characters within a string.

Cheers.

Nate
Flower - 30 May 2008 12:40 GMT
Hi,

It's not very pretty but it works for me;

Sub removechar()

Dim i, l As Integer
Dim mystr1, mystr2 As String

mystr1 = "how to remove ' from a string"
l = Len(mystr1)

For i = 1 To l
If Mid(mystr1, i, 1) <> "'" Then
mystr2 = mystr2 & Mid(mystr1, i, 1)
End If
Next i

End Sub

Ammend to sub or UDf as neccessary.
Hopefully someone else will have a sexier solution!

> Hello is it possible to go through a String checking for certain characters?
>
[quoted text clipped - 5 lines]
>
> Nate
NateBuckley - 30 May 2008 12:51 GMT
Hehe, well It may not be sexy but it gets the job done.

Thank yee kindly good sir/madam. (Circle which applies).

> Hi,
>
[quoted text clipped - 28 lines]
> >
> > Nate
Rick Rothstein (MVP - VB) - 30 May 2008 20:38 GMT
A "sexier" subroutine might be...

Sub RemoveApostrophe()
 Dim mystr1 As String, mystr2 As String
 mystr1 = "how to remove ' from a string"
 mystr2 = Replace(mystr1, "'", "")
End Sub

By the way, note the difference between my Dim statement for mystr1 and
mystr2 and yours. Leaving out the As String declaration for mystr1 make it a
Variant, not a String variable. In VB(A), all variables must be declared as
to their type or else they get declared as a default of Variant (this
differs from other languages you may be familiar with).

Rick

> Hi,
>
[quoted text clipped - 30 lines]
>>
>> Nate
Andy Pope - 30 May 2008 12:49 GMT
Hi,

You can remove characters from a string by using the replace function

Msgbox Replace("checking for the ' character","'","")

Cheers
Andy
Signature


Andy Pope, Microsoft MVP - Excel
http://www.andypope.info

> Hello is it possible to go through a String checking for certain
> characters?
[quoted text clipped - 7 lines]
>
> Nate
Sandy Mann - 30 May 2008 12:53 GMT
This may not be what you are looking for but to remove a letter, (or
character if not a letter), from a cell try:

=SUBSTITUTE(A1,"e","")

To count the number of occurrences of the character use:

=LEN(A1)-LEN(SUBSTITUTE(A1,"e",""))

Signature

HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings

sandymann2@mailinator.com
Replace @mailinator.com with @tiscali.co.uk

> Hello is it possible to go through a String checking for certain
> characters?
[quoted text clipped - 7 lines]
>
> Nate
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.