我有1 1 1(1月1日的第一個小時)到12 31 24(12月31日的最後一個小時)的數據。我試着每天總結使用情況(與每天相關的24小時),然後找出一年中所有日子的最大消耗量。試圖使使用VBAExcel&VBA:將小時信息總和爲天,然後找到最高日值
1 1 1 0.011
其中A欄是月在Excel宏,B欄是一天,列C是小時和列d是使用
編輯:然後,如果可能的話,指示哪個日期有用法嗎?
我有1 1 1(1月1日的第一個小時)到12 31 24(12月31日的最後一個小時)的數據。我試着每天總結使用情況(與每天相關的24小時),然後找出一年中所有日子的最大消耗量。試圖使使用VBAExcel&VBA:將小時信息總和爲天,然後找到最高日值
1 1 1 0.011
其中A欄是月在Excel宏,B欄是一天,列C是小時和列d是使用
編輯:然後,如果可能的話,指示哪個日期有用法嗎?
這裏是我的解決方案:這是假設你的第一行標題,數據開始第2行。
子getUsage()
Dim lim As Integer
Dim count As Integer
Dim total As Double
Dim rVal As range
total = 0
Final = 0
lim = Sheet1.range("B2", Sheet1.range("B2").End(xlDown)).Rows.count
For count = 0 To lim
total = total + Sheet1.range("B2").Offset(count, 2).Value
If Sheet1.range("B2").Offset(count, 1) = 1 Then
If total > Final Then
Final = total
Set rVal = Sheet1.range("B2").Offset(count, 0)
End If
total = 0
End If
Next
MsgBox ("The date with the largest usage is: " & rVal.Offset(0, -1) & ", " & rVal.Offset(0, 0) & ", " & rVal.Offset(0, 1))
末次
這個用戶自定義Function¹過程您的數據塊在內存中,並返回每日最大使用情況以及發生日期。
Function fcnDatedMax(rng As Range, Optional yr As Integer = 2015)
Dim u As Long, vUSEs As Variant, dbl As Double
With rng
vUSEs = .Resize(.Rows.Count + 1, .Columns.Count).Value2
For u = 1 To 4
vUSEs(UBound(vUSEs, 1), u) = vUSEs(LBound(vUSEs, 1), u)
Next u
dbl = vUSEs(UBound(vUSEs, 1), UBound(vUSEs, 2))
End With
For u = LBound(vUSEs, 1) + 1 To UBound(vUSEs, 1) - 1
If vUSEs(u, 2) <> vUSEs(u - 1, 2) Then
If dbl > vUSEs(UBound(vUSEs, 1), UBound(vUSEs, 2)) Then
vUSEs(UBound(vUSEs, 1), 1) = vUSEs(u - 1, 1)
vUSEs(UBound(vUSEs, 1), 2) = vUSEs(u - 1, 2)
vUSEs(UBound(vUSEs, 1), 3) = vUSEs(u - 1, 3)
vUSEs(UBound(vUSEs, 1), 4) = dbl
End If
dbl = vUSEs(u, UBound(vUSEs, 2))
Else
dbl = dbl + vUSEs(u, UBound(vUSEs, 2))
End If
Next u
fcnDatedMax = Format(DateSerial(yr, vUSEs(UBound(vUSEs, 1), 1), vUSEs(UBound(vUSEs, 1), 2)), "dd-mmm-yyyy ") & _
vUSEs(UBound(vUSEs, 1), UBound(vUSEs, 2))
Erase vUSEs
End Function
在G145和H145作爲,
=MAX(E:E)
=fcnDatedMax(A2:D2161)
通過這一年的第一季度跑,回到正確的每日最高,以及它發生的日期。
¹一個用戶定義函數(UDF又名)放入一個標準模塊代碼表。點擊Alt鍵 + F11和VBE打開時,立即使用下拉菜單插入►模塊(Alt鍵 + 我,中號)。將功能代碼粘貼到名爲Book1 - Module1(Code)的新模塊代碼表中。點擊Alt + 問題返回到您的工作表。
最後一小時應該是23,而不是24.午夜到凌晨01:00被認爲是0小時。 – Jeeped
它看起來像午夜從1小時開始 – mcando
您需要一個幫助列,在每天的最後一個小時添加每天的使用情況,然後獲取該列的MAX並使用INDEX查找日期。 – Jeeped