我试图遍历一列日期,以及其他两个设定日期(周的第一天,每周的最后一天)或(月的第一天,月的最后一天)内的任何日期
当我将列中的日期指定给变量时,出现类型不匹配错误。
Sub JobsDue(ByRef DateStart As Date, ByRef DateEnd As Date)
Dim LastRow As Integer
LastRow = Sheet1.Cells(Rows.count, "B").End(xlUp).Row
Dim JobDueDate As String
Dim JobPN As String
For i = 10 To LastRow
JobDueDate = Sheet1.Range("B" & 10, "B" & i).Value (ERROR OCCURS HERE)
JobPN = Sheet1.Range("C" & 10, "C" & i).Value
If (JobDueDate) >= DateStart And (JobDueDate) <= DateEnd Then
lbJobsDue.AddItem (JobPN & "," & CDate(JobDueDate))
End If
Next i
End Sub
Private Sub comboxJobsDue_Change()
If comboxJobsDue.Value = "This Week" Then
StartOfWeek = Date - Weekday(Date) + 2
endofweek = Date - Weekday(Date) + 6
lblJobsDue.Caption = (StartOfWeek) & "-" & (endofweek)
Call JobsDue((StartOfWeek), (endofweek))
ElseIf comboxJobsDue.Value = "Next Week" Then
StartOfWeek = Date - Weekday(Date) + 9
endofweek = Date - Weekday(Date) + 13
lblJobsDue.Caption = (StartOfWeek) & "-" & (endofweek)
ElseIf comboxJobsDue.Value = "This Month" Then
FirstDayInMonth = DateSerial(Year(Date), Month(Date), 1)
LastDayInMonth = DateSerial(Year(Date), Month(Date) + 1, 0)
lblJobsDue.Caption = FirstDayInMonth & "-" & LastDayInMonth
ElseIf comboxJobsDue.Value = "Next Month" Then
FirstDayInMonth = DateSerial(Year(Date), Month(Date) + 1, 1)
LastDayInMonth = DateSerial(Year(Date), Month(Date) + 2, 0)
lblJobsDue.Caption = FirstDayInMonth & "-" & LastDayInMonth
End If
End Sub
错误发生在第7行,当我运行程序进行调试时,JobDueDate获取日期,但格式为&#34; 9/25/2013&#34;,其他日期的格式为8 / 18/2014没有引号。任何人都可以向我解释为什么会发生这种错误以及如何解决它?
提前致谢,Evan
答案 0 :(得分:0)
请参阅阅读"Dim JobDueDate As String"
的行声明(我认为第4行)你告诉系统你想要它作为一个字符串...也许你想要Dim JobDueDate As Date
所以你可以使用日期函数在上面。
DateStart和End作为日期传入,因此如果传入无效日期,则调用此子代码时会出错。
此外,您似乎正在尝试将日期范围分配给日期字段。您需要一组日期或一组日期来处理这个问题。