用於提取第二個/連續括號內資料的公式

用於提取第二個/連續括號內資料的公式

從下面我想提取括號之間的數據(如果它只是數字)。

Nissan - X-Trail 發佈(五月)(六月) - SO9158518 (65124817) 旅遊 - Curious 2017(新聞)(44124469)

答案1

嘗試這個使用者定義的函數:

Public Function parens(t As String) As String
    Dim i As Long, L As Long, CH As String
    Dim x As String, y As String, temp As String
    Dim ary() As String, Kapture As Boolean, a As Variant
    Dim j As Long

    parens = ""
    L = Len(t)
    x = "("
    y = ")"
    Kapture = False
    temp = ""
    j = 1

    For i = 1 To L
        CH = Mid(t, i, 1)
        If CH = x Then
            Kapture = True
            temp = ""
            ReDim Preserve ary(1 To j)
            j = j + 1
        ElseIf CH = y Then
            ary(UBound(ary)) = temp
        Else
            If Kapture Then temp = temp & CH
        End If
    Next i

    For Each a In ary
        If IsNumeric(a) Then
            parens = a
            Exit Function
        End If
    Next a
End Function

在此輸入影像描述

它提取括號材料,直到找到數值。

使用者定義函數 (UDF) 非常易於安裝和使用:

  1. ALT-F11 調出 VBE 窗口
  2. ALT-I ALT-M 開啟新模組
  3. 將內容貼進去並關閉 VBE 窗口

如果儲存工作簿,UDF 將隨之儲存。如果您使用的是 2003 年以後的 Excel 版本,則必須將檔案另存為 .xlsm 而不是 .xlsx

若要刪除 UDF:

  1. 如上所示調出 VBE 窗口
  2. 清除程式碼
  3. 關閉VBE視窗

若要使用 Excel 中的 UDF:

=parens(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

必須啟用巨集才能使其工作!

相關內容