我需要你的帮助:
COLUMN J(完成日期)
COLUMN G(DUE DATE)
COLUMN M(将显示:提前,按时或延迟)
注意:当我尝试使用一个单元而不是范围时,它可以正常工作
Sub TIMESTATUS()
Dim CompletionDate As Long
Dim DueDate As Long
Dim D As Boolean
For Each C In Sheet1.Range("j:j")
If C.Value = "" Then
Exit For
Else
For Each g In Sheet1.Range("g:g")
CompletionDate = C.Value
DueDate = g.Value
D = CompletionDate - DueDate
If D > 0 Then
Range("m:m").Value = "Delay"
ElseIf D < 0 Then
Range("m:m").Value = "Early"
ElseIf D = 0 Then
Range("m:m").Value = "On Time"
End If
Next
End If
Next
End Sub
答案 0 :(得分:0)
在你的代码D
中是布尔值。这意味着它有两个值 - True
或False
。因此代码如下:
If D > 0 Then
ElseIf D < 0 Then
ElseIf D = 0 Then
毫无意义......
您可以在此处阅读更多布尔值:
https://msdn.microsoft.com/en-us/library/wts33hb3.aspx
答案 1 :(得分:0)
Sub TIMESTATUS()
Dim C As Range
Dim strng As String
With Sheet1 '<--| reference your sheet
For Each C In .Range("J:J").SpecialCells(xlCellTypeConstants, xlNumbers) '<--| loop through its column J not empty cells with numbers only
Select Case C.Value - .Cells(C.row, "G").Value '<--| check the difference between curent column "J" cell and its corresponding one i column "G" against following cases
Case Is > 0 '<--| if difference >0
strng = "Delay"
Case Is < 0 '<--| if difference <0
strng = "Early"
Case Else '<--| esle (i.e. if difference =0)
strng = "On Time"
End Select
.Cells(C.row, "M").Value = strng '<--| write down the value
Next
End With
End Sub