我有一個非常大的表格,其中包含文章的輸入和輸出時間。我想把回圈時間放在一個圖形中。

紅框內是我文章的序號。黃色框是開始時間,綠色框是結束時間。并非總是有 3 個相同的序列號,但也可以完全不同(例如 6 個相同的序列號)。
要顯示周期時間,我需要黃色框中的最早開始時間(分鐘)和綠色框中的最大值作為一個序列號的結束時間(最大值)。
這是無法手動完成的,因為作業表太大。有人對實施有想法嗎?問題不是從黃色框中找到最小值,從綠色框中找到最大值,而是在相同序列號之后的列中搜索的公式。
uj5u.com熱心網友回復:
請測驗下一個解決方案/代碼。由于您沒有回答澄清問題,因此假定顯示的列是“A:D”,而“B:D”列中的時間格式為Date( "hh:mm:ss")。該代碼使用字典來保存唯一的序列號并計算/存盤第二列的最小值和第四列的最大值。序列號可以按任何順序出現:
Sub ExtractMinMaxDates()
Dim sh As Worksheet, lastR As Long, i As Long, arr, arrEl, arrFin
Dim key As Variant, dict As Object
Set sh = ActiveSheet 'use here the necessary sheet
lastR = sh.Range("A" & sh.rows.count).End(xlUp).row 'last row
arr = sh.Range("A2:D" & lastR).Value2 'place the range in an array for faster iteration/processing
Set dict = CreateObject("Scripting.Dictionary") 'set the necessary dictionary
For i = 1 To UBound(arr) 'iterate between the array rows
If Not dict.Exists(arr(i, 1)) Then 'if a dictionary key has not been created:
dict.Add arr(i, 1), Array(arr(i, 2), arr(i, 4)) 'make it and place (as item) an array containing
Else 'time from columns 2 and 4
arrEl = dict(arr(i, 1)) 'place the item in an array
If arr(i, 2) < arrEl(0) Then arrEl(0) = arr(i, 2) 'keep the minimum
If arr(i, 4) > arrEl(1) Then arrEl(1) = arr(i, 4) 'keep the maximum
End If
Next i
'Redim the final array to keep the dictionary data:
ReDim arrFin(1 To dict.count, 1 To 3): i = 1
'fill the final aray with the necesssary data
For Each key In dict.Keys
arrFin(i, 1) = key: arrFin(i, 2) = dict(key)(0)
arrFin(i, 3) = dict(key)(1): i = i 1
Next
'Drop the array content at once anf format the necessary columns:
With sh.Range("F2").Resize(dict.count, 3) 'it may return anywhere (just change "F2" with needed cell)
.Value2 = arrFin
.Columns(2).EntireColumn.NumberFormat = "hh:mm:ss"
.Columns(3).EntireColumn.NumberFormat = "hh:mm:ss"
End With
End Sub
請在測驗后發送一些反饋。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/530986.html
標籤:擅长vba
