我有一个很长的文档,如果最后一个数字出现在行尾,则需要替换它。但有时这个数字与字母a或b混在一起。
那么如何获得号码和位置"<>"标签吗?
Text Line 1
Text Line
Text Line 20a
Text Line 300b
Text COVID-19 4000
Text Line <a href="../1.html">1</a>
Text Line
Text Line <a href="../20.html">20</a>a
Text Line <a href="../300.html">300</a>b
Text COVID-19 <a href="../4000.html">4000</a>
这个自定义函数将起作用。
Function findTheEndNumner(theText As String) As String
Dim NewResult As String, finalResult As String, i As Long
NewResult = Mid(theText, InStrRev(theText, " ", -1, vbTextCompare) + 1, 999)
For i = 1 To Len(NewResult)
If IsNumeric(Mid(NewResult, i, 1)) Then
finalResult = finalResult & Mid(NewResult, i, 1)
End If
Next i
findTheEndNumner = finalResult
End Function
你可以用这个宏来测试:
Sub test()
MsgBox findTheEndNumner("Text Line 1")
MsgBox findTheEndNumner("Text Line")
MsgBox findTheEndNumner("Text Line 20a")
MsgBox findTheEndNumner("Text Line 300b")
MsgBox findTheEndNumner("Text COVID-19 4000")
End Sub
示例文件也可用,如果你想…点击这里下载