从多行string(如键值string)中提取文本
Excel中有一个单元格文本,如下所示
单元格A1:
Key1: Value1 Key2: Value2 Key3: Value3 Key Name4: Value4 Key5: Value5
现在我想提取Value4
因为我知道它的关键是Key Name4
,任何使用Excel公式的build议?
PS:在我感兴趣的键之前有一个空格
这是另一个使用不区分大小写的search:
=TRIM(MID(A1,SEARCH(B2,A1)+LEN(B2)+1,IFERROR(FIND(CHAR(10),A1,SEARCH(B2,A1))-SEARCH(B2,A1)-LEN(B2),LEN(A1))))
假设您正在search的键值是在B4
,以下应该工作:
=MID(A1,FIND(B4,A1)+LEN(B4)+2,IFERROR(FIND(CHAR(10),MID(A1,FIND(B4,A1)+LEN(B4)+2,LEN(A1))),LEN(A1)))
用A1中的数据,在B1中input:
=LEFT(MID(A1,FIND("Key Name4: ",A1)+10,9999),FIND("Key5",MID(A1,FIND("Key Name4: ",A1)+10,9999))-1)
这就是我的VBA解决scheme的工作原理:
这是代码:
Option Explicit Public Function TakeMeAValue(rngRange As Range, lngCounter As Long) Application.Volatile Dim lngStart As Long Dim lngEnd As Long Dim strWord As String: strWord = "Key" Dim strWordToSearch As String On Error GoTo TakeMeAValue_Error strWordToSearch = CStr(strWord & lngCounter & ":") lngStart = InStr(1, rngRange, strWordToSearch) + Len(strWordToSearch) lngEnd = InStr(lngStart, rngRange, strWord & lngCounter + 1) If lngEnd = 0 Then lngEnd = Len(rngRange) + 1 End If TakeMeAValue = Trim(Mid(rngRange, lngStart, (lngEnd - lngStart))) Exit Function TakeMeAValue_Error: TakeMeAValue = err.Description End Function