繁体   English   中英

如何在VBA中编写此单元格引用的Excel公式以在MS Access中使用:(b2-a1)

[英]How can I write this cell-referenced Excel formula in VBA to use in MS Access: (b2-a1)

这是在Excel中执行的非常简单的计算,但是我对如何使用不同记录的表中的2个字段在Access中执行此操作感到困惑。 我需要一个应用程序来查找操作STOPTime和下一个STARTTime之间的差异。 任何帮助将不胜感激。 我是VBA的新手。

Sub samPle()
Dim startTime As Date
Dim endTime As Date

startTime = Time
'''Your Code


endTime = Time

Debug.Print DateDiff("n", startTime, endTime) ' difference in minutes

End Sub

有多种方法可以根据您的数据来解决问题,这使得发布有关SQL问题的数据非常重要。 例如:

表:

ID  EventTime           EventType
1   17/02/2013 08:52:00 start
2   17/02/2013 09:52:00 stop
3   17/02/2013 11:52:00 start
4   17/02/2013 15:52:00 stop

SQL:

SELECT a.id,
       a.eventtime                    AS Start,
       a.eventtype,
       (SELECT TOP 1 eventtime
        FROM   times b
        WHERE  eventtype = "stop"
               AND eventtime > a.eventtime
        ORDER  BY eventtime,
                  id)                 AS Stop,
       Datediff("n", [start], [stop]) AS RunTime
FROM   times AS a
WHERE  (( ( a.eventtype ) = "start" ))
ORDER  BY a.eventtime;

结果:

ID  Start               EventType   Stop                RunTime
1   17/02/2013 08:52:00 start       17/02/2013 09:52:00     60
3   17/02/2013 11:52:00 start       17/02/2013 15:52:00     240

重新评论

SELECT a.id, 
       a.eventtime                         AS Start, 
       a.eventtype, 
       (SELECT TOP 1 eventtime 
        FROM   times b 
        WHERE  eventtype = "stop" 
               AND eventtime > a.eventtime 
        ORDER  BY eventtime, 
                  id)                      AS Stop, 
       Datediff("n", [start], [stop])      AS RunTime, 
       (SELECT TOP 1 eventtime 
        FROM   times b 
        WHERE  eventtype = "start" 
               AND eventtime > a.eventtime 
        ORDER  BY eventtime, 
                  id)                      AS NextStart, 
       Datediff("n", [start], [nextstart]) AS StartDiff, 
       Datediff("n", [stop], [nextstart])  AS DownTime 
FROM   times AS a 
WHERE  (( ( a.eventtype ) = "start" )) 
ORDER  BY a.eventtime; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM