我希望发表IF Then声明。单元格A2和A3中都有日期。我希望我的IF Then语句为A3的分钟减去A2的分钟等于1,然后执行我的命令。我还想执行ElseIf Then语句,以了解分钟差是否为5分钟以及分钟差是否为15分钟。
我尝试在线查找语法,并在下面复制了代码,但是它不起作用。该代码读取到,如果单元格A3的分钟值减去单元格A2的分钟值等于1,则执行该功能。但是它不起作用。
Sub ExportAverageData()
If (Minute(A3 - A2) = 1) Then
Sheets(2).Select
Range("E2").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(OFFSET(R2C2,(ROW()-ROW(R2C5))*15,,15,))"
Selection.AutoFill Destination:=Range("E2:E9999")
Range("F2").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(OFFSET(R2C3,(ROW()-ROW(R2C6))*15,,15,))"
Selection.AutoFill Destination:=Range("F2:F9999")
ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas, 16).ClearContents
ElseIf (Minute(A3 - A2) = 5) Then
Sheets(2).Select
Range("E2").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(OFFSET(R2C2,(ROW()-ROW(R2C5))*3,,3,))"
Selection.AutoFill Destination:=Range("E2:E9999")
Range("F2").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(OFFSET(R2C3,(ROW()-ROW(R2C6))*15,,15,))"
Selection.AutoFill Destination:=Range("F2:F9999")
ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas, 16).ClearContents
ElseIf (Minute(A3 - A2) = 15) Then
Sheets(2).Select
Range("E2").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(OFFSET(R2C2,(ROW()-ROW(R2C5))*15,,15,))"
Selection.AutoFill Destination:=Range("E2:E9999")
Range("F2").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(OFFSET(R2C3,(ROW()-ROW(R2C6))*15,,15,))"
Selection.AutoFill Destination:=Range("F2:F9999")
ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas, 16).ClearContents
End If
End Sub
答案 0 :(得分:0)
也许这会有所帮助,我假设您要引用单元格A2和A3内的值,在这种情况下,您还需要包括Range()
和.Value
标签。
If (Minute(Range("A2").Value) - Minute(Range("A3").Value) = 1) Then
'Do something
Else If (Minute(Range("A2").Value) - Minute(Range("A3").Value) = 5) Then
'Do something else
Else If (Minute(Range("A2").Value) - Minute(Range("A3").Value) = 15) Then
'Do something else
End If