1 Sub count_date() 2 '计算所有天数,并对日期去重 3 Dim dic As Object 4 Dim arr() As Variant 5 Dim d As Date 6 Dim cols As Integer 7 8 '选5列:1条件列,2开始日期,3结束日期,4,第一列出现次数,5累计天数 9 arr = Selection 10 cols = UBound(arr, 2) 11 Debug.Print cols 12 For i = 1 To UBound(arr, 1) '大循环 13 Set dic = CreateObject("scripting.dictionary") 14 n = 0 15 For j = 1 To UBound(arr, 1) '小循环 16 17 If arr(j, 1) = arr(i, 1) Then '找到同名行 18 For d = arr(j, 2) To arr(j, 3) '累计日期 19 '字典的键名不能重复,使得d重复输入无效,起到去重日期的效果。 20 dic(d) = d 21 Next 22 n = n + 1 23 End If 24 '如果没有选第四列,删除此if和n 25 If n > arr(i, 4) Then 26 Exit For 27 End If 28 Next 29 arr(i, cols) = dic.Count 30 Next 31 'WorksheetFunction.Index(数组, 行数, 列数),"0"代表整行或整列 32 Selection.Columns(cols) = WorksheetFunction.Index(arr, 0, cols) 33 End Sub
标签:Dim,VBA,End,arr,累计,cols,日期,dic From: https://www.cnblogs.com/yjyblog/p/16997511.html