VBA Sumif based on column headers

milanso

New Member
Joined
May 17, 2017
Messages
4
Hi,
I have two worksheets. The first contains the production plan with dates for column headers and products for rows.

Product7.2.20187.3.20187.4.20187.5.20187.6.20187.7.20187.8.20187.9.2018
a15020055000000
b502004006000000
c0060050050040000
d0008001000600250100

<tbody>
</tbody>

I needed to create a second worksheet based on the first one, but with shipping dates instead of production dates, based on criteria like,
if date 1 is tuesday, then it ships on friday, etc. Since we have two shipments per week, we will have several date headers repeating, like this:

Product7.5.20187.5.20187.5.20187.9.20187.9.20187.9.20187.9.20187.12.2018
a15020055000000
b502004006000000
c0060050050040000
d0008001000600250100

<tbody>
</tbody>

Ive managed to do all this, but I need the columns summed for the same dates, and I'm stuck.
This should be the final result:

Product7.5.20187.9.20187.12.2018
a90000
b6506000
c60014000
d02650100

<tbody>
</tbody>


Here's the code:

Code:
Option Explicit

Sub Plan()
Dim d As Object
Dim i As Range, n As Range, v As Range, h As Range
Dim wbO As Workbook
Dim wsPlan As Worksheet, wsShip As Worksheet
Dim lRowP As Long, lColP As Long, lRowS As Long, lColS As Long, lRowS2 As Long, lColS2 As Long, rowNrI As Long, colNrI As Long




Set wbO = ThisWorkbook
Set wsPlan = wbO.Sheets("plan")
Set wsShip = wbO.Sheets("shipments")


With Application
        .ScreenUpdating = False
        .EnableEvents = False
End With


lRowP = wsPlan.Cells(wsPlan.Rows.Count, 1).End(xlUp).Row
lColP = wsPlan.Cells(1, wsPlan.Columns.Count).End(xlToLeft).Column

'Calculates which shipment date it is and writes over new value

For Each i In wsPlan.Range(wsPlan.Cells(1, 2), wsPlan.Cells(1, lColP))
    If Weekday(i.Value) = 1 Then
        i = DateAdd("d", 3, i.Value)
    ElseIf Weekday(i.Value) = 2 Then
        i = DateAdd("d", 2, i.Value)
    ElseIf Weekday(i.Value) = 3 Then
        i = DateAdd("d", 6, i.Value)
    ElseIf Weekday(i.Value) = 4 Then
        i = DateAdd("d", 5, i.Value)
    ElseIf Weekday(i.Value) = 5 Then
        i = DateAdd("d", 4, i.Value)
    ElseIf Weekday(i.Value) = 6 Then
        i = DateAdd("d", 5, i.Value)
    ElseIf Weekday(i.Value) = 7 Then
        i = DateAdd("d", 4, i.Value)
    End If
Next i

'Clears Shipment sheet

If wsShip.Range("A1").Value <> 0 Then
    lRowI = wsShip.Cells(wsShip.Rows.Count, 1).End(xlUp).Row
    lColI = wsShip.Cells(1, wsShip.Columns.Count).End(xlToLeft).Column
    wsShip.Range(wsShip.Cells(1, 1), wsShip.Cells(lRowI, lColI)).ClearContents
End If

'copies first column with the Products to new sheet

wsPlan.Range(wsPlan.Cells(1, 1), wsPlan.Cells(lRowP, 1)).Copy wsShip.Range("A1")

'Creates scripting dictionary from the shipping dates and pastes unique values to new sheet

Set d = CreateObject("scripting.dictionary")
For Each n In wsPlan.Range(wsPlan.Cells(1, 2), wsPlan.Cells(1, lColP))
    If Len(n.Value) > 0 Then
        If Not d.Exists(n.Value) Then d.Add n.Value, 1
    End If
Next n


wsShip.Range("B1").Resize(1, UBound(d.keys) + 1).Value = d.keys


lRowS2 = wsShip.Cells(wsShip.Rows.Count, 1).End(xlUp).Row
lColS2 = wsShip.Cells(1, wsShip.Columns.Count).End(xlToLeft).Column

'Tried with sumif but stuck

For rowNrI = 2 To lRowI2
    For Each v In wsShip.Range(wsShip.Cells(rowNrI, 2), wsShip.Cells(rowNrI, lColI2))
            v = Application.WorksheetFunction.SumIf(wsPlan.Range(wsPlan.Cells(1, 2), wsPlan.Cells(rowNrI, lColP)), d.keys, wsPlan.Range(wsPlan.Cells(rowNrI, 2), wsPlan.Cells(rowNrI, lColP)))
    Next v
Next rowNrI


With Application
        .ScreenUpdating = True
        .EnableEvents = True
End With


End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Forum statistics

Threads
1,216,555
Messages
6,131,372
Members
449,646
Latest member
dwalls

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