Excel VBA函数尝试在Excel公式中使用时出现#值错误

我正在使用下面的代码从文本中删除元音,当我运行它使用VBA它工作正常。 但是,当我在Excel中使用相同的公式= RMV(文本)它给了我一个错误#Value。 以下是代码,请帮助


Function RMV(Text) As String 'RMV = RemoveVowels Dim I As Long Dim J As Long Dim Ws As Worksheet Dim Cell As Range Set Ws = ActiveWorkbook.Sheets("sheet1") I = 2 J = 1 Lastrow = Ws.Cells(Rows.Count, 1).End(xlUp).Row For I = 2 To Lastrow '' For Loop to cover all the cells in the range RMV = "" If Ws.Cells(I, 1) <> "" Then ''If condition to select each cell Text = Ws.Cells(I, 1) '' Assigning the cell Value to variable Text For J = 1 To Len(Text) '' FOR loop to go scan each letter in the Text If Not UCase(Mid(Text, J, 1)) Like "[AEIOU]" Then RMV = RMV & Mid(Text, J, 1) End If Next J Lastrow1 = Ws.Cells(Rows.Count, 2).End(xlUp).Row Cells(I, 2) = RMV End If Next I End Function 

Cells(I, 2) = RMV是你的问题。 在一个函数中,你不能直接写回工作表(否则计算过程会中断)。

您唯一能做的就是将RMV设置为调整后的string。 这是返回到工作表。