0

例子

这个问题是我之前在以下位置发布的问题的延续: Excel - Return value, after cell has been mapped, between headers

和以前一样(以及更多):

• 如果一个数字是负数:

• 使用 MATCH 检查范围内(标题之间)是否有对应的(正数)数字并且“IDX”是否相同

• 如果有 MATCH,则输出已匹配的 F 的 ID(第 1 列)的值,然后,输出已匹配的负值的 ID;

• 如果没有,什么也不做。

问题是:

• 它需要循环,因为负值可以在正值之后;

• 搜索必须包含在标题之间并且idx 必须相同。我认为我上传的图像很清晰,希望如此。

我已经尝试从 lori_m 修改以前的答案,添加一个新的变量和条件,但一切都刚刚结束:

Names.Add "i", "=$A$1:$A$12": _
Names.Add "v", "=$B$1:$B$12": _
Names.Add "m", "=$D$1:$D$12": _
Names.Add "h", [IF({1},LOOKUP(ROW(i),ROW(i)/(v=INDEX(v,1))))]: _
Names.Add "l", [IF({1},LOOKUP(ROW(i),ROW(i)/(v=INDEX(v,1)),FREQUENCY(-ROW(i),IF(v=INDEX(v,1),-ROW(i),0))-1))]: _
[m]=[IF(COUNTIF(OFFSET(v,h,,l),"=-"&v),CONCATENATE("=",ADDRESS(ROW(i),COLUMN(i),4),"&"" AND ""&INDEX(",ADDRESS(h+1,COLUMN(i)),":",ADDRESS(h+l,COLUMN(i)),",MATCH(-",ADDRESS(ROW(v),COLUMN(v),4),",",ADDRESS(h+1,COLUMN(v)),":",ADDRESS(h+l,COLUMN(v)),",0))"),"")]: _
Names("h").Delete: _
Names("l").Delete

我非常感谢所有的帮助。

4

1 回答 1

1

这是一些 VBA 代码,40,000 行大约需要 0.4 秒:

Public Sub MatchVals()

    Dim Arr As Variant, dic As Object, v As Variant, Match() As String
    Dim i As Long, j As Long, k As Long, n As Long

    Arr = ActiveSheet.UsedRange.Value2
    Set dic = CreateObject("scripting.dictionary")
    n = UBound(Arr)

    ReDim Match(1 To n, 1 To 1) As String
    k = n
    For i = n To 1 Step -1
        If Arr(i, 3) <> Arr(1, 3) Then
            dic.Add Arr(i, 3) & "|" & Arr(i, 2), i
        Else
            For j = i + 1 To k
                If Arr(j, 3) < 0 Then
                    v = dic(-Arr(j, 3) & "|" & Arr(j, 2))
                    If Not IsEmpty(v) Then
                        Match(v, 1) = Arr(v, 1) & " AND " & Arr(j, 1)
                    End If
                End If
                k = i - 1
            Next j
            dic.RemoveAll
        End If
    Next i

    With ActiveSheet.UsedRange
        .Offset(0, .Columns.Count).Resize(, 1).Value2 = Match
    End With
End Sub
于 2012-08-14T19:02:10.427 回答