如何使用 IF Then 语句计算单元格减去单元格的分钟等于?
How to use IF Then statements for minute of cell minus cell is equal to?
我想做一个 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
也许这会有所帮助,我假设您想要引用单元格 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
我想做一个 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
也许这会有所帮助,我假设您想要引用单元格 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