【问题标题】:Excel VBA search range depending on time of dayExcel VBA搜索范围取决于一天中的时间
【发布时间】:2014-12-11 10:17:02
【问题描述】:

我有一列格式为“dd/mm/yy HH:MM:SS”的单元格,我想根据时间计算日期和时间在以下范围内的单元格的数量天。

如果当前时间

Count Occurances in range between 07:00 the previous day and now

否则

Count occurances between 07:00 today and now

如果结束

提前谢谢你。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    此代码很可能适合您。它也非常简单且易于更改以引用您需要查看的正确工作表和单元格。我希望这会有所帮助!

    Sub CountingNumberOfOccurancesWithingRangeDependingOnTime()
        Dim curTime As Date
        Dim preTime As Date
        Dim todayTime As Date
        Dim curHour As Integer
        Dim counter As Integer
    
        curTime = Now
        curHour = Format(curTime, "HH")
        preTime = curTime - 1
    
        x = 1
    
        If curHour < 7 Then
            Do Until Sheet1.Cells(x, 1).Value = Empty
                cellTime = Sheet1.Cells(x, 1).Value
    
                If cellTime > preTime And cellTime < curTime Then
                    counter = counter + 1
                End If
                x = x + 1
            Loop
        Else
            todayTime = Date & " " & "07:00:00"
            Do Until Sheet1.Cells(x, 1).Value = Empty
                cellTime = Sheet1.Cells(x, 1).Value
    
                If cellTime > todayTime And cellTime < curTime Then
                    counter = counter + 1
                End If
                x = x + 1
            Loop
        End If
    
        MsgBox counter 'This isn't necissary, but it just is here to show the number of cells counted
    
    
    
    End Sub
    

    【讨论】:

    • 太棒了!我能够对其进行调整以完全适合我的项目。非常感谢!
    猜你喜欢
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 2013-11-17
    • 1970-01-01
    相关资源
    最近更新 更多