我有一个日期列表,我正在尝试添加到一个日期开始的日程表中。我尝试了很多不同的东西,但我无法解决这个问题。无论我尝试什么,它每次都会出现错误。如果我将日期更改为数字(例如2015年1月12日至12日),则可以正常工作。
'Set the start of my column number.
Col = 1
'Set it to stop when the dates stop
Do While Not IsEmpty(Range("A" & Col))
'See if the date entered is past the previous date e.g. 01/01/15
If Cells(3, Col).Value > Cells(1, Col).Value Then
'Then see if it is before the next date e.g. 07/01/15
If Cells(3, Col) < Cells(1, (Col + 1)).Value Then
'If both are true value ins the schedule will be "service"
Cells(2, Col).Text = "Service"
Col = Col + 1
'loops for next set of dates
Loop
我是否需要使用.value
以外的其他内容作为日期(如果有帮助,则以dd / mm / yyyy格式输入)或者是否有我不知道需要使用的功能在VBA中,为了将它们识别为日期?
或者作为替代方案,最好使用DateDiff
代替&#34;&gt;&#34;和&#34;&lt;&#34;将日期作为相关两个日期之间的差异返回。然后,找到两个与输入日期差异小于7的日期,然后使用它将它们添加到日程表中?在上面的代码中替换下面的行?
DiffBefore = DateDiff(DateInterval.Day, Cells(3, Col), Cells(1, Col))
感谢您的帮助
答案 0 :(得分:0)
以下是检查日期 d3 是否介于其他两个日期 d1 和 d2 之间的一般方法:
Public Function DateCheck(d1 As Date, d2 As Date, d3 As Date) As Boolean
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction
If d3 >= wf.Min(d1, d2) And d3 <= wf.Max(d1, d2) Then
DateCheck = True
Else
DateCheck = False
End If
End Function