excel - 为什么我的 Visual Basic excel 代码在我的单元格中返回 #value
问题描述
为什么我的 Visual Basic excel 代码在我的单元格中返回 #value?我正在寻找从单元格 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 的屏幕截图。
截屏:
此外,这是它如何返回值的屏幕截图
返回值截图:
解决方案
我在这里收到“无效的模式字符串”错误:
If Mid(s, i, 1) Like CharList Then
问题在于CharList
常数:
Const CharList As String = "[A-Za-Z0-9._-]"
替换A-Za-Z
为A-Za-z
并且功能开始工作=)
Const CharList As String = "[A-Za-z0-9._-]"
推荐阅读
- sql-server - SQl Server 2017 - 可用性组和特殊表“复制”到仓库?
- node.js - Node js (multer) req.file 和 req.files 在 react js 中总是未定义,但在模板引擎中工作正常
- swift - 如何防止在 api 更改时解码/编码丢失密钥?
- php - 如何解决 PHP For 循环 INF 内存问题
- python - 基于部分匹配的国家名称连接两个数据框
- python - Python和Matlab对矩阵的功率显示相同矩阵的不同输出
- javascript - 按下后退按钮重置所有变量反应原生
- python - 我应该使用什么类型的服务器?
- apache-flink - 将 Flink 工作负载拆分到多个作业时如何避免冗余 IO 和反序列化
- windows - PSReadLine 历史不加载