如何在Countif中连接偏移范围

时间:2019-05-03 20:48:20

标签: excel concatenation formula countif

我在Excel中有一个公式,可以告诉我前几行中是否已发生列组合。我用它作为数组公式。

{=MATCH([@[col1]]&[@[col2]],$K$9:K11&$H$9:H11)}

我也尝试了COUNTIF,但它不能像这样工作。

问题是我需要将此绝对引用更改为相对。

到目前为止,最优雅的解决方案是使用OFFSET函数(行从第10行开始):

{=MATCH([@[col1]]&[@[col2]],OFFSET([@[col1]],-(ROW()-10),0,ROW()-9,1)&OFFSET([@[col2]],-(ROW()-10),0,ROW()-9,1))}

不幸的是,这种方法不起作用。有人解决吗?

我计数还是真/假都没关系。

Does not matter if I get count or just true/false

1 个答案:

答案 0 :(得分:0)

我已通过在VBA中创建自定义函数来解决此问题:

Function FIRSTOCCURANCE(StartFromRow As Integer, _
                             val1 As Range, _
                             val2 As Range) As Boolean

Dim RangeX() As Variant, RangeY() As Variant, RangeZ() As Variant, Concat() As Variant
Dim lastRow As Integer, i As Integer, j As Integer: j = 0
lastRow = val1.Row - 1

RangeX = Range(Cells(StartFromRow, val1.Column), Cells(lastRow, val1.Column)).Value
RangeY = Range(Cells(StartFromRow, val2.Column), Cells(lastRow, val2.Column)).Value
'when assigning from range always is 2-D array

For i = LBound(RangeX, 1) To UBound(RangeX, 1)
    ReDim Preserve Concat(j)
    Concat(j) = RangeY(i, 1) & RangeX(i, 1)
    j = j + 1
Next i

FIRSTOCCURANCE = IsError(Application.Match(val2.Value & val1.Value, Concat, 0))

End Function