以下是创建字典并从 3 列 (i,1) 键中分配值以及来自 getdates() 的日期数组从 startdate(i,2) 和 enddate(i,3) 的代码。
我的目标是按键打印每个日期(一个键可能有多个日期),但下面的代码给我类型不匹配的错误。
我的目标是按键打印每个日期(一个键可能有多个日期),但下面的代码给我类型不匹配的错误。
Sub Test_Dates()
Dim TESTWB As Workbook
Dim TESTWS As Worksheet
Set TESTWB = ThisWorkbook
Set TESTWS = TESTWB.Worksheets("TEST")
Dim Dict As New Scripting.Dictionary
For i = 2 To TESTWS.Cells(1, 1).End(xlDown).Row
Dict.Add TESTWS.Cells(i, 1).Value, getDates(TESTWS.Cells(i, 2), TESTWS.Cells(i, 3))
Next i
For Each Key In Dict.Keys
Dim idate As Variant
For Each idate In Dict.Items
Debug.Print idate 'this line is where the error is
Next idate
Next Key
End Sub
这是一个获得在起始日期和结束日期之间的日期数组的函数
Function getDates(ByVal StartDate As Date, ByVal EndDate As Date) As Variant
Dim varDates() As Date
Dim lngDateCounter As Long
ReDim varDates(0 To CLng(EndDate) - CLng(StartDate))
For lngDateCounter = LBound(varDates) To UBound(varDates)
varDates(lngDateCounter) = CDate(StartDate)
StartDate = CDate(CDbl(StartDate) + 1)
Next lngDateCounter
getDates = varDates
End Function
idate
都是一个数组,因此您不能将其作为字符串返回。为了测试代码是否正确返回到该点,请尝试Debug.Print idate(0),idate(UBound(idate))
。通过这种方式,它将从数组字典项中“提取”StartDate
和EndDate
... - FaneDuruDim varDates
,则可以简单地返回:Debug.Print Join(idate, ", ")
。并且使用StartDate = StartDate + 1
应该就足够了... - FaneDuru