2015-08-24 155 views
0

我試圖創建一個函數來連接一個單元格範圍,如果它們各自的日期是大於或等於(> =)到存儲在工作表單元格中的特定日期。比較日期範圍到數組值

在下面的代碼中,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!

回答

1

,如果這是真實地你的代碼的副本,然後行:

ConcatRange_V = ConcatR.Value 

ConcatR點尚未賦值的變量。它應該是:

ConcatRange_V = ConcatRange.Value 
+0

很好發現。它經歷了重命名過程,但這不是問題。將更新問題中的代碼。 – toms