Excel UDF用于捕获字符内的数字 [英] Excel UDF for capturing numbers within characters

查看:110
本文介绍了Excel UDF用于捕获字符内的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位于单元格A1中的可变文本字段,其中包含以下内容:


Text; #Number; #Text; #Number





  • 此格式可以继续重复,但模式始终为Text; #Number。

  • 数字可以从1位数到n位数(限制7)



示例:



原始值


MyName;#123; #YourName;# 3456; #HisName;#78


必需值:


123,3456,78


根据我的理解,该字段对于excel公式来说太可变了。



我尝试使用正则表达式,但我是一个初学者,当涉及到编码。如果你可以用一些解释文本来分解代码,那将是非常感激的。



我已经尝试了下面的一些建议,他们的工作完美。还有一个问题。



现在我可以从文本中分割数字,有没有办法使用下面的代码,并添加另一个图层,我们将数字分割成x单元格。例如:一旦我们运行该函数,如果我们在同一个单元格中得到1234,567,那么该函数将在单元格B2中输入1234,在单元格C2中输入567。这将不断更新同一行中的所有单元格,直到字符串耗尽了从函数中检索到的所有数字。



谢谢

解决方案

如果模式是固定的,并且数字的位置永远不会改变,您可以假定数字将位于字符串中的偶数位置。这意味着在源字符串中的数组结果中,可以使用结果数组的奇数索引。例如在这个字符串Text;# Number ;#Text;# Number 中,数组索引1,3将是数字(Text(0);#数(1);#文本(2);#<强>数(3))。如果模式确实是固定的,我认为这种方法更容易和更安全,因为它避免了验证数据类型的需要。

 公共函数GetNums(src As String)As String 
Dim arr
Dim i As Integer
Dim result As String
arr = Split(src,;#)'Split字符串到数组。
result =
对于i = 1到UBound(arr)步骤2'循环通过数组,从第二个项目开始,并跳过一个项目(使用步骤2)。
result = result& arr(i)& ,
Next
如果Len(结果)> 2然后
GetNums = Left(result,Len(result) - 2)'删除结果字符串末尾的额外,。
Else
GetNums =
如果
结束函数


I have a variable text field sitting in cell A1 which contains the following:

Text;#Number;#Text;#Number

  • This format can keep repeating, but the pattern is always Text;#Number.
  • The numbers can vary from 1 digit to n digits (limit 7)

Example:

Original Value

MyName;#123;#YourName;#3456;#HisName;#78

Required value:

123, 3456, 78

The field is too variable for excel formulas from my understanding.

I tried using but I am a beginner when it comes to coding. if you can break down the code with some explanation text, it would be much appreciated.

I have tried some of the suggestions below and they work perfectly. One more question.

Now that I can split the numbers from the text, is there any way to utilize the code below and add another layer, where we split the numbers into x cells.

For example: once we run the function, if we get 1234, 567 in the same cell, the function would put 1234 in cell B2, and 567 in cell C2. This would keep updating all cells in the same row until the string has exhausted all of the numbers that are retrieved from the function.

Thanks

解决方案

If the pattern is fixed, and the location of the numbers never changes, you can assume the numbers will be located in the even places in the string. This means that in the array result of a split on the source string, you can use the odd indexes of the resulting array. For example in this string "Text;#Number;#Text;#Number" array indexes 1, 3 would be the numbers ("Text(0);#Number(1);#Text(2);#Number(3)"). I think this method is easier and safer to use if the pattern is indeed fixed, as it avoids the need to verify data types.

Public Function GetNums(src As String) As String
    Dim arr
    Dim i As Integer
    Dim result As String
    arr = Split(src, ";#") ' Split the string to an array.
    result = ""
    For i = 1 To UBound(arr) Step 2 ' Loop through the array, starting with the second item, and skipping one item (using Step 2).
        result = result & arr(i) & ", "
    Next
    If Len(result) > 2 Then
        GetNums = Left(result, Len(result) - 2) ' Remove the extra ", " at the end of the the result string.
    Else
        GetNums = ""
    End If
End Function

这篇关于Excel UDF用于捕获字符内的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