如何findstring中的两个值的文本?

我目前有一个单元(“文件名”),它返回H:\ F0791 \采购申请[PCS.xlsm]

我想单独列出'F0791'的价值。 我以前使用MID函数,但是,如果'F0791'是不同的长度,这是行不通的。

是否有可能调用前两个之间的值,还是有更好的select?

如果你可以提供这个在公式和VBA,这将是伟大的。 这是其他问题所独有的,因为它们不提供配方替代品。

任何帮助,将不胜感激!

另一个公式方法是在单元格B2使用此公式=MID(A1,4,FIND("----",SUBSTITUTE(A1,"\","----",2),1)-4)如果你的string在列A

在这里输入图像说明

你可以使用这个使用分割函数的 UDF

 Function EXTRACTELEMENT(Txt As String, n, Separator As String) As String On Error GoTo ErrHandler: EXTRACTELEMENT = Split(Application.Trim(Mid(Txt, 2)), Separator)(n - 1) Exit Function ErrHandler: ' error handling code MsgBox "ERROR: Verify if the data exists, example if the separator is correct." On Error GoTo 0 End Function 

这是在VBA中的testing

 Sub test() Text = "H:\F0791\Purchase Requisitions[PCS.xlsm]" Debug.Print EXTRACTELEMENT(CStr(Text), 2, "\") End Sub 

您也可以将其添加到单元格中,如果E1 =“H:\ F0791 \ Purchase Requisitions [PCS.xlsm]”然后将其添加到所需的结果单元格中。

在单元格F1上,这个公式: =EXTRACTELEMENT(E1;2;"\")在下面的图像上给出结果: 例

或者打开插入function窗口 插入功能窗口

可选,UDF说明

该代码添加了 UDF 的描述 。 你必须运行一次。

 Sub DescribeFunction() Dim FuncName As String Dim FuncDesc As String Dim Category As String Dim ArgDesc(1 To 3) As String FuncName = "EXTRACTELEMENT" FuncDesc = "Returns the nth element of a string that uses a separator character" Category = 7 'Text category ArgDesc(1) = "String that contains the elements" ArgDesc(2) = "Element number to return" ArgDesc(3) = "Single-character element separator (spc default)" Application.MacroOptions _ Macro:=FuncName, _ Description:=FuncDesc, _ Category:=Category, _ ArgumentDescriptions:=ArgDesc End Sub 

对于一个公式的方法,下面为我工作,给定“文件名”单元格在A1

 =MID(A1,FIND("\",A1)+1,((FIND("\",A1,FIND("\",A1)+1))-(FIND("\",A1))-1)) 

这基本上find了第一个“\”和第二个“\”的位置,并使用mid()函数来拉动两个“\”之间的string。

= MID(A2,FIND(“\”,A2)+1,FIND(“\”,A2,4) – 4)

这对我来说很有效,假设除了代码长度之外,结构或多或less是相同的。