- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
回復 7# b9208
唉週次沒有弄好,更正如下- Option Explicit
- Dim D(1 To 2) As Object, 週次 As Object, Ar ' 'Dim : 此模組的私用變數(僅此模組可用)
- Sub EX()
- Dim i As Integer, ii As Integer, M As String, Rng As Range, 統計單位 As Variant
- Set D(1) = CreateObject("scripting.dictionary") '字典物件
- Set D(2) = CreateObject("scripting.dictionary")
- Set 週次 = CreateObject("scripting.dictionary")
- With Sheets("統計")
- i = Application.CountA(.[b4:b13])
- 統計單位 = Join(Application.Transpose(.Range(.[b4], .[b4].Offset(i - 1))), ",") '統計單位=QWE,ASD
- End With
- With Sheets("明細")
- i = 6
- Do While .Cells(i, "D") <> ""
- ' "," & 統計單位 & "," -> ,QWE,ASD,
- If InStr("," & 統計單位 & ",", "," & .Cells(i, "F") & ",") Then '比對到 ,QWE, ,ASD, .....
-
- If InStr("," & 週次(.Cells(i, "F").Value) & ",", "," & Mid(.Cells(i, "E"), 1, 4)) & "," = 0 Then '統計單位: 比對週次不存在, .....
- 週次(.Cells(i, "F").Value) = IIf(週次(.Cells(i, "F").Value) = "", "", 週次(.Cells(i, "F").Value) & ",") & Mid(.Cells(i, "E"), 1, 4)
- End If
-
- M = .Cells(i, "D") & Mid(.Cells(i, "E"), 1, 4) & .Cells(i, "F")
- D(1)(M) = D(1)(M) + 1 '全部
- M = .Cells(i, "D") & Mid(.Cells(i, "E"), 1, 4) & .Cells(i, "F") & .Cells(i, "L")
- D(2)(M) = D(2)(M) + 1 '區域
- End If
- i = i + 1
- Loop
- End With
- With Sheets("統計")
- .[F:IQ].Clear
- For i = 0 To Application.CountA(.Range("B4:B13")) - 1
- Ar = Array("全部", "單位", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "小計")
- If i = 0 Then
- Set Rng = .[F3]
- Else
- Set Rng = .Cells(.Rows.Count, "F").End(xlUp).Offset(6) '每張表格間隔五列
- End If
-
- 週次(.Range("B4").Offset(i).Value) = Split(週次(.Range("B4").Offset(i).Value), ",")
- '取得統計單位之週次
-
- 表格製造 Rng, .Range("B4").Offset(i)
- 表格統計 Rng.CurrentRegion
-
- For ii = 0 To Application.CountA(.Range("B18:B21")) - 1
- Set Rng = .Cells(.Rows.Count, "F").End(xlUp).Offset(6) '每張表格間隔五列
- Ar(0) = .[B18].Offset(ii)
- 表格製造 Rng, .Range("B4").Offset(i)
- 表格統計 Rng.CurrentRegion
- Next
- Next
- End With
- End Sub
- Private Sub 表格製造(Rng As Range, 單位 As String)
- Rng.Resize(UBound(Ar) + 1).Value = Application.Transpose(Ar)
- With Rng.Offset(, 1).Resize(1, UBound(週次(單位)) + 1)
- .Value = 週次(單位)
- .Offset(1) = 單位
- End With
- Rng.CurrentRegion.Borders.LineStyle = 1 '框線
- End Sub
- Private Sub 表格統計(Rng As Range)
- Dim R As Integer, C As Integer
- With Rng
- For R = 3 To .Rows.Count - 1
- For C = 2 To .Columns.Count
- If .Cells(1) = "全部" Then '全部
- .Cells(R, C) = D(1)(.Cells(R, 1) & Mid(.Cells(1, C), 1, 4) & .Cells(2, C))
- Else '區域
- .Cells(R, C) = D(2)(.Cells(R, 1) & Mid(.Cells(1, C), 1, 4) & .Cells(2, C) & .Cells(1))
- End If
- Next
- Next
- For C = 2 To .Columns.Count
- .Cells(.Rows.Count, C).FormulaR1C1 = "=SUM(R[-" & .Rows.Count - 3 & "]C:R[-1]C)" '公式
- .Cells(.Rows.Count, C) = .Cells(.Rows.Count, C).Value
- Next
- End With
- End Sub
複製代碼 |
|