【问题标题】:Extracting a substring from a cell at a certain index (different row length, multiple rows)从某个索引处的单元格中提取子字符串(不同的行长,多行)
【发布时间】:2019-02-16 06:11:07
【问题描述】:

作为一名会计师,我想知道是否有办法从不同长度的单元格中提取特定索引处的特定字符串。有时,我会收到一份 PDF 格式的对帐单,它不能很好地转换为 Excel 电子表格。

This is what I want to achieve

字符串在每个单元格中的索引相同。不过,我不太清楚如何隔离它们,因为每个字符串的长度不同,而且我想要的子字符串不是以相同的字符开头。我试过 LEN、MID 等来提取它们,但我不知道该怎么做。

我能够提取以特定字符(如 INV 和“45”)开头的项目:

=TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",99)),MAX(1,FIND("XXX",SUBSTITUTE(A1," ",REPT(" ",99)))-50),99))

但我不知道如何获取我要查找的字符串(在本例中为发票金额)。

【问题讨论】:

  • 使用文本到列在空间上拆分并只保留最后三列
  • Text To Columns 绝对是已经建议的最简单的方法。如果必须是公式,则在单元格 B2 中使用它并上下复制:=--TRIM(LEFT(RIGHT(SUBSTITUTE(TRIM($A2)," ",REPT(" ",LEN($A2))),LEN($A2)*(4-COLUMN(A$1))),LEN($A2)))
  • 谢谢你们,完美无缺。我不知道 Excel 有一个内置函数。 :)

标签: excel substring


【解决方案1】:

如果你不介意一些 VBA,你可以试试这个:

数据的基本问题是字段中包含的日期是可变长度的,并且在这些元素中包含唯一真正的分隔符(空格)。使用右侧的空格,可用于返回您的字段。

感谢 Sumit Bansal 在 (https://trumpexcel.com) 的原始 LastPosition() 函数。但是,此函数只返回最后一次出现的字符。
但稍作修改后,LastPositionN() 现在返回搜索文本的第 n 次出现,指定为第三个参数。

注意:点击此处查看原始功能,以及剪切/粘贴到 VBA 编辑器的信息:https://trumpexcel.com/find-characters-last-position/

Function LastPosition(rCell As Range, rChar As String)
'This function gives the last nth position of the specified character
'This code has been developed by Sumit Bansal (https://trumpexcel.com)

Dim rLen As Integer
rLen = Len(rCell)
For i = rLen To 1 Step -1
    If Mid(rCell, i - 1, 1) = rChar Then
        'LastPosition = i ' original code returnd Last position +1
        LastPosition = i - 1 ' correction returns codes original intent

        Exit Function
    End If
Next i
End Function

Function LastPositionN(rCell As Range, rChar As String, Optional nNthOccurance As Integer = 1)
    'This function gives the last position of the specified character
    'This code has been developed by Sumit Bansal
    ' (https://trumpexcel.com)
    ' and was modified my Jeff Bowman 9/11/2018 for nth position from right

    'Returns zero (0) if not nth occurence is not found
    Dim rLen As Integer, nNthCount As Integer, LastPosition As Integer

    nNthCount = 0

    rLen = Len(rCell)
    For i = rLen To 1 Step -1
        If Mid(rCell, i - 1, 1) = rChar Then
            'LastPosition = i ' original code returnd Last position +1
            LastPositionN = i - 1 ' correction returns codes original 
                                  'intent
            nNthCount = nNthCount + 1

            If nNthCount = nNthOccurance Then
                Exit Function
            End If

        End If
    Next i
End Function

`

现在,通过 left()、right() 和 find(),我们可以隔离并解析数据字符串的结尾。

=LEFT(RIGHT(A8,LastPositionN(A8," ",1)),FIND(" ",RIGHT(A8,LastPositionN(A8," ",1))))

【讨论】:

    猜你喜欢
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    相关资源
    最近更新 更多