根据VBA中的date值识别天数

Col K的date和date一致,以及Col M的这些天相应的某些值(数字)。

我有一个代码,如果它们大于1,并且如果它们有一个文本“正在等待”颜色,就会改变这些值的颜色。

我不知道要做的是,将下面的条件添加到此代码中:

我想确定这些日子是否属于星期天。

2.如果是,那么我想检查星期天的时间(可以说date/时间格式是“15/1/2016 17:00”,所以星期天过去的剩余时间是0.3天)从号码,如果号码大于1,则应该用“红色”突出显示。

3.减法不应该影响或出现在当前表格中。

我试了下面的代码,但我不知道我犯了什么错误,因为没有结果。

Sub Datefilter() Dim r As Long Dim m As Long On Error GoTo ExitHere: m = Range("M:P").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row Application.ScreenUpdating = False For r = 1 To m remainingDay = 0 If Weekday(Range("K" & r)) = 1 Then remainingDay = Round((24 - Format(TimeValue(Range("K" & r)), "h")) / 24, 1) End If If Range("P" & r) = "*waiting*" Then If Range("M" & r) - remainingDay >= 1 Then Range("M" & r).Cells.Font.ColorIndex = 3 Else Range("M" & r).Cells.Font.ColorIndex = 0 End If End If Next r ExitHere: Application.ScreenUpdating = True End Sub 

尝试这个:

 Sub Datefilter() Dim r, lastrow, remainingDay As Long 'On Error GoTo ExitHere: ' I recommend to delete this lastrow = Range("M:P").Cells(Rows.Count, "A").End(xlUp).Row Application.ScreenUpdating = False For r = 1 To lastrow remainingDay = 0 If Weekday(Range("K" & r).Value, vbSunday) = 1 Then remainingDay = Round((24 - Format(TimeValue(Range("K" & r)), "h")) / 24, 1) If InStr(1, Range("P" & r).Text, "waiting", vbTextCompare) > 0 Then If Range("M" & r) - remainingDay >= 1 Then Range("M" & r).Cells.Font.ColorIndex = 3 Else Range("M" & r).Cells.Font.ColorIndex = 0 End If End If End If Next r 'ExitHere: ' I recommend to delete this Application.ScreenUpdating = True End Sub 

我觉得使用Excel的内置函数和一些帮助列会容易得多。

(1)使用WEEKDAY()函数获取星期几。 然后用一个简单的比较来检查它是否是星期天。

(2)date存储为自1900年1月0日以来的过期时间,部分date为分数。 因此,要返回时间,只需从date中取出四舍五入的位: =A1-ROUNDDOWN(A1,0)

(3)使用条件格式检查单元格是否小于1,然后将其变成红色。

让我知道你是否想要一个例子的截图。