Reputation: 31
I wrote a VBA program to remove vowels. I am not able to call the function from excel. i get a #NAME error. Code below
Function REMOVEVOWELS(Txt) As String
'Removes all vowels from the Txt argument
Dim i As Long
REMOVEVOWELS = ""
For i = 1 To Len(Txt)
If Not UCase(Mid(Txt, i, 1)) Like "(AEIOU)" Then
REMOVEVOWELS = REMOVEVOWELS & Mid(Txt, i, 1)
End If
Next i
End Function
Code tags inserted by Tim Stack
Upvotes: 1
Views: 105
Reputation: 6627
Regardign the #NAME error (not about the logic) - most likely you've added function at Application of Sheet level in VBA. It must be added in separate module instead (Right click on VBAProject - Insert - Module)
Upvotes: 1
Reputation: 3248
Rewritten, tested, and works! Insert this at the top of a Module
Function REMOVEVOWELS(Txt) As String
'Removes all vowels from the Txt argument
Dim i As Long
For i = 1 To Len(Txt)
If Mid(Txt, i, 1) Like "[AEIOU]" Then
Txt = Replace(Txt, Mid(Txt, i, 1), "")
End If
Next i
REMOVEVOWELS = Txt
End Function
EDIT
A more elegant solution.
Function REMOVEVOWELS(Txt) As String
'Removes all vowels from the Txt argument
Vowels = Array("A", "E", "I", "O", "U")
For Each a In Vowels
Txt = Replace(Txt, a, "")
Next a
REMOVEVOWELS = Txt
End Function
Upvotes: 2