[英]Extract numbers from String - Macro/Excel
I am looking for help on the below 2 instances in Macro & also as a formula.我正在寻找有关宏中以下 2 个实例的帮助以及作为公式的帮助。 Have to extract the numbers including the comma's必须提取包括逗号在内的数字
Situation1: Cell A1 has情况 1:单元格 A1 有
C160516XXX 164614722,65 C160516XXX 164614722,65
Thinking if we can extract the number by reading from right and until a letter/character appears思考我们是否可以通过从右侧读取直到出现字母/字符来提取数字
Situation2:情况2:
Cell B1 has单元格 B1 有
1605080506C 91244,22 FDEC20160413000488//4261997/XXXX/XXXXXXX/XXX/US98268242 1605080506C 91244,22 FDEC20160413000488//4261997/XXXX/XXXXXXX/XXX/US98268242
Is it possible to extract the numbers hidden between the first and the second letter/character?是否可以提取隐藏在第一个和第二个字母/字符之间的数字?
You have the right idea.你有正确的想法。 For Situation 1, loop backwards until you encounter a non-numeric character.对于情况 1,向后循环直到遇到非数字字符。 Use IsNumeric(Mid(str, index, 1))
to grab a 1-character string and test whether it is numeric.使用IsNumeric(Mid(str, index, 1))
抓取一个 1 个字符的字符串并测试它是否是数字。 Then, store the number of iterations n
til value was found.然后,存储迭代次数n
直到找到值。 Use Right(str, n)
to grab the substring of the last n characters of the string.使用Right(str, n)
抓取字符串的最后 n 个字符的子字符串。
For Situation 2, loop forwards, storing the indices of the first and second non-numeric characters, x1
and x2
.对于情况 2,向前循环,存储第一个和第二个非数字字符x1
和x2
的索引。 Then, use Mid(str, x1+1, x2-x1-1)
to get the substring between your first and second non-numeric character.然后,使用Mid(str, x1+1, x2-x1-1)
获取第一个和第二个非数字字符之间的子字符串。
Here is an example VBA implementation that will put your answers in C1 and D1:这是一个示例 VBA 实现,它将把你的答案放在 C1 和 D1 中:
Sub getSubstrings()
Dim str1 As String
Dim str2 As String
Dim char As String
Dim n As Integer
Dim x1 As Integer
Dim x2 As Integer
str1 = Cells(1, 1).Value 'A1
str2 = Cells(1, 2).Value 'B1
'Situation 1
For i = Len(str1) To 1 Step -1
char = Mid(str1, i, 1)
If Not IsNumeric(char) And char <> "," Then
n = i
Exit For
End If
Next
Cells(1, 3).Value = Right(str1, Len(str1) - n) 'write answer to C1
'Situation 2
x1 = 0
x2 = 0
For i = 1 To Len(str2)
char = Mid(str2, i, 1)
If Not IsNumeric(char) And char <> "," Then
If x1 = 0 Then
x1 = i 'set first index
Else
x2 = i 'set second
Exit For
End If
End If
Next
Cells(1, 4).Value = Mid(str2, x1 + 1, x2 - x1 - 1)
End Sub
FYI most people on SO will not give you a full code implementation, but I had nothing better to do.仅供参考,SO 上的大多数人不会为您提供完整的代码实现,但我无事可做。 There is also likely a Excel command solution, but they normally don't handle "character classes" (non-numeric but not comma).也可能有 Excel 命令解决方案,但它们通常不处理“字符类”(非数字但不是逗号)。 I suggest using this code as a template to think about future VBA solutions.我建议使用此代码作为模板来考虑未来的 VBA 解决方案。 I show you here how to access cells, loop over data, and write conditional statements.我在这里向您展示如何访问单元格、循环数据和编写条件语句。 Please do some research online as well to become more familiar.请在网上做一些研究,以变得更加熟悉。
For对于
C160516XXX 164614722,65 C160516XXX 164614722,65
you can use Excel formulas, For example MID()
and SEARCH()
can be combined to give you following result.您可以使用 Excel 公式,例如MID()
和SEARCH()
可以组合为您提供以下结果。
164614722,65 164614722,65
put this where you want result =MID(A1,SEARCH("xxx",A1,1)+3,SEARCH("xxx",A1,1)+100)
把这个放在你想要结果的地方=MID(A1,SEARCH("xxx",A1,1)+3,SEARCH("xxx",A1,1)+100)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.