从不同 lengths/formats 的单元格中提取数字
Extract numbers from cells of varying lengths/formats
我正在寻找从 A 列中提取数字的公式。我 运行 遇到的问题是不同的长度和格式。我正在根据下面的期望结果栏寻求解决方案。
B 列中的公式:
=IFERROR(INT(LEFT(REPLACE(SUBSTITUTE(A4,"-"," "),1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A4&1/17))-1,""),5)),INT(LEFT(REPLACE(SUBSTITUTE(A4,"-"," "),1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A4&1/17))-1,""),4)))
练习册:
谢谢!
尝试以下用户定义函数:
Public Function LastNumber(s As String) As Variant
Dim L As Long, i As Long, temp As String, arr
Dim CH As String
L = Len(s)
temp = ""
For i = 1 To L
CH = Mid(s, i, 1)
If CH Like "[0-9]" Then
temp = temp & CH
Else
temp = temp & " "
End If
Next i
arr = Split(Application.WorksheetFunction.Trim(temp), " ")
For i = UBound(arr) To LBound(arr) Step -1
If IsNumeric(arr(i)) Then
LastNumber = CLng(arr(i))
Exit Function
End If
Next i
LastNumber = ""
End Function
它将return字符串中的最后一个数字(一组数字)。
用户定义函数 (UDF) 非常易于安装和使用:
- ALT-F11 调出 VBE window
- ALT-I
ALT-M 打开一个新模块
- 粘贴内容并关闭 VBE window
如果您保存工作簿,UDF 将随之保存。
如果您使用的 Excel 版本晚于 2003,则必须保存
文件为 .xlsm 而不是 .xlsx
删除 UDF:
- 如上所述调出 VBE window
- 清除代码
- 关闭 VBE window
要使用来自 Excel 的 UDF:
=我的函数(A1)
要了解有关宏的更多信息,请参阅:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
和
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
有关 UDF 的详细信息,请参阅:
http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx
必须启用宏才能运行!
在标准 public 模块代码 sheet 中尝试这个用户定义的函数 sheet。
function lastNumber(str as string)
dim i as long, tmp as string
tmp = str
for i=len(tmp) to 1 step-1
if asc(right(tmp, 1))<48 or asc(right(tmp, 1))>57 then
tmp = left(tmp, len(tmp)-1)
else
exit for
end if
next i
for i=len(tmp) to 1 step-1
if asc(mid(tmp, i))<48 or asc(mid(tmp, i))>57 then
exit for
end if
next i
lastNumber = mid(tmp, i+1)
end function
您可以使用 Regex,它需要引用 Microsoft VBScript 正则表达式:
Public Function GetLastNum(str As String) As String
Dim numRegEx As RegExp
Set numRegEx = New RegExp
Dim matchColl As MatchCollection
Dim retStr As String
With numRegEx
.Global = True
.Pattern = "[0-9]+"
Set matchColl = .Execute(str)
If matchColl.Count > 0 Then
GetLastNum = matchColl.Item(matchColl.Count - 1)
Exit Function
End If
End With
End Function
我正在寻找从 A 列中提取数字的公式。我 运行 遇到的问题是不同的长度和格式。我正在根据下面的期望结果栏寻求解决方案。
B 列中的公式:
=IFERROR(INT(LEFT(REPLACE(SUBSTITUTE(A4,"-"," "),1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A4&1/17))-1,""),5)),INT(LEFT(REPLACE(SUBSTITUTE(A4,"-"," "),1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A4&1/17))-1,""),4)))
练习册:
谢谢!
尝试以下用户定义函数:
Public Function LastNumber(s As String) As Variant
Dim L As Long, i As Long, temp As String, arr
Dim CH As String
L = Len(s)
temp = ""
For i = 1 To L
CH = Mid(s, i, 1)
If CH Like "[0-9]" Then
temp = temp & CH
Else
temp = temp & " "
End If
Next i
arr = Split(Application.WorksheetFunction.Trim(temp), " ")
For i = UBound(arr) To LBound(arr) Step -1
If IsNumeric(arr(i)) Then
LastNumber = CLng(arr(i))
Exit Function
End If
Next i
LastNumber = ""
End Function
它将return字符串中的最后一个数字(一组数字)。
用户定义函数 (UDF) 非常易于安装和使用:
- ALT-F11 调出 VBE window
- ALT-I ALT-M 打开一个新模块
- 粘贴内容并关闭 VBE window
如果您保存工作簿,UDF 将随之保存。 如果您使用的 Excel 版本晚于 2003,则必须保存 文件为 .xlsm 而不是 .xlsx
删除 UDF:
- 如上所述调出 VBE window
- 清除代码
- 关闭 VBE window
要使用来自 Excel 的 UDF:
=我的函数(A1)
要了解有关宏的更多信息,请参阅:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
和
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
有关 UDF 的详细信息,请参阅:
http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx
必须启用宏才能运行!
在标准 public 模块代码 sheet 中尝试这个用户定义的函数 sheet。
function lastNumber(str as string)
dim i as long, tmp as string
tmp = str
for i=len(tmp) to 1 step-1
if asc(right(tmp, 1))<48 or asc(right(tmp, 1))>57 then
tmp = left(tmp, len(tmp)-1)
else
exit for
end if
next i
for i=len(tmp) to 1 step-1
if asc(mid(tmp, i))<48 or asc(mid(tmp, i))>57 then
exit for
end if
next i
lastNumber = mid(tmp, i+1)
end function
您可以使用 Regex,它需要引用 Microsoft VBScript 正则表达式:
Public Function GetLastNum(str As String) As String
Dim numRegEx As RegExp
Set numRegEx = New RegExp
Dim matchColl As MatchCollection
Dim retStr As String
With numRegEx
.Global = True
.Pattern = "[0-9]+"
Set matchColl = .Execute(str)
If matchColl.Count > 0 Then
GetLastNum = matchColl.Item(matchColl.Count - 1)
Exit Function
End If
End With
End Function