【问题标题】:isnumber, parse through sentence for variable wordisnumber,通过句子解析变量词
【发布时间】:2012-01-23 21:07:58
【问题描述】:

我目前正在使用 isnumber 来确定特定单词是否存在于包含句子的单元格中。我正在尝试确定如何找到始终在特定单词之前或之后的数字的值。如何操作公式或创建一系列公式,以提取位于“常量词”之前或之后的“可变词”?

例如,
有 39 只猫跑过 45 个门。
有 32 只猫跑过 20 道门。

我正在寻找穿过大门的猫的数量。

我目前的进度...

猫的数量总是在 1 - 100 之间,门的数量也总是在 1 - 100 之间。 目前,我正在运行 100 列。每列都有一个公式,例如 =ISNUMBER(SEARCH(" 2 个猫",'包含句子的单元格')),下一列将是 =ISNUMBER(SEARCH(" 3 个猫",'包含句子的单元格')) .这将返回 true 或 false,以便我可以有一列在整个行中找到 true 并返回该单元格的 cat 值...

我目前的问题...

每个句子中有两个数字,所以...我目前有两个真实的列。

问题...

有没有办法提取单词cats之前的数字? 猫的数量永远不会总是大于门的数量,也不总是小于门的数量。 “猫”这个词在每个句子中都会保持不变,但数量会改变......

希望这足够清楚,有人可以提供帮助!提前致谢!!

刚刚添加 - 我意识到在我当前的计算机上运行的是入门版,所以我无法访问 VBA。我发现使用这个公式效果很好。 =LOOKUP(1,RIGHT(TRIM(LEFT(J46,FIND("cats",J46)-1)),{1,2,3,4,5,6}))。 尽管它将所有数字都返回为正数。

我现在的问题是——你如何找到一个跟在常量词后面而不是在它前面的值? ——

【问题讨论】:

  • how do you find a value that follows a constant word - 这是一个不同的问题吗?我下面的公式立即找到数字 preceding "cats" - 立即 following "dogs" 试试这个 =LOOKUP(1000,MID(A1,FIND("dogs",A1)+4,{1,2,3,4})+0)
  • 谢谢巴里!这比我使用 =MID(A1,(FIND("cats",A1)+6),2) 的等式完美而且可能更有效,其中 6 是您想跳到的右侧空格数, 2 是您想要返回的位数。假设句子的格式保持不变,其中右侧的空格数在整个单元格中始终保持不变。

标签: excel


【解决方案1】:

这个公式应该可以从A1 中的文本中得到猫的数量

=LOOKUP(1000,MID(A1,FIND("cats",A1)-{1,2,3,4},{1,2,3,4})+0)

你可以对“门”做同样的事情。注意: FIND 区分大小写,因此它会找到“cats”而不是“Cats” - 如果您不希望区分大小写,请更改为 SEARCH

说明:公式的MID部分,即这部分

MID(A1,FIND("cats",A1)-{1,2,3,4},{1,2,3,4})

返回一个包含 4 个值的“数组”,例如如果 A1 包含 There were 3 cats that ran through 20 gates 则将返回此“数组”。 {" ","3 "," 3 ","e 3 "} 然后 +0 将它们转换为数字(或错误),这样你就得到了

=LOOKUP(1000,{#VALUE!,3,3,#VALUE!})

如果查找值大于查找数组中的任何数字(它将始终在此处),则 LOOKUP 采用 last 数字,即 3

您可以通过更改为来满足最多 9999 的号码

=LOOKUP(10000,MID(A1,FIND("cats",A1)-{1,2,3,4,5},{1,2,3,4,5})+ 0)

以同样的方式调整更高的数字

【讨论】:

  • 我喜欢这个答案,但我不熟悉在公式中使用 {。它在做什么?按照这些思路,我将如何更改它以获取 > 999 的数字?
【解决方案2】:

只需将其放入模块中并在新单元格中使用该函数:=findPrevCount(A1,"cats")

Function findPrevCount(ByVal sString As String, ByVal sKey As String) As Long
    Dim v As Variant
    Dim i As Integer

    sString = Replace(sString, ".", "")   'strips periods
    sString = Replace(sString, ",", "")   'strips commas
    v = Split(sString, " ")               'turn sentence into several words

    For i = LBound(v) To UBound(v)
        'case insensitive search, can use wild cards
        If LCase(v(i)) Like LCase(sKey) And i > 1 Then
            If IsNumeric(v(i - 1)) Then
                findPrevCount = v(i - 1)
                Exit Function
            End If
        End If
    Next i

End Function

这应该适用于任何文本和任何金额。如果你想确保一个大于另一个,只需添加逻辑,使用 =IF(findPrevCount(C2,"cat*") >= findPrevCount(C2, "gate*"), findPrevCount(C2, "cat* "), "没有足够的猫。")

String                                          Cats    Gates   Result
There were 39 cats that ran through 25 gates.   39      25      39
There were 32 cats that ran through 40 gates.   32      40      Not enough cats.
There were 2 cats that ran through 1 gate.      2       1       2
There was 1 cat that ran through 1 gate.        1       1       1
There was 1 cat that ran through 2 gates.       1       2       Not enough cats.

【讨论】:

    【解决方案3】:

    这是一个更简单的函数,只要数字和它前面的单词之间有空格,它就可以工作。

    Function FindValue(ByVal sString As String, ByVal sKey As String) As Integer
       Dim iFound As Integer
    
       iFound = InStr(LCase(sString), LCase(sKey))
       If iFound > 0 Then
          sString = Trim(Mid(sString, 1, iFound - 1))          ' discard the key and everything after it
          sString = Trim(Mid(sString, InStrRev(sString, " "))) ' discard everything before the number
       End If
    
       FindValue = Val(sString)
    
    End Function
    

    如果您需要它处理 > 999 的值,请使用 replace 去掉任何逗号。

    FindValue = Val(Replace(sString, ",", ""))
    

    【讨论】:

      猜你喜欢
      • 2013-04-06
      • 2018-07-13
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多