PDA

View Full Version : Solved: Check for Vowel or Consonant



tyndale2045
10-22-2008, 07:42 AM
Anybody know a quick way to create a function that will check if a letter is a consonant or a vowel? The only way I can think of is to assign the letter (after it is passed to the function) a numerical value (using an array) and then use SELECT CASE to see if that numerical value matches the value of the vowels of the alphabet. Sounds a little long and drawn out to me. Anything simpler than this?

Any ideas will be appreciated.

Jim

Andy Pope
10-22-2008, 07:52 AM
worksheet formula would be

=IF(ISERROR(MATCH(A1,{"A";"E";"I";"O";"U"},0)),"Consonant","Vowel")

function would be


Function IsVowel(Letter) As Boolean

IsVowel = InStr(1, "AEIOU", Letter, vbTextCompare) > 0

End Function

tyndale2045
10-22-2008, 08:00 AM
Thanks Andy.

It works perfect.

(You did in one line of code what took me twenty lines.)

Jim

tyndale2045
10-22-2008, 08:44 AM
Quick Question (for anyone who is willing to answer it)

What is the purpose of the "> 0" at the end of the line of code (as Andy posted it)?

I find the function works the same with or without it.

JKwan
10-22-2008, 09:24 AM
InStr returns the position of the searched text. Therefore > 0 means it is found. It works with or without > 0 because you are only checking for one character, and 1 = true and 0 is false

RonMcK
10-22-2008, 09:44 AM
Quick Question (for anyone who is willing to answer it)

What is the purpose of the "> 0" at the end of the line of code (as Andy posted it)?

I find the function works the same with or without it.
Including the '> 0' changes a math equation into a logical test. You can see this by extending your test.Sub test_ltr()

Debug.Print IsVowel("N")
Debug.Print IsVowel("U")

End Sub

Function IsVowel(Letter) ' As Boolean

IsVowel = InStr(1, "AEIOU", Letter, vbTextCompare) ' > 0

End Function
Begin by commenting out the 'As Boolean in the function line. Open the View Immediate window and walk through the test; you'll find that you get answers of False and True, as expected.

Now, comment out the '> 0 and rerun the test. Now, IsVowel returns answers of 0 and 5, the positions of the test letter in the vowel string not the True or False answers you are expecting.

With just the '> 0' commented out, the As Boolean forces the function to evaluate the answer to True or False. Including the '> 0' shows the casual reader that you intend to create a True/False answer and not a numeric.

HTH

mdmackillop
10-22-2008, 10:18 AM
Not as neat as the above, but the Select Case method would be

Function Test(Data As Range)
Select Case Data
Case "A", "E", "I", "O", "U"
Test = "Vowel"
Case Else
Test = "Consonant"
End Select
End Function

tyndale2045
10-22-2008, 10:19 AM
You guys are great.

Thanks for explaining that.

tyndale2045
10-22-2008, 10:21 AM
MD,

Thanks for the input. I guess even if I did opt for Select Case I should be able to do it in less than 20 lines.

Jim

Demosthine
10-22-2008, 06:07 PM
But what about "Y"? They always taught me...

A, E, I, O, U and sometimes Y.

:rofl: Sorry. Bad joke!
Scott

P.S. But that does make me wonder why a y is only considered a vowel in certain situations. And what are those situations anyways?



If 'y' is preceded by a vowel, it is a consonant. Unless that preceding vowel is an 'e'.

If the 'y' is the beginning of the word, it is a consonant.

If the 'y' is the only letter in the word (i.e. you're speaking Spanish), the program blows up because there are no preceding letters.


:: Sounds like Microsoft ::

mdmackillop
10-23-2008, 12:43 AM
But what about "Y"? They always taught me...

A, E, I, O, U and sometimes Y.

:rofl: Sorry. Bad joke!
Scott

P.S. But that does make me wonder why a y is only considered a vowel in certain situations. And what are those situations anyways?



:: Sounds like Microsoft ::

But you Yanks struggle to use the vowels that we have now. (colour/color, aluminium/aluminum etc.) What do you need another one for? :whistle:

Demosthine
10-23-2008, 06:07 PM
Hey MD.

Yeah, you are absolutely right. The "American" language is falling to pieces. At what point did we start accepting language and names that are intentionally spelled wrong? No wonder our society is collapsing upon itself.

But interestingly enough, I often find I use the British spelling of certain words. After continually being marked down in school for the use of 'colour', I finally broke that one, but I still use 'gray' for instance.

Happy thoughts.
Scott