查找基于命名范围中的条件,然后连接结果

时间:2012-03-26 20:42:54

标签: excel excel-vba vba

我们有一个有趣的公式可以搞清楚。

首先,我们计算以下内容,其中MYDATE需要介于两个日期之间。 REGISTER.DT和EXIT.DT都是命名范围A:A和B:B,MYDATE是指向C1的命名范围。

  

= SUMPRODUCT( - (REGISTER.DT&LT =数值指明MyDate)*(EXIT.DT&GT =指明MyDate))

和另一个例子,有更多标准(有时最多5个标准)

  

= SOMPRODUCT( - (AANM.DT&GT = DT.START)(AANM.DT&LT = DT.EIND)(TYPE.TXT = I8))

现在提出问题。上面的formule返回2(所以它找到两个日期)

我们现在想要以串联字符串

查找并显示这些日期

我一直在摆弄我在网上找到的VBA函数StringConcat,但我不会跳过零(将所有转换为字符串),但也要了解命名范围。

有人有提示吗?或者可以起作用的矩阵函数查找?

非常感谢

1 个答案:

答案 0 :(得分:3)

这是一个基于您的设计工作的用户定义函数。在前两个参数中输入两个匹配的数组,然后在第三个参数中输入关键日期。您将返回注册日期列表,这些日期标记持有该关键日期的范围的开始:

Option Explicit

Function DateCAT(RegisterRng As Range, ExitRng As Range, MyDt As Date) As String
Dim DtARR As Variant, D As Long

If RegisterRng.Cells.Count <> ExitRng.Cells.Count Then
    DateCAT = "date ranges do not match"
    Exit Function
End If

DtARR = Union(RegisterRng, ExitRng)

For D = LBound(DtARR) To UBound(DtARR)
    If DtARR(D, 1) <= MyDt And DtARR(D, 2) >= MyDt Then
        DateCAT = DateCAT & ", " & DtARR(D, 1)
    End If
Next D

If DateCAT = "" Then
    DateCAT = "none"
Else
    DateCAT = Mid(DateCAT, 3, Len(DateCAT))
End If

End Function

= DATECAT(REGISTER.DT,EXIT.DT,MYDATE)

enter image description here

顺便说一句,我用于REGISTER.DT和EXIT.DT的公式是动态的。

<强> = OFFSET(!Sheet 1中$ A $ 1 ,,, COUNTA(Sheet 1中$ A:$ A))

<强> = OFFSET(Sheet 1中$ B $ 1 ,,, COUNTA(Sheet 1中$ B:!$ B))

============================ 这个版本就像COUNTIFS(),你把范围从第一个返回,然后列出值对...一个范围,然后是对该范围的测试,然后是另一个可选范围和测试,最多总共5个。

= DATECAT(值,TstRng1,Test1,OptTstRng2,OptTest2,OptTstRng3,OptTest3)

Option Explicit

Function DateCAT(RegisterRng As Range, RNG1 As Range, TST1 As String, _
    Optional RNG2 As Range, Optional TST2 As String, _
    Optional RNG3 As Range, Optional TST3 As String, _
    Optional RNG4 As Range, Optional TST4 As String, _
    Optional RNG5 As Range, Optional TST5 As String) As String

Dim D As Long, Bad As Boolean, i As Long

D = RegisterRng.Cells.Count

If RNG1.Cells.Count <> D Then Bad = True
If Not RNG2 Is Nothing Then If RNG2.Cells.Count <> D Then Bad = True
If Not RNG3 Is Nothing Then If RNG3.Cells.Count <> D Then Bad = True
If Not RNG4 Is Nothing Then If RNG4.Cells.Count <> D Then Bad = True
If Not RNG5 Is Nothing Then If RNG5.Cells.Count <> D Then Bad = True

If Bad Then
    DateCAT = "data ranges do not match"
    Exit Function
End If

For i = 1 To RNG1.Cells.Count
    If WorksheetFunction.CountIf(RNG1.Cells(i), TST1) = 0 Then Bad = True

    If Not RNG2 Is Nothing Then If WorksheetFunction.CountIf(RNG2.Cells(i), TST2) = 0 Then Bad = True
    If Not RNG3 Is Nothing Then If WorksheetFunction.CountIf(RNG3.Cells(i), TST3) = 0 Then Bad = True
    If Not RNG4 Is Nothing Then If WorksheetFunction.CountIf(RNG4.Cells(i), TST4) = 0 Then Bad = True
    If Not RNG5 Is Nothing Then If WorksheetFunction.CountIf(RNG5.Cells(i), TST5) = 0 Then Bad = True

    If Not Bad Then DateCAT = DateCAT & ", " & RegisterRng.Cells(i).Value
    Bad = False
Next i

If DateCAT = "" Then
    DateCAT = "none"
Else
    DateCAT = Mid(DateCAT, 3, Len(DateCAT))
End If

End Function

重要提示:当检查命名范围(一个单元格,具有1个值)时,您需要输入TST1等作为operater&amp;命名范围:">"&MYCELL

enter image description here