VBA宏来计算分钟

时间:2018-08-14 19:04:19

标签: excel vba excel-vba

我想知道通过VBA完成我要解释的任务是否可行。 基本上我想做的是,通过“ DUMMY2”,一旦值是“ 27”,我想减去该范围内“ DATETIME”的开始和结束以获取分钟计数

例如:05/16/2018 11:05-05/16/2018 10:50->可获得15分钟。 然后继续遍历该列表,跳过“ 16”,“ 9”,“ 4”并执行直到它到达第二批“ 27”并再次通过宏以计算分钟数。

非常感谢您!

Sub Test()

Cells.Find("DUMMY2").Offset(1, 0).Select
Do Until ActiveCell.Value = ""


Do While ActiveCell.Value > 26.8 And ActiveCell.Value < 27.1
First = ActiveCell.Offset(0, -2)
'maybe that doesn't make sense, but I'm trying to select the cells adjacent to "27"
ActiveCell.Offset(0, -2).Select
Loop
ActiveCell.Offset(1, 0).Select

Loop
End Sub

数据:

5/16/2018 10:35 -0.03   0
5/16/2018 10:40 -0.04   0
5/16/2018 10:45 -0.04   12
5/16/2018 10:50 -0.32   27
5/16/2018 10:55 -0.27   27
5/16/2018 11:00 -0.23   27
5/16/2018 11:05 -0.21   27
5/16/2018 11:10 -0.14   16
5/16/2018 11:15 -0.01   9
5/16/2018 11:20 -0.02   4
5/16/2018 11:25 -0.32   27
5/16/2018 11:30 -0.31   27
5/16/2018 11:35 -0.30   27
5/16/2018 11:40 -0.29   27

enter image description here

1 个答案:

答案 0 :(得分:2)

这是可行的方法。您可以根据需要将硬编码的列引用更新为命名范围

VARIABLE x refcursor;
DECLARE BEGIN

    IF
        pers_api.is_manager_(10018) = 1
    THEN
        OPEN :x FOR SELECT some_columns FROM some_tables;
    ELSE
        OPEN :x FOR SELECT some_columns FROM other_tables;
    END IF;
   --DBMS_SQL.RETURN_RESULT(:x); --12c and above
END;
/

PRINT x -- 11g

基本上,满足您需求的代码行是Sub cntDuration() Dim lRow As Long Dim intFlag As Integer Dim firstDate As Date, secondDate As Date Dim difTime As Long 'Find the last non-blank cell in column A(1) lRow = Cells(Rows.Count, 1).End(xlUp).Row intFlag = 0 For i = 2 To lRow If Sheets("Sheet1").Range("C" & i).Value > 26.8 And Sheets("Sheet1").Range("C" & i).Value < 27.1 Then If intFlag = 0 Then firstDate = Sheets("Sheet1").Range("A" & i).Value intFlag = 1 Else If Sheets("Sheet1").Range("C" & i + 1).Value > 26.8 And Sheets("Sheet1").Range("C" & i + 1).Value < 27.1 Then Else secondDate = Sheets("Sheet1").Range("A" & i).Value difTime = DateDiff("n", firstDate, secondDate) Sheets("Sheet1").Range("D" & i).Value = difTime intFlag = 0 End If End If End If Next i End Sub