VBA Code to display calculated data every second column with count in between

BigMillBoy

New Member
Joined
Sep 6, 2021
Messages
5
Office Version
  1. 2013
Platform
  1. 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
Test code.xlsm
ABC
1DatePARTDUR
21A200
31B15
41C150
51A125
61B175
71C300
81A20
91B120
102A50
112C400
122B60
132B300
142C130
153A55
163A370
173A20
183B15
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
ABCD
1PART123
2A34550445
3B31036015
4C4505300
Cumlative_A
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Welcome to MrExcel Message Board.
How about Formula Method:
Book1
ABCDEFGHIJ
1DatePARTDURSum
21A200Unique Part123  
31B15A34550445
41C150B31036015
51A125C4505300
61B175 
71C300 
81A20Counting
91B120Unique Part123 
102A50A313
112C400B321
122B60C220
132B300 
142C130
153A55
163A370
173A20
183B15
19
20
21
Sheet1
Cell Formulas
RangeFormula
G9:I9,F2:J2F2=IFERROR(INDEX($A$2:$A$18, MATCH(0,INDEX(COUNTIF($E$2:E2, $A$2:$A$18),0,0), 0)), "")
F3:H5F3=SUMIFS($C$2:$C$18,$A$2:$A$18,F$2,$B$2:$B$18,$E3)
E3:E7E3=IFERROR(INDEX($B$2:$B$18, MATCH(0,INDEX(COUNTIF($E$2:E2, $B$2:$B$18),0,0), 0)), "")
F9F9=IFERROR(INDEX($A$2:$A$18, MATCH(0,INDEX(COUNTIF($E$2:E2, $A$2:$A$18),0,0), 0)), "")
F10:H12F10=COUNTIFS($A$2:$A$18,F$2,$B$2:$B$18,$E10)
E10:E13E10=IFERROR(INDEX($B$2:$B$18, MATCH(0,INDEX(COUNTIF($E$9:E9, $B$2:$B$18),0,0), 0)), "")
 
Last edited:
Upvote 0
Or all at One Table:
Book1
ABCDEFGHIJKL
1DatePARTDURSumCountSumCountSumCount
21A200Unique Part112233
31B15A34535014453
41C150B31033602151
51A125C4502530200
61B175 
71C300 
81A20
91B120
102A50
112C400
122B60
132B300
142C130
153A55
163A370
173A20
183B15
19
20
Sheet1
Cell Formulas
RangeFormula
F2:K2F2=IF(MOD(COLUMN(),2)=0,IFERROR(INDEX($A$2:$A$18, MATCH(0,INDEX(COUNTIF($E$2:E2, $A$2:$A$18),0,0), 0)), ""),E2)
F3:K5F3=IF(MOD(COLUMN(),2)=0,SUMIFS($C$2:$C$18,$A$2:$A$18,F$2,$B$2:$B$18,$E3),COUNTIFS($A$2:$A$18,F$2,$B$2:$B$18,$E3))
E3:E7E3=IFERROR(INDEX($B$2:$B$18, MATCH(0,INDEX(COUNTIF($E$2:E2, $B$2:$B$18),0,0), 0)), "")
 
Last edited:
Upvote 0
Or all at One Table:
Book1
ABCDEFGHIJKL
1DatePARTDURSumCountSumCountSumCount
21A200Unique Part112233
31B15A34535014453
41C150B31033602151
51A125C4502530200
61B175 
71C300 
81A20
91B120
102A50
112C400
122B60
132B300
142C130
153A55
163A370
173A20
183B15
19
20
Sheet1
Cell Formulas
RangeFormula
F2:K2F2=IF(MOD(COLUMN(),2)=0,IFERROR(INDEX($A$2:$A$18, MATCH(0,INDEX(COUNTIF($E$2:E2, $A$2:$A$18),0,0), 0)), ""),E2)
F3:K5F3=IF(MOD(COLUMN(),2)=0,SUMIFS($C$2:$C$18,$A$2:$A$18,F$2,$B$2:$B$18,$E3),COUNTIFS($A$2:$A$18,F$2,$B$2:$B$18,$E3))
E3:E7E3=IFERROR(INDEX($B$2:$B$18, MATCH(0,INDEX(COUNTIF($E$2:E2, $B$2:$B$18),0,0), 0)), "")
Sorry
Or all at One Table:
Book1
ABCDEFGHIJKL
1DatePARTDURSumCountSumCountSumCount
21A200Unique Part112233
31B15A34535014453
41C150B31033602151
51A125C4502530200
61B175 
71C300 
81A20
91B120
102A50
112C400
122B60
132B300
142C130
153A55
163A370
173A20
183B15
19
20
Sheet1
Cell Formulas
RangeFormula
F2:K2F2=IF(MOD(COLUMN(),2)=0,IFERROR(INDEX($A$2:$A$18, MATCH(0,INDEX(COUNTIF($E$2:E2, $A$2:$A$18),0,0), 0)), ""),E2)
F3:K5F3=IF(MOD(COLUMN(),2)=0,SUMIFS($C$2:$C$18,$A$2:$A$18,F$2,$B$2:$B$18,$E3),COUNTIFS($A$2:$A$18,F$2,$B$2:$B$18,$E3))
E3:E7E3=IFERROR(INDEX($B$2:$B$18, MATCH(0,INDEX(COUNTIF($E$2:E2, $B$2:$B$18),0,0), 0)), "")

