首页 > 解决方案 > Excel VBA 的局限性?

问题描述

我在 Excel VBA 中有这段代码,它检查一些记录的日期并检查它是否在我们正在寻找的日期范围内。这段代码上周似乎运行良好,但现在突然跳过记录。我发现,如果我要单步执行编码,它会拾取所有记录,但如果我让它运行,它似乎会跳过记录。谁能帮忙解释一下原因。我对 VBA 还很陌生,我相信有些人会对编码感到畏缩,但这是我所知道的,并且在我的最后期限内工作。欢迎任何帮助解释正在发生的事情以及避免这种情况的可能解决方案。

    MaxDate = ReportDate + 2
    MinDate = ReportDate - 6
    
    Set DatesEntryRange = Range("A2:A" & LastRowArray(2))
    
    DataRange.AutoFilter Field:=1, Criteria1:=Array( "A", "R", "S"), 
    Operator:=xlFilterValues
    DataRange.AutoFilter Field:=9, Criteria1:= "Company Name"
    DataRange.AutoFilter Field:=19, Criteria1:=Array( "Closed", "Completed", 
"Draft", "Implementation In Progress", "Pending", "Planning In Progress", "Request For Authorization", "Request For Change", "Scheduled", "Scheduled For Approval", "Scheduled For Review"), Operator:= xlFilterValues
    DataRange.AutoFilter Field:=15, Criteria1:= "=Emergency", Operator:=xlOr, Criteria2:="=Expedited"
    
    For Each DateEntry In DatesEntryRange.SpecialCells(xlCellTypeVisible)
        If ErrorOccurance <> True Then
            DateEntry.Select
        Else
            ErrorOccurance = False
            GoTo NextCriteria1
        End If

    Select Case Cells(DateEntry.Row, "A").Value
    Case "R"
        If Cells(DateEntry.Row, "B").Value >= MinDate And Cells(DateEntry.Row, "B").Value < MaxDate Then
            Cells(DateEntry.Row, "M").Interior.Color = RGB(RED, GREEN, BLUE)
        ElseIf Cells(DateEntry.Row, "C").Value >= MinDate And Cells(DateEntry.Row, "C").Value < MaxDate Then
            Cells(DateEntry.Row, "M").Interior.Color = RGB(RED, GREEN, BLUE)
        ElseIf Cells(DateEntry.Row, "B").Value < MinDate And Cells(DateEntry.Row, "C").Value >= MaxDate Then
            Cells(DateEntry.Row, "M").Interior.Color = RGB(RED, GREEN, BLUE)
        Else
        End If
    Case "S"
        If Cells(DateEntry.Row, "D").Value >= MinDate And Cells(DateEntry.Row, "D").Value < MaxDate Then
            Cells(DateEntry.Row, "M").Interior.Color = RGB(RED, GREEN, BLUE)
        ElseIf Cells(DateEntry.Row, "E").Value >= MinDate And Cells(DateEntry.Row, "E").Value < MaxDate Then
            Cells(DateEntry.Row, "M").Interior.Color = RGB(RED, GREEN, BLUE)
        ElseIf Cells(DateEntry.Row, "D").Value < MinDate And Cells(DateEntry.Row, "E").Value >= MaxDate Then
            Cells(DateEntry.Row, "M").Interior.Color = RGB(RED, GREEN, BLUE)
        Else
        End If
    Case "A"
        If Cells(DateEntry.Row, "F").Value >= MinDate And Cells(DateEntry.Row, "F").Value < MaxDate Then
            Cells(DateEntry.Row, "M").Interior.Color = RGB(RED, GREEN, BLUE)
        ElseIf Cells(DateEntry.Row, "G").Value >= MinDate And Cells(DateEntry.Row, "G").Value < MaxDate Then
            Cells(DateEntry.Row, "M").Interior.Color = RGB(RED, GREEN, BLUE)
        ElseIf Cells(DateEntry.Row, "F").Value < MinDate And Cells(DateEntry.Row, "G").Value >= MaxDate Then
            Cells(DateEntry.Row, "M").Interior.Color = RGB(RED, GREEN, BLUE)
        Else
        End If
    Case Else
        GoTo NextCriteria1
   End Select
Next

reportdate 是一个日期变量,B 到 H 列都是日期。LastRowArray 是一个整数数组,用于保存我正在生成的每个报告的最后一个单元格行。希望这里有人可以帮助解释为什么会跳过。

编辑:根据评论进行了一些更改。Excel 无法找到数据或跳过数据的问题仍然存在。提前致谢。

标签: excelvbaloopsforeach

解决方案


推荐阅读