'自己设置一下单元格格式
Option Explicit
Sub test()
Dim arr, i, j, m, min, p, brr, cnt, t
arr = [a1].CurrentRegion.Offset(1)
brr = arr: m = 1
brr(m, 1) = arr(UBound(arr, 1) - 1, 1)
brr(m, 2) = arr(UBound(arr, 1) - 1, 2)
min = arr(UBound(arr, 1) - 1, 2)
For i = UBound(arr, 1) - 2 To 1 Step -1
If min - arr(i, 2) > 0 Then
m = m + 1: min = arr(i, 2)
brr(m, 1) = arr(i, 1): brr(m, 2) = arr(i, 2)
Else
For j = i To 1 Step -1
If min - arr(j, 2) > 0 Then i = j + 1: Exit For
Next
End If
Next
If m = 0 Then MsgBox "!": Exit Sub
For i = 1 To m / 2
t = brr(i, 1): brr(i, 1) = brr(m - i + 1, 1): brr(m - i + 1, 1) = t
t = brr(i, 2): brr(i, 2) = brr(m - i + 1, 2): brr(m - i + 1, 2) = t
Next
arr = Range("k2:n" & Cells(Rows.Count, "k").End(xlUp).Row)
p = 1
For i = 1 To UBound(arr, 1)
For j = p To m
If arr(i, 1) <= brr(j, 2) Then
If arr(i, 1) = brr(j, 2) Then
arr(i, 2) = brr(j, 2): arr(i, 3) = brr(j, 1)
Else
arr(i, 2) = brr(j - 1, 2): arr(i, 3) = brr(j - 1, 1)
End If
p = j: cnt = cnt + 1: Exit For
End If
Next j, i
If cnt = 0 Then MsgBox "!!": Exit Sub
arr(1, 4) = vbNullString
For i = 2 To cnt
arr(i, 4) = (arr(i, 2) - arr(i - 1, 2)) / (arr(i, 3) - arr(i - 1, 3)) / 24 / 60
Next
[d2].Resize(m, 2) = brr
[k2].Resize(UBound(arr, 1), UBound(arr, 2)) = arr
End Sub |