簡體   English   中英

Excel UDF用於捕獲字符內的數字

[英]Excel UDF for capturing numbers within characters

我在單元格A1中有一個可變文本字段,其中包含以下內容:

文字; #Number; #Text; #Number

  • 該格式可以重復,但是模式始終為Text; #Number。
  • 數字范圍從1位數到n位數(上限7)

例:

原始值

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

要求值:

123, 3456, 78

根據我的理解,該字段對於excel公式而言太可變了。

我嘗試使用但是在編碼方面我還是一個初學者。 如果您可以使用一些解釋性文字來分解代碼,將不勝感激。

我嘗試了下面的一些建議,它們運行良好。 還有一個問題。

現在,我可以從文本中拆分數字了,有什么方法可以利用下面的代碼並添加另一層,我們將這些數字拆分為x個單元格。

例如:一旦運行該函數,如果在同一單元格中獲得1234、567,則該函數會將1234放入單元格B2,將567放入單元格C2。 這將繼續更新同一行中的所有單元格,直到字符串用盡了從該函數檢索到的所有數字為止。

謝謝

這是John Coleman建議的方法:

Public Function GetTheNumbers(st As String) As String
    ary = Split(st, ";#")
    GetTheNumbers = ""

    For Each a In ary
        If IsNumeric(a) Then
            If GetTheNumbers = "" Then
                GetTheNumbers = a
            Else
                GetTheNumbers = GetTheNumbers & ", " & a
            End If
        End If
    Next a
End Function

如果模式是固定的,並且數字的位置永遠不變,則可以假定數字將位於字符串的偶數位置。 這意味着在源字符串拆分的數組結果中,可以使用結果數組的奇數索引。 例如,在此字符串“文本;# 數量 ; #Text;# 號碼 ”數組索引1,3將是數字(“文本(0);# 號(1); #Text(2);# 號(3) ”)。 我認為,如果模式確實是固定的,則此方法更容易使用,也更安全,因為它避免了驗證數據類型的需要。

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

數字可以從1位數到n位數不等( 限制為7

其他響應似乎都沒有考慮到提供的參數,因此我將一個真正的解決方案合並在一起。

Option Explicit
Option Base 0    '<~~this is the default but I've included it because it has to be 0

Function numsOnly(str As String, _
                  Optional delim As String = ", ")
    Dim n As Long, nums() As Variant
    Static rgx As Object, cmat As Object

    'with rgx as static, it only has to be created once; beneficial when filling a long column with this UDF
    If rgx Is Nothing Then
        Set rgx = CreateObject("VBScript.RegExp")
    End If
    numsOnly = vbNullString

    With rgx
        .Global = True
        .MultiLine = False
        .Pattern = "[0-9]{1,7}"
        If .Test(str) Then
            Set cmat = .Execute(str)
            'resize the nums array to accept the matches
            ReDim nums(cmat.Count - 1)
            'populate the nums array with the matches
            For n = LBound(nums) To UBound(nums)
                nums(n) = cmat.Item(n)
            Next n
            'convert the nums array to a delimited string
            numsOnly = Join(nums, delim)
        End If
    End With
End Function

僅nums

使用Replace表達式選項

Sub Test()
Debug.Print StrOut("MyName;#123;#YourName;#3456;#HisName;#78")
End Sub

功能

Option Explicit
Function StrOut(strIn As String) As String
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
    .Pattern = "(^|.+?)(\d{1,7})"
    .Global = True
    If .Test(strIn) Then
        StrOut = .Replace(strIn, "$2, ")
        StrOut = Left$(StrOut, Len(StrOut) - 2)
    Else
        StrOut = "Nothing"
    End If
End With
End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM