|
本帖最后由 ykcbf1100 于 2024-2-20 16:54 编辑
代码供参考。。。- Sub ykcbf() '//2024.2.20
- Dim arr, d
- Application.ScreenUpdating = False
- Set d = CreateObject("Scripting.Dictionary")
- With Sheets("日期展开")
- r = .Cells(.Rows.Count, "a").End(xlUp).Row
- arr = .[a1].Resize(r, 3)
- End With
- On Error Resume Next
- ReDim brr(1 To UBound(arr), 1 To 5)
- For i = 2 To UBound(arr)
- s = arr(i, 2) & "|" & arr(i, 1)
- rq = Format(arr(i, 3), "yyyy-m-d")
- If Not d.exists(s) Then
- d(s) = rq
- Else
- d(s) = IIf(InStr(d(s), rq), d(s), d(s) & "、" & rq)
- End If
- Next
- For Each k In d.keys
- m = m + 1
- brr(m, 1) = Split(k, "|")(0)
- brr(m, 4) = Split(k, "|")(1)
- t = Split(d(k), "、")
- st = t(0)
- For x = 1 To UBound(t)
- If CLng(DateValue(t(x))) - CLng(DateValue(t(x - 1))) = 1 Then
- If x = t(UBound(t)) Or CLng(DateValue(t(x + 1))) - CLng(DateValue(t(x))) <> 1 Then
- st = st & "至" & Format(t(x), "yyyy-m-d")
- End If
- Else
- st = st & "、" & Format(t(x), "yyyy-m-d")
- End If
- Next
- brr(m, 5) = st
- Next
- With Sheets("日期汇总")
- .UsedRange.Offset(1).Clear
- .Columns(1).NumberFormatLocal = "@"
- With .[a2].Resize(m, 5)
- .Value = brr
- .Borders.LineStyle = 1
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- .EntireColumn.AutoFit
- End With
- End With
- Set d = Nothing
- Application.ScreenUpdating = True
- MsgBox "OK!"
- End Sub
复制代码
|
|