I am looking to use vba code as it will need to be automated each week to a button. So simply adding formulas to each cell will not accomplish the task I have been set. Thanks though
 
Upvote 0
Try this:
VBA Code:
Sub ArrahngeData()
Dim i As Long, Lr As Long, Sh1 As Worksheet, Sh2 As Worksheet, Ar As Variant, Ar2 As Variant
Dim j As Long, Lr2 As Long
Set Sh1 = Sheets("A_Stops")
Set Sh2 = Sheets("Cumlative_A")
Sh2.Cells.ClearContents
Lr = Sh1.Range("A" & Rows.Count).End(xlUp).Row
Ar = Sh1.Range("A2:A" & Lr)
 With CreateObject("Scripting.Dictionary")
        For i = LBound(Ar) To UBound(Ar)
            If Not IsMissing(Ar(i, 1)) Then .Item(Ar(i, 1)) = 1
        Next
        Ar = .Keys
End With
 For i = LBound(Ar) To UBound(Ar)
   Sh2.Cells(1, 2 * i + 2).Resize(, 2).Value = Array("Sum", "Count")
   Sh2.Cells(2, 2 * i + 2).Resize(, 2).Value = Ar(i)
 Next i
    Sh2.Range("A2:A" & Lr).Value = Sh1.Range("B1:B" & Lr).Value
    Sh2.Range("A3:B" & Lr).RemoveDuplicates Columns:=Array(1, 2), Header:=xlNo
    Lr2 = Sh2.Cells(Rows.Count, 1).End(xlUp).Row
 For i = 3 To Lr2
    For j = 2 To 2 * UBound(Ar) + 3
    If j Mod 2 = 0 Then
    Cells(i, j).Value = Application.WorksheetFunction.SumIfs(Sh1.Range("C:C"), Sh1.Range("A:A"), _
    Sh2.Cells(2, j), Sh1.Range("B:B"), Sh2.Range("A" & i))
    Else
    Cells(i, j).Value = Application.WorksheetFunction.CountIfs(Sh1.Range("A:A"), Sh2.Cells(2, j), _
    Sh1.Range("B:B"), Sh2.Range("A" & i))
    End If
    Next j
 Next i
End Sub
 
Upvote 0
Try this:
VBA Code:
Sub ArrahngeData()
Dim i As Long, Lr As Long, Sh1 As Worksheet, Sh2 As Worksheet, Ar As Variant, Ar2 As Variant
Dim j As Long, Lr2 As Long
Set Sh1 = Sheets("A_Stops")
Set Sh2 = Sheets("Cumlative_A")
Sh2.Cells.ClearContents
Lr = Sh1.Range("A" & Rows.Count).End(xlUp).Row
Ar = Sh1.Range("A2:A" & Lr)
 With CreateObject("Scripting.Dictionary")
        For i = LBound(Ar) To UBound(Ar)
            If Not IsMissing(Ar(i, 1)) Then .Item(Ar(i, 1)) = 1
        Next
        Ar = .Keys
End With
 For i = LBound(Ar) To UBound(Ar)
   Sh2.Cells(1, 2 * i + 2).Resize(, 2).Value = Array("Sum", "Count")
   Sh2.Cells(2, 2 * i + 2).Resize(, 2).Value = Ar(i)
 Next i
    Sh2.Range("A2:A" & Lr).Value = Sh1.Range("B1:B" & Lr).Value
    Sh2.Range("A3:B" & Lr).RemoveDuplicates Columns:=Array(1, 2), Header:=xlNo
    Lr2 = Sh2.Cells(Rows.Count, 1).End(xlUp).Row
 For i = 3 To Lr2
    For j = 2 To 2 * UBound(Ar) + 3
    If j Mod 2 = 0 Then
    Cells(i, j).Value = Application.WorksheetFunction.SumIfs(Sh1.Range("C:C"), Sh1.Range("A:A"), _
    Sh2.Cells(2, j), Sh1.Range("B:B"), Sh2.Range("A" & i))
    Else
    Cells(i, j).Value = Application.WorksheetFunction.CountIfs(Sh1.Range("A:A"), Sh2.Cells(2, j), _
    Sh1.Range("B:B"), Sh2.Range("A" & i))
    End If
    Next j
 Next i
End Sub
This works thanks :)
The only issue is that it places the week#, "count" & "sum" and part number in worksheet cumlative_A but, it places the actual sum of durationan and count into whatever worksheet you are currently in. How would I fix this?
 
Upvote 0
The only issue is that it places the week#, "count" & "sum" and part number in worksheet cumlative_A but, it places the actual sum of durationan and count into whatever worksheet you are currently in. How would I fix this?
Are You want Data and Result at same Sheet?
if Yes, You want Result at where? (Which Sheet & Columns)?
 
Upvote 0

Forum statistics

Threads
1,223,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top