diff --git a/months.cls b/months.cls index d140f9c..fbf3496 100644 --- a/months.cls +++ b/months.cls @@ -23,15 +23,10 @@ Private jtext() As Variant Private basejson As Object -Private Sub Worksheet_Activate() - - Call Me.load_sheet - -End Sub Private Sub Worksheet_Change(ByVal target As Range) - Application.Calculation = xlCalculationManual + If Not dumping Then If Not Intersect(target, Range("E6:E17")) Is Nothing Then Call Me.mvp_adj @@ -44,13 +39,10 @@ Private Sub Worksheet_Change(ByVal target As Range) End If 'Call Me.set_format - Application.Calculation = xlCalculationAutomatic End Sub - - Sub mvp_set() Dim i As Integer @@ -63,6 +55,7 @@ Sub mvp_set() price(i, 4) = price(i, 5) - (price(i, 2) + price(i, 3)) sales(i, 5) = units(i, 5) * price(i, 5) sales(i, 4) = sales(i, 5) - (sales(i, 2) + sales(i, 3)) + Call Me.build_json(i) Next i Me.crunch_array @@ -83,6 +76,7 @@ Sub mvp_adj() price(i, 5) = price(i, 4) + (price(i, 2) + price(i, 3)) sales(i, 5) = units(i, 5) * price(i, 5) sales(i, 4) = sales(i, 5) - (sales(i, 2) + sales(i, 3)) + Call Me.build_json(i) Next i Me.crunch_array @@ -126,12 +120,20 @@ End Sub Sub set_sheet() - dumping = True + Dim i As Integer + dumping = True + Range("B6:F17") = units Range("H6:L17") = price Range("N6:R17") = sales + Range("B18:F18").FormulaR1C1 = tunits + Range("H18:L18").FormulaR1C1 = tprice + Range("N18:R18").FormulaR1C1 = tsales + For i = 1 To 12 + Sheets("_month").Cells(i + 1, 16) = JsonConverter.ConvertToJson(adjust(i)) + Next i dumping = False End Sub @@ -141,7 +143,12 @@ Sub load_sheet() units = Sheets("_month").Range("A2:E13").FormulaR1C1 price = Sheets("_month").Range("F2:J13").FormulaR1C1 sales = Sheets("_month").Range("K2:O13").FormulaR1C1 - + tunits = Range("B18:F18") + tprice = Range("H18:L18") + tsales = Range("N18:R18") + ReDim adjust(12) + Me.crunch_array + Me.set_sheet End Sub @@ -265,12 +272,32 @@ End Sub Sub build_json(ByVal pos As Integer) - 'if there is no existing volume on the target month - If units(pos, 2) + units(pos, 3) = 0 Then - 'add month - If price(pos, 4) = 0 Then - 'if the target price is diferent from the average - adjust(pos)("type") = "addmonth_vd" + 'if something is changing + If Round(units(pos, 4), 2) <> 0 Or Round(price(pos, 4), 8) <> 0 Or Round(sales(pos, 4), 8) <> 0 Then + Set adjust(pos) = basejson + 'if there is no existing volume on the target month but units are being added + If units(pos, 2) + units(pos, 3) = 0 And units(pos, 4) <> 0 Then + 'add month + If Round(price(pos, 5), 8) <> Round(tprice(1, 2) + tprice(1, 3), 8) Then + 'if the target price is diferent from the average and a month is being added + adjust(pos)("type") = "addmonth_vp" + Else + 'if the target price is the same as average and a month is being added + adjust(pos)("type") = "addmonth_v" + End If + adjust(pos)("qty") = units(pos, 4) + adjust(pos)("amount") = sales(pos, 4) + Else + 'scale the existing volume(price) on the target month + If Round(price(pos, 4), 8) <> 0 Then + 'if the target price is diferent from the average and a month is being added + adjust(pos)("type") = "scale_vp" + Else + 'if the target price is the same as average and a month is being added + adjust(pos)("type") = "scale_v" + End If + adjust(pos)("qty") = units(pos, 4) + adjust(pos)("amount") = sales(pos, 4) End If End If @@ -279,6 +306,7 @@ End Sub Sub crunch_array() Dim i As Integer + Dim j As Integer For i = 1 To 5 tunits(1, i) = 0 @@ -287,20 +315,22 @@ Sub crunch_array() Next i For i = 1 To 12 - tunits(1, 1) = tunits(1, 1) + units(i, 1) - tsales(1, 1) = tsales(1, 1) + sales(i, 1) + For j = 1 To 5 + tunits(1, j) = tunits(1, j) + units(i, j) + tsales(1, j) = tsales(1, j) + sales(i, j) + Next j Next i 'prior tprice(1, 1) = tsales(1, 1) / tunits(1, 1) 'base - tprice(2, 1) = tsales(2, 1) / tunits(2, 1) + tprice(1, 2) = tsales(1, 2) / tunits(1, 2) 'forecast - tprice(5, 1) = tsales(5, 1) / tunits(5, 1) + tprice(1, 5) = tsales(1, 5) / tunits(1, 5) 'adjust - tprice(3, 1) = (tsales(2, 1) + tsales(3, 1)) / (tunits(2, 1) + tunits(3, 1)) - tprice(2, 1) + tprice(1, 3) = (tsales(1, 2) + tsales(1, 3)) / (tunits(1, 2) + tunits(1, 3)) - tprice(1, 2) 'current adjust - tprice(4, 1) = tprice(5, 1) - (tprice(2, 1) + tprice(3, 1)) + tprice(1, 4) = tprice(1, 5) - (tprice(1, 2) + tprice(1, 3)) End Sub