我将列与日期和数字进行比较。我总共有8列(A-H)。 A列和B列包含数字。 (在某些情况下,B列是空的)。 D列(sdate)和E(Ldate)包含日期。 (在某些情况下,列E为空)。
我现在有4个案子。
案例1是sdate< 2周的Ldate,然后按时打印。
case2是sdate> 4周的Ldate,打印延迟。
案例3是sdate< 4weeks> 2周Ldate,然后打印项目remaning.with案例3,还有另一个条件,如果列A,B和D存在,但在E列没有日期,它仍应打印剩余。
case4:A列包含数字,B列没有数字,D列有日期,E列没有日期,应该留空。
论坛的2位专家指导我达到了标准,但是使用下面的代码,有一个错误,它并不是satistfy case4。
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long, i As Long
Dim zWeeks As Double, zcolour As Long
Dim Ztext As String
Set ws = Sheets("Preparation Sheet")
With ws
lRow = .Range("D" & .Rows.Count).End(xlUp).Row
For i = 2 To lRow
zWeeks = DateDiff("ww", .Range("E" & i).Value, .Range("D" & i).Value)
If .Range("A" & i).Value <> "" And .Range("B" & i).Value <> "" And .Range("E" & i).Value = "" Then
Ztext = "remaining"
zcolour = vbYellow
Cells(i, 7) = "Yellow"
ElseIf zWeeks < 2 Then
Ztext = " on time"
zcolour = vbGreen
Cells(i, 7) = "Green"
ElseIf zWeeks > 4 Then
Ztext = "delayed"
zcolour = vbRed
Cells(i, 7) = "Red"
ElseIf zWeeks > 2 < 4 Then
Ztext = "remaining"
zcolour = vbYellow
Cells(i, 7) = "Yellow"
End If
With .Range("F" & i)
.Value = Ztext
.Interior.Color = zcolour
End With
Next i
End With
End Sub
答案 0 :(得分:0)
尝试在结束前添加这些行,如果
ElseIf IsNumeric(Range("A" & i).Value) And (Not IsNumeric(Range("A" & i).Value)) And IsDate(Range("D" & i).Value) And Range("E" & i).Value = "" Then
Ztext = ""
zcolour = vbYellow
Cells(i, 7) = "Yellow"
答案 1 :(得分:0)
试试这个
注意:未经测试
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long, i As Long
Dim zWeeks As Double, zcolour As Long
Dim Ztext As String
Set ws = Sheets("Preparation Sheet")
With ws
lRow = .Range("D" & .Rows.Count).End(xlUp).Row
For i = 2 To lRow
lDate = .Range("E" & i).Value
sDate= .Range("D" & i).Value
zWeeks = DateDiff("ww",sDate , lDate)
If .Range("A" & i).Value <> "" And .Range("B" & i).Value <> "" And .Range("E" & i).Value = "" Then
Ztext = "remaining"
zcolour = vbYellow
Cells(i, 7) = "Yellow"
ElseIf .Range("A" & i).Value <>"" And .Range("B" & i).Value = "" And IsDate(sDate) And Ldate = "" Then
Ztext = ""
zcolour = vbYellow
Cells(i, 7) = "Yellow"
ElseIf zWeeks < 2 Then
Ztext = " on time"
zcolour = vbGreen
Cells(i, 7) = "Green"
ElseIf zWeeks > 4 Then
Ztext = "delayed"
zcolour = vbRed
Cells(i, 7) = "Red"
ElseIf zWeeks > 2 < 4 Then
Ztext = "remaining"
zcolour = vbYellow
Cells(i, 7) = "Yellow"
End If
With .Range("F" & i)
.Value = Ztext
.Interior.Color = zcolour
End With
Next i
End With
End Sub
答案 2 :(得分:0)
在第一个条件之后添加一个新条件,检查Col.B和E上的值是否为空白。
ElseIf .Range("B" & i).value = "" And .Range("E" & i).value = "" Then
GoTo nextrow
然后在循环结束前添加标签。这将跳过其他条件并移至下一行:
nextrow:
Next i