貼吧提問《求助 按周期在日期數(shù)據(jù)里提取最大值最小值穷当,以及周一周天的數(shù)據(jù)》额港,使用字典嵌套數(shù)組可以很方便的獲取數(shù)據(jù)
注意:對(duì)于字典嵌套的數(shù)組扛施,不可以像python一樣直接對(duì)數(shù)組元素進(jìn)行賦值修改漓穿,而應(yīng)該對(duì)整個(gè)數(shù)組重新賦值
如第12行代碼如果是 If Weekday(rq) = 2 Then dict(nz)(2) = sj 則會(huì)報(bào)錯(cuò)
Sub 按日期匯總數(shù)據(jù)()
'字典嵌套數(shù)組枷餐,鍵為“**年**周”坷衍,值為Array(最大值, 最小值, 周一值, 周五值)
Dim arr, i, rq, nz, sj, k, v, x, dict As Object
Set dict = CreateObject("scripting.dictionary")
arr = [a1].CurrentRegion
For i = 2 To UBound(arr):
rq = arr(i, 1) '日期
nz = Year(rq) & "年" & WorksheetFunction.WeekNum(rq) & "周" '年-周寝优,key
sj = arr(i, 2) '數(shù)據(jù)
If Not dict.Exists(nz) Then '新鍵-值
dict(nz) = Array(sj, sj, "", "")
If Weekday(rq) = 2 Then dict(nz) = Array(dict(nz)(0), dict(nz)(1), sj, dict(nz)(3)) '周一值
If Weekday(rq) = 6 Then dict(nz) = Array(dict(nz)(0), dict(nz)(1), dict(nz)(2), sj) '周五值
Else '已有鍵-值,更新
If sj > dict(nz)(0) Then dict(nz) = Array(sj, dict(nz)(1), dict(nz)(2), dict(nz)(3)) '最大值
If sj < dict(nz)(1) Then dict(nz) = Array(dict(nz)(0), sj, dict(nz)(2), dict(nz)(3)) '最小值
If Weekday(rq) = 2 Then dict(nz) = Array(dict(nz)(0), dict(nz)(1), sj, dict(nz)(3)) '周一值
If Weekday(rq) = 6 Then dict(nz) = Array(dict(nz)(0), dict(nz)(1), dict(nz)(2), sj) '周五值
End If
Next
k = dict.keys
v = dict.Items
For x = 0 To dict.count - 1: '遍歷字典
row_write = [f1].CurrentRegion.Rows.count + 1 '輸出結(jié)果區(qū)域的第一個(gè)空行寫入
Cells(row_write, 6).Resize(1, 1) = k(x)
Cells(row_write, 7).Resize(1, 4) = v(x)
Next
End Sub
舉例:
按日期匯總數(shù)據(jù)-舉例