如果空白没有停止循环

时间:2016-11-24 10:59:08

标签: vba excel-vba excel

我需要你的帮助:

COLUMN J(完成日期)

COLUMN G(DUE DATE)

COLUMN M(将显示:提前,按时或延迟)

  • 当J为空时我希望循环停止
  • 下面没有我的代码,J列中的所有单元格都填满了
  • D值始终为0(我在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

2 个答案:

答案 0 :(得分:0)

在你的代码D中是布尔值。这意味着它有两个值 - TrueFalse。因此代码如下: 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