尝试在Excel公式中使用时,Excel VBA函数给出#Value Error

时间:2014-06-16 10:17:23

标签: excel function vba

我正在使用以下代码从文本中删除元音,当我使用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 

1 个答案:

答案 0 :(得分:1)

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

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