我有下面的代码,它将识别一个星期日,并在Col M中突出显示值,如果它们大于1并且文本为#34;则等待"在col P。
我想做的是:
示例场景:
如果日期/时间是col K中的1月22日21:00,那么这里的剩余时间是0.3小时..这应该从col M中的值中减去,假设col M有1.3,所以1.3-0.3 = 1.所以应该突出显示。
代码:
Sub SundayDatefilter()
Dim r, lastrow, remainingDay As Long
lastrow = Range("M:P").Cells(Rows.count, "A").End(xlUp).Row
Application.ScreenUpdating = False
For r = 2 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
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
正如我在评论中提到的,只需更改remainingDay的数据类型,因为Long
是整数类型(无小数部分)。
Sub SundayDatefilter()
Dim r, lastrow, remainingDay As Double '<--- Correction
lastrow = Range("M:P").Cells(Rows.Count, "A").End(xlUp).Row
Application.ScreenUpdating = False
For r = 2 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 '<--- Correction
Range("M" & r).Cells.Font.ColorIndex = 3
Else
Range("M" & r).Cells.Font.ColorIndex = 0
End If
End If
End If
Next r
Application.ScreenUpdating = True
End Sub