将日期范围与数组值进行比较

时间:2015-08-24 22:00:32

标签: arrays excel vba date concatenation

我正在尝试创建一个函数来连接一系列单元格,如果它们各自的日期是Bigger或Equal(> =)到存储在工作表单元格中的特定日期。

在下面的代码中,DatesRange_V是一个包含日期列表的数组,ThisDate是包含日期的A1。我希望循环遍历ConcatRange_V并在日期大于ThisDay时将它们与分隔符连接起来。

Function ConcatRangeIf() As String
Dim ConcatRange As Range
Dim DatesRange As Range

Set ConcatRange = Range("RP_Names")  'A list of names to concat
Set DatesRange = Range("RP_Dates")  'A list of dates

Dim Result As String
Dim Seperator As String
Dim FirstCell As Boolean

Dim ConcatRange_V() As Variant
Dim DatesRange_V() As Variant
Dim ThisDate As Variant

FirstCell = True
Seperator = ", "

ConcatRange_V = ConcatRange.Value
DatesRange_V = DatesRange.Value
ThisDate = Range("a1").Value ' A date on the current sheet

Dim i As Integer
For i = 1 To UBound(ConcatRange_V)
    If ConcatRange_V(i, 1) <> "" And DatesRange_V(i, 0) >= ThisDate Then
        If FirstCell Then
            Result = ConcatRange_V(i, 1)
        Else
            Result = Result & Seperator & ConcatRange_V(i, 1)
        End If
    End If
FirstCell = False
Next

ConcatRangeIf = Result
End Function

似乎存在问题,因为我放置=ConcatRangeif()的单元格正在返回#VALUE!

0 个答案:

没有答案