|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub test1()
- Dim ar(), br(), cr, Dict As Object
- Dim i As Long, j As Integer, n As Long, sKey As String
- Dim r As Long, c As Integer, p As Long, posRow As Long
-
- Worksheets("全年统计").Activate
- Cells.Clear
- Application.ScreenUpdating = False
-
- posRow = 1
- Set Dict = CreateObject("Scripting.Dictionary")
- cr = Split("月份天数 最高气温 最低气温 晴天日 雨天日")
- ReDim br(1 To 34, 1 To (UBound(cr) + 1) * 2)
- For j = 0 To UBound(cr)
- br(2, j * 2 + 1) = cr(j)
- br(3, j * 2 + 1) = Split("最高气温℃ 最低气温℃ 天气 风向 风力")(j)
- br(3, (j + 1) * 2) = "天数"
- Next
- ReDim ar(2 To Worksheets.Count)
- For n = LBound(ar) To UBound(ar)
- ar(n) = br
- With Worksheets(n)
- ar(n)(1, 1) = "2022年" & .Name & "份天气情况统计表"
- With .Range("F2", .Cells(.Rows.Count, "A").End(xlUp))
- cr = .Value
- ar(n)(2, 4) = WorksheetFunction.Max(.Columns(2))
- ar(n)(2, 6) = WorksheetFunction.Min(.Columns(3))
- ar(n)(2, 8) = WorksheetFunction.CountIf(.Columns(4), "晴")
- ar(n)(2, 10) = WorksheetFunction.CountIf(.Columns(4), "*雨*")
- End With
- End With
- For j = 2 To UBound(cr, 2)
- r = 3
- c = (j - 1) * 2
- Dict.RemoveAll
- For i = 1 To UBound(cr)
- If IsDate(cr(i, 1)) Then
- sKey = Trim(cr(i, j))
- If Dict.Exists(sKey) Then
- p = Dict(sKey)
- ar(n)(p, c) = ar(n)(p, c) + 1
- Else
- r = r + 1
- ar(n)(r, c - 1) = sKey
- ar(n)(r, c) = 1
- Dict.Add sKey, r
- End If
- If j = 2 Then ar(n)(2, 2) = ar(n)(2, 2) + 1
- End If
- Next
- Next
- With Range("A" & posRow)
- .Resize(UBound(ar(n)), UBound(ar(n), 2)) = ar(n)
- With .CurrentRegion
- Intersect(.Offset(0), .Offset(1)).Borders.LineStyle = xlContinuous
- .HorizontalAlignment = xlCenter
- End With
- .Font.Size = 14
- .Font.Bold = True
- .Resize(, UBound(ar(n), 2)).HorizontalAlignment = 7
- End With
- posRow = Range("A1").Resize(Rows.Count, UBound(ar(n), 2)).Find("*", , , , 1, 2).Row + 2
- Next
- Set Dict = Nothing
-
- Application.ScreenUpdating = True
- Beep
- End Sub
复制代码 |
|