为什么我的visual basic excel代码在我的单元格中返回#值

为什么我的visual basic excel代码在我的单元格中返回#值,excel,vba,Excel,Vba,为什么我的visual basic excel代码在我的单元格中返回#值?我想从A1单元提取电子邮件地址。下面是我的代码 Function ExtractEmailAddress(s As String) As String Dim AtSignLocation As Long Dim i As Long Dim TempStr As String Const CharList As String = "[A-Za-Z0-9._-]" 'Get loca

为什么我的visual basic excel代码在我的单元格中返回#值?我想从A1单元提取电子邮件地址。下面是我的代码

Function ExtractEmailAddress(s As String) As String
    Dim AtSignLocation As Long
    Dim i As Long
    Dim TempStr As String
    Const CharList As String = "[A-Za-Z0-9._-]"

    'Get location of the @
    AtSignLocation = InStr(s, "@")
    If AtSignLocation = 0 Then
        ExtractEmailAddress = "" 'no email address is there
    Else
        TempStr = ""
        'Get 1st half of the email address
        For i = AtSignLocation - 1 To 1 Step -1
            If Mid(s, i, 1) Like CharList Then
                    TempStr = Mid(s, i, 1) & TempStr
            Else
                Exit For
            End If
        Next i
        If TempStr = "" Then Exit Function
        'get 2nd half of the email address
        TempStr = TempStr & "@"
        For i = AtSignLocation + 1 To Len(s)
            If Mid(s, i, 1) Like CharList Then
                TempStr = TempStr & Mid(s, i, 1)
            Else
                Exit For
            End If
        Next i
    End If
    'remove trailing period if there is any
    If Right(TempStr, 1) = "." Then TempStr = _
        Left(TempStr, Len(TempStr) - 1)
    ExtractEmailAddress = TempStr
End Function
我还包括了excel VBA的屏幕截图

截图:

此外,这里还有一个屏幕截图,显示它是如何返回值的

返回值屏幕截图:

我在这里遇到“无效模式字符串”错误:

问题在于
字符列表
常量:

A-Za-Z
替换为
A-Za-Z
,函数开始工作=)


在VB编辑器中,点击Ctrl+G以打开即时工具窗口,然后键入
?ExtractEmailAddress(“abc@def.xyz“”
-是否获取错误?哪一行?我输入了那一行?ExtractEmailAddress(“abc@def.com)并返回运行时错误“93”:无效模式stringBeat me 2秒。:)+1@dn18不过,最重要的不是输入错误,而是学习使用即时工具窗口和断点(F9)调试UDF=)另一个后续问题。我现在需要把这个公式向下拖950000行。我知道这听起来很疯狂,但拖下去要花很长时间。由于某些原因,Flashfill不起作用。有什么建议吗?
If Mid(s, i, 1) Like CharList Then
Const CharList As String = "[A-Za-Z0-9._-]"
Const CharList As String = "[A-Za-z0-9._-]"