BigMillBoy
New Member
- Joined
- Sep 6, 2021
- Messages
- 5
- Office Version
- 2013
- Platform
- Windows
Hi,
I am looking for a way to display the cumulative duration of stops for each week by machine part in the table below. I would like to display the cumulative time in every second column with the number of times the stop occurred in the one next to it. I have already have a script that displays the cumulative duration but not the number of times it occurred which is required (see below).
Sheet: A_Stops
My scrip:
Which gives this output:
Sheet: Cumlative_A
I am looking for a way to display the cumulative duration of stops for each week by machine part in the table below. I would like to display the cumulative time in every second column with the number of times the stop occurred in the one next to it. I have already have a script that displays the cumulative duration but not the number of times it occurred which is required (see below).
Sheet: A_Stops
Test code.xlsm | |||||
---|---|---|---|---|---|
A | B | C | |||
1 | Date | PART | DUR | ||
2 | 1 | A | 200 | ||
3 | 1 | B | 15 | ||
4 | 1 | C | 150 | ||
5 | 1 | A | 125 | ||
6 | 1 | B | 175 | ||
7 | 1 | C | 300 | ||
8 | 1 | A | 20 | ||
9 | 1 | B | 120 | ||
10 | 2 | A | 50 | ||
11 | 2 | C | 400 | ||
12 | 2 | B | 60 | ||
13 | 2 | B | 300 | ||
14 | 2 | C | 130 | ||
15 | 3 | A | 55 | ||
16 | 3 | A | 370 | ||
17 | 3 | A | 20 | ||
18 | 3 | B | 15 | ||
A_Stops |
My scrip:
VBA Code:
Sub CumStops()
Dim wsA As Worksheet: Set wsA = Worksheets("A_Stops")
Dim wsCA As Worksheet: Set wsCA = Worksheets("Cumlative_A")
Dim lRow As Long, x As Long, lRow2 As Long, i As Long, c As Long
Dim dts As Variant
wsCA.Cells.ClearContents
lRow = wsA.Cells(Rows.Count, 1).End(xlUp).Row
dts = wsA.Range("A2:A" & lRow)
With CreateObject("Scripting.Dictionary")
For x = LBound(dts) To UBound(dts)
If Not IsMissing(dts(x, 1)) Then .Item(dts(x, 1)) = 1
Next
dts = .Keys
End With
wsCA.Range("B1").Resize(, UBound(dts) + 1) = dts
wsA.Range("B1:B" & lRow).Copy wsCA.Range("A1")
wsCA.Range("A2:B" & lRow).RemoveDuplicates Columns:=Array(1, 2), _
Header:=xlNo
lRow2 = wsCA.Cells(Rows.Count, 1).End(xlUp).Row
For c = 2 To 2 + UBound(dts)
For i = 2 To lRow2
wsCA.Cells(i, c) = Application.WorksheetFunction.SumIfs _
(wsA.Range("C:C"), wsA.Range("A:A"), wsCA.Cells(1, c), _
wsA.Range("B:B"), wsCA.Range("A" & i))
Next
Next
End Sub
Which gives this output:
Sheet: Cumlative_A
Test code.xlsm | ||||||
---|---|---|---|---|---|---|
A | B | C | D | |||
1 | PART | 1 | 2 | 3 | ||
2 | A | 345 | 50 | 445 | ||
3 | B | 310 | 360 | 15 | ||
4 | C | 450 | 530 | 0 | ||
Cumlative_A |