【问题标题】:Compare date range to array values将日期范围与数组值进行比较
【发布时间】:2015-11-18 11:24:12
【问题描述】:

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

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

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!

【问题讨论】:

    标签: arrays excel vba date concatenation


    【解决方案1】:

    如果这确实是您的代码的副本,那么该行:

    ConcatRange_V = ConcatR.Value
    

    ConcatR 指向一个尚未赋值的变量。它应该是:

    ConcatRange_V = ConcatRange.Value
    

    【讨论】:

    • 很好看。它经历了重命名过程,但这不是问题。将更新问题中的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 2018-08-25
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多