谷歌电子表格:从文本单元格中提取多个数字

时间:2014-07-07 12:52:38

标签: excel search spreadsheet

我正在开发一个类似这样的谷歌电子表格(这是一个专栏):

1
2
3,4
5
6-9
15
18

3,4是3和​​4

6-9是6,7,8和9

在另一张纸上,我制作了一个标准的1-100列表,我必须找到一种方法来“标记”出现的数字或包含在上一个列表中的数字,如下所示。

1       YES
2       YES
3       YES
4       YES
5       YES
6       YES
7       YES
8       YES
9       YES
10      NO
11      NO
12      NO
13      NO
14      NO
15      YES

我可以通过在单元格B1中使用来轻松找到简单的唯一数字:     = IF((COUNTIF(列表$ A $ 1:$ A $ 100 VALUE(A1))= 1), “YES”, “NO”)

我甚至可以找到第一个数字:

=IFERROR(LEFT(list!$A$1:$A$100,(SEARCH(",",list!$A$1:$A$100)-1)),IFERROR(LEFT(list!$A$1:$A$100,(SEARCH("-",list!$A$1:$A$100)-1)),list!$A$1:$A$100))

但是我无法扩大搜索范围以包含“x-y”之间或“x,y”中逗号之后的数字。

我已经尝试了AND(“大于第一个数字”,“小于第二个数字”)但我还没有找到提取第二个数字的方法。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

这是一个适用于 excel 电子表格的宏。您可以将其改编为Google应用脚本:

Sub IsItThere()
    Dim s1 As Worksheet, s2 As Worksheet, N As Long
    Dim v As Variant, i As Long, ary()
    Dim N1 As Long, N2 As Long, j As Long
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")

    s1.Activate

    N = Cells(Rows.Count, "A").End(xlUp).Row
    Dim c As Collection
    Set c = New Collection
    For i = 1 To N
        v = Cells(i, 1).Value
        If InStr(1, v, ",") > 0 Then
            c.Add CLng(Split(v, ",")(0))
            c.Add CLng(Split(v, ",")(1))
        ElseIf InStr(1, v, "-") > 0 Then
            N1 = CLng(Split(v, "-")(0))
            N2 = CLng(Split(v, "-")(1))
            For j = N1 To N2
                c.Add j
            Next j
        Else
            c.Add CLng(v)
        End If
    Next i
    ReDim ary(1 To c.Count)
    For i = 1 To c.Count
        ary(i) = c.Item(i)
    Next i

    s2.Activate

    N = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To N
        v = Cells(i, 1).Value
        For j = 1 To c.Count
            If v = ary(j) Then
                Cells(i, 2).Value = "YES"
                GoTo pass
            End If
        Next j
        Cells(i, 2).Value = "NO"
pass:
    Next i
End Sub