Help with Formula to find specific characters in a string

Jat999

New Member
Joined
May 7, 2016
Messages
49
Hi all

I have the following formula that I cannot seem to make work.

Excel Formula:
Function ContainsSpecialCharacters(str As String) As Boolean
For I = 1 To Len(str)
 ch = Mid(str, I, 1)
 Select Case ch
  Case "&", ",", ";", ":", "(", ")", "%", "?", "!", "*"
   ContainsSpecialCharacters = False
  Case Else
   ContainsSpecialCharacters = True
  Exit For
 End Select
Next
End Function

The return of the formula is "TRUE" even though I deliberately post an illegal character.

Can someone please correct me as to the problem.

Many thanks in advance
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
You need to exit the function once it finds an illegal character.
That said, judging by the name of your function, shouldn't you be returning True if an illegal character is found?
 
Upvote 0
You need to exit the function once it finds an illegal character.
That said, judging by the name of your function, shouldn't you be returning True if an illegal character is found? See below
Hi Fluff

Thanks for your reply. So I added an "Exit For" statement after the "False" statement, but it still does not recognise the illegal characters. However what is interesting, if I examine a cell that the formula is active and referring to a cell with the illegal characters, I do not see it in the formula evaluation box - The & being the illegal character.
Capture.JPG
 
Upvote 0
Didn't notice the Exit For, you need to get rid of that.
 
Upvote 0
Solution
Hi Fluff

Revised formula reads as follows and works:)
Excel Formula:
Function ContainsSpecialCharacters(str As String) As Boolean
For I = 1 To Len(str)
 ch = Mid(str, I, 1)
 Select Case ch
  Case "&", ",", ";", ":", "(", ")", "%", "?", "!", "*"
   ContainsSpecialCharacters = False
   Exit For
  Case Else
   ContainsSpecialCharacters = True
 End Select
Next
End Function
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,467
Messages
6,124,984
Members
449,201
Latest member
Lunzwe73

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