VBA excel-从excel调用函数
我编写了一个VBA程序来删除元音。我无法从excel调用该函数。我的名字有误。代码如下VBA excel-从excel调用函数,excel,vba,Excel,Vba,我编写了一个VBA程序来删除元音。我无法从excel调用该函数。我的名字有误。代码如下 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 REMOVEV
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
由Tim Stack插入的代码标记表示#名称错误(与逻辑无关)-很可能在VBA中的工作表级应用程序中添加了函数。它必须添加到单独的模块中(右键单击VBAProject-Insert-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
编辑
更优雅的解决方案
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
它确实可以工作,因为它不会给出
#Name
错误。但是它也没有去掉元音。现在我想起来了。。。也许您最好只为5个元音中的每个元音添加一个替换函数,而不是在字符串中的所有字符之间循环。我的代码现在做的是,一旦找到所说的元音,就替换字符串中的所有元音。运行替换功能五次应该不会给ExcelI带来太大压力,因为我没有发现方括号错误。因此,如果OP使用[]
而不是()
并将代码放在正确的位置,那么OP发布的原始代码确实有效。但是,正如你所说-REPLACE
会更简单。没错,我添加了一个更优雅的解决方案,它不需要函数循环遍历所有字符Bartrup Cook先生和Stack先生。我复制了您的代码,还插入了方括号以指定范围。我还将此代码放置在新的VBA模块中,但仍会出现#名称错误。是否将代码粘贴到与使用公式的工作簿相同的工作簿中?您要向公式中添加什么类型的字符串?