diff --git a/Cat.cls b/Cat.cls new file mode 100644 index 0000000..09b6c9f --- /dev/null +++ b/Cat.cls @@ -0,0 +1,14 @@ +VERSION 1.0 CLASS +BEGIN + MultiUse = -1 'True +END +Attribute VB_Name = "Cat" +Attribute VB_GlobalNameSpace = False +Attribute VB_Creatable = False +Attribute VB_PredeclaredId = False +Attribute VB_Exposed = False +Implements IAnimal + +Private Sub IAnimal_Speak() + Debug.Print "Meow" +End Sub diff --git a/Dog.cls b/Dog.cls new file mode 100644 index 0000000..668bed8 --- /dev/null +++ b/Dog.cls @@ -0,0 +1,14 @@ +VERSION 1.0 CLASS +BEGIN + MultiUse = -1 'True +END +Attribute VB_Name = "Dog" +Attribute VB_GlobalNameSpace = False +Attribute VB_Creatable = False +Attribute VB_PredeclaredId = False +Attribute VB_Exposed = False +Implements IAnimal + +Private Sub IAnimal_Speak() + Debug.Print "Woof" +End Sub diff --git a/Fruits 63.xlsm b/Fruits 63.xlsm new file mode 100644 index 0000000..35438cb Binary files /dev/null and b/Fruits 63.xlsm differ diff --git a/IAnimal.cls b/IAnimal.cls new file mode 100644 index 0000000..b06bc5e --- /dev/null +++ b/IAnimal.cls @@ -0,0 +1,11 @@ +VERSION 1.0 CLASS +BEGIN + MultiUse = -1 'True +END +Attribute VB_Name = "IAnimal" +Attribute VB_GlobalNameSpace = False +Attribute VB_Creatable = False +Attribute VB_PredeclaredId = False +Attribute VB_Exposed = False +Sub Speak() +End Sub diff --git a/ICalc.cls b/ICalc.cls new file mode 100644 index 0000000..4fdd41c --- /dev/null +++ b/ICalc.cls @@ -0,0 +1,13 @@ +VERSION 1.0 CLASS +BEGIN + MultiUse = -1 'True +END +Attribute VB_Name = "ICalc" +Attribute VB_GlobalNameSpace = False +Attribute VB_Creatable = False +Attribute VB_PredeclaredId = False +Attribute VB_Exposed = False +Sub Add() +End Sub +Sub Subract() +End Sub diff --git a/ISummaryUpdater.cls b/ISummaryUpdater.cls new file mode 100644 index 0000000..d9b576b --- /dev/null +++ b/ISummaryUpdater.cls @@ -0,0 +1,19 @@ +VERSION 1.0 CLASS +BEGIN + MultiUse = -1 'True +END +Attribute VB_Name = "ISummaryUpdater" +Attribute VB_GlobalNameSpace = False +Attribute VB_Creatable = False +Attribute VB_PredeclaredId = False +Attribute VB_Exposed = False +Sub Update(engine As RuleEngine, sheet As Worksheet) +End Sub + +Function GetVersion() As String +End Function + +Function ClearSheet(ws As Worksheet) +End Function +Function FitAllColumns(ws As Worksheet) +End Function diff --git a/Main.bas b/Main.bas index 2928cf5..88fc4d0 100644 --- a/Main.bas +++ b/Main.bas @@ -56,9 +56,19 @@ Sub Main() ' Update category column in Groceries worksheet. UpdateCategory engine - UpdateSummary engine + 'UpdateSummary engine + Dim updater As New summaryUpdater + Dim updaterColorful As New SummaryUpdaterColorful + Dim updaterAdd As New SummaryUpdaterAdd + Dim updaterSubtract As New SummaryUpdaterSubtract - ' MsgBox (GetTotal(New totalProvider, engine)) + + UpdateSummary updaterAdd, engine, Worksheets("Summary") + UpdateSummary updaterSubtract, engine, Worksheets("SummarySubtract") + 'Or pass a variable + 'UpdateSummary updaterColorful, engine, Worksheets("Summary") + + 'MsgBox (GetTotal(New totalProvider, engine)) End Sub @@ -72,11 +82,15 @@ Private Sub UpdateCategory(engine As RuleEngine) Dim r As Range Dim Total As Double: Total = 0 + Dim colLetter As String + + colLetter = Col_Letter(ws.Range("A1").End(xlToRight).Column) For Each row In engine.Rows Set r = ws.Range("A" & row.RowNumber) 'COLUMN WHERE CATEGORY IS PRINTED - ws.Range("M" & row.RowNumber).ClearContents - ws.Range("M" & row.RowNumber).Value = row.Category + + ws.Range(colLetter & row.RowNumber).ClearContents + ws.Range(colLetter & row.RowNumber).Value = row.Category 'Debug.Print row.Columns("Id") & "=" & row.Category r.Interior.ColorIndex = 0 @@ -84,42 +98,35 @@ Private Sub UpdateCategory(engine As RuleEngine) Next End Sub -Private Sub UpdateSummary(engine As RuleEngine) - Dim row As RowInfo - Dim pRule As rule - Dim offset As Integer - Dim colHeaders As New Collection - colHeaders.Add ("PriceL") - colHeaders.Add ("PriceB") - - Dim colHeader1 As String - Dim colHeader2 As String - Dim colHeader3 As String - Dim colHeader4 As String - - colHeader1 = Worksheets("Summary").Range("B1").Value - colHeader2 = Worksheets("Summary").Range("C1").Value - ' go through all available rules - For Each pRule In engine.rules - Worksheets("Summary").Range("A2").offset(offset, 0) = pRule.Category - - ' Get total for each rule - Dim pTotal1 As Double: pTotal1 = 0 - Dim pTotal2 As Double: pTotal2 = 0 - For Each row In engine.Rows - If row.Category = pRule.Category Then - pTotal1 = pTotal1 + row.Columns(colHeader1) - pTotal2 = pTotal2 + row.Columns(colHeader2) - End If - Next - Worksheets("Summary").Range("B2").offset(offset, 0) = pTotal1 - Worksheets("Summary").Range("C2").offset(offset, 0) = pTotal2 - - offset = offset + 1 - Next -End Sub +Function Col_Letter(lngCol As Long) As String + Dim vArr + vArr = Split(Cells(1, lngCol).Address(True, False), "$") + Col_Letter = vArr(0) +End Function 'Private Function GetTotal(totalProvider As totalProvider, engine As RuleEngine) As Double ' GetTotal = totalProvider.GetTotal(engine) 'End Function +'Sub Test() +' +' Dim cat As New cat +' Dim dog As New dog +' +' AnimalSpeak cat +' AnimalSpeak dog +' +'End Sub +' +'Sub AnimalSpeak(a As IAnimal) +' a.Speak +'End Sub + +Sub UpdateSummary(summaryUpdater As ISummaryUpdater, engine As RuleEngine, sheet As Worksheet) + summaryUpdater.ClearSheet sheet + summaryUpdater.Update engine, sheet + summaryUpdater.FitAllColumns sheet + 'Debug.Print summaryUpdater.GetVersion() +End Sub + + diff --git a/RuleEngine.cls b/RuleEngine.cls index ac6f67a..7d9270e 100644 --- a/RuleEngine.cls +++ b/RuleEngine.cls @@ -122,7 +122,7 @@ Function IsRuleSuccess(ruleName As String) As Boolean If ruleName = "Rule" & r.RuleID Then result = r.Success If result Then - Debug.Print result + 'Debug.Print result End If End If Next diff --git a/SummaryUpdater.cls b/SummaryUpdater.cls new file mode 100644 index 0000000..7c5c88b --- /dev/null +++ b/SummaryUpdater.cls @@ -0,0 +1,62 @@ +VERSION 1.0 CLASS +BEGIN + MultiUse = -1 'True +END +Attribute VB_Name = "SummaryUpdater" +Attribute VB_GlobalNameSpace = False +Attribute VB_Creatable = False +Attribute VB_PredeclaredId = False +Attribute VB_Exposed = False +Implements ISummaryUpdater + +Private Function ISummaryUpdater_ClearSheet(ws As Worksheet) As Variant + +End Function + +Private Function ISummaryUpdater_FitAllColumns(ws As Worksheet) As Variant + ws.Cells.Select + ws.Cells.EntireColumn.AutoFit + ws.Cells(1, 1).Select +End Function + +Private Function ISummaryUpdater_GetVersion() As String + ISummaryUpdater_GetVersion = "1.0" +End Function + +Private Sub ISummaryUpdater_Update(engine As RuleEngine, sheet As Worksheet) + Dim row As RowInfo + Dim pRule As rule + Dim offset As Integer + + Dim ColHeaders As New Collection + ColHeaders.Add ("PriceL") + ColHeaders.Add ("PriceB") + + Dim colHeader1 As String + Dim colHeader2 As String + Dim colHeader3 As String + Dim colHeader4 As String + + colHeader1 = sheet.Range("B1").Value + colHeader2 = sheet.Range("C1").Value + ' go through all available rules + For Each pRule In engine.rules + sheet.Range("A2").offset(offset, 0) = pRule.Category + + ' Get total for each rule + Dim pTotal1 As Double: pTotal1 = 0 + Dim pTotal2 As Double: pTotal2 = 0 + For Each row In engine.Rows + If row.Category = pRule.Category Then + pTotal1 = pTotal1 + row.Columns(colHeader1) + pTotal2 = pTotal2 + row.Columns(colHeader2) + End If + Next + sheet.Range("B2").offset(offset, 0) = pTotal1 + sheet.Range("B2").offset(offset, 0).Interior.ColorIndex = 0 + sheet.Range("C2").offset(offset, 0) = pTotal2 + sheet.Range("C2").offset(offset, 0).Interior.ColorIndex = 0 + + offset = offset + 1 + Next +End Sub diff --git a/SummaryUpdaterAdd.cls b/SummaryUpdaterAdd.cls new file mode 100644 index 0000000..6cab004 --- /dev/null +++ b/SummaryUpdaterAdd.cls @@ -0,0 +1,58 @@ +VERSION 1.0 CLASS +BEGIN + MultiUse = -1 'True +END +Attribute VB_Name = "SummaryUpdaterAdd" +Attribute VB_GlobalNameSpace = False +Attribute VB_Creatable = False +Attribute VB_PredeclaredId = False +Attribute VB_Exposed = False +Option Explicit +Implements ISummaryUpdater + +Private Function ISummaryUpdater_ClearSheet(ws As Worksheet) As Variant +ws.Range("A2:ZZ6000").ClearContents +End Function + +Private Function ISummaryUpdater_FitAllColumns(ws As Worksheet) As Variant + ws.Activate + ws.Cells.Select + ws.Cells.EntireColumn.AutoFit + ws.Cells(1, 1).Select +End Function + +Private Function ISummaryUpdater_GetVersion() As String + +End Function + +Private Sub ISummaryUpdater_Update(engine As RuleEngine, sheet As Worksheet) + Dim row As RowInfo + Dim pRule As rule + Dim offset As Integer + + + Dim r As Range + Dim c As Range + Set r = sheet.Range(sheet.Range("B1"), sheet.Range("B1").End(xlToRight)) + 'r.Select + + + offset = 1 + ' go through all available rules + For Each pRule In engine.rules + sheet.Range("A2").offset(offset - 1, 0) = pRule.Category + + For Each row In engine.Rows + If row.Category = pRule.Category Then + + For Each c In r + c.offset(offset, 0).Value = c.offset(offset, 0).Value + row.Columns(c.Value) + Next + End If + Next + + + offset = offset + 1 + Next + +End Sub diff --git a/SummaryUpdaterColorful.cls b/SummaryUpdaterColorful.cls new file mode 100644 index 0000000..2903054 --- /dev/null +++ b/SummaryUpdaterColorful.cls @@ -0,0 +1,64 @@ +VERSION 1.0 CLASS +BEGIN + MultiUse = -1 'True +END +Attribute VB_Name = "SummaryUpdaterColorful" +Attribute VB_GlobalNameSpace = False +Attribute VB_Creatable = False +Attribute VB_PredeclaredId = False +Attribute VB_Exposed = False +Implements ISummaryUpdater + +Private Function ISummaryUpdater_ClearSheet(ws As Worksheet) As Variant + +End Function + +Private Function ISummaryUpdater_FitAllColumns(ws As Worksheet) As Variant + ws.Cells.Select + ws.Cells.EntireColumn.AutoFit + ws.Cells(1, 1).Select +End Function + +Private Function ISummaryUpdater_GetVersion() As String + ISummaryUpdater_GetVersion = "2.0" +End Function + +Private Sub ISummaryUpdater_Update(engine As RuleEngine, sheet As Worksheet) + + Dim row As RowInfo + Dim pRule As rule + Dim offset As Integer + Dim ColHeaders As New Collection + ColHeaders.Add ("PriceL") + ColHeaders.Add ("PriceB") + + Dim colHeader1 As String + Dim colHeader2 As String + Dim colHeader3 As String + Dim colHeader4 As String + + colHeader1 = sheet.Range("B1").Value + colHeader2 = sheet.Range("C1").Value + ' go through all available rules + For Each pRule In engine.rules + sheet.Range("A2").offset(offset, 0) = pRule.Category + + ' Get total for each rule + Dim pTotal1 As Double: pTotal1 = 0 + Dim pTotal2 As Double: pTotal2 = 0 + For Each row In engine.Rows + If row.Category = pRule.Category Then + pTotal1 = pTotal1 + row.Columns(colHeader1) + pTotal2 = pTotal2 + row.Columns(colHeader2) + End If + Next + sheet.Range("B2").offset(offset, 0) = pTotal1 + sheet.Range("B2").offset(offset, 0).Interior.ColorIndex = 6 + sheet.Range("C2").offset(offset, 0) = pTotal2 + sheet.Range("C2").offset(offset, 0).Interior.ColorIndex = 7 + + offset = offset + 1 + Next + +End Sub + diff --git a/SummaryUpdaterSubtract.cls b/SummaryUpdaterSubtract.cls new file mode 100644 index 0000000..78c66e2 --- /dev/null +++ b/SummaryUpdaterSubtract.cls @@ -0,0 +1,59 @@ +VERSION 1.0 CLASS +BEGIN + MultiUse = -1 'True +END +Attribute VB_Name = "SummaryUpdaterSubtract" +Attribute VB_GlobalNameSpace = False +Attribute VB_Creatable = False +Attribute VB_PredeclaredId = False +Attribute VB_Exposed = False +Option Explicit +Implements ISummaryUpdater + +Private Function ISummaryUpdater_ClearSheet(ws As Worksheet) As Variant +ws.Range("A2:ZZ6000").ClearContents +End Function + +Private Function ISummaryUpdater_FitAllColumns(ws As Worksheet) As Variant + ws.Activate + ws.Cells.Select + ws.Cells.EntireColumn.AutoFit + ws.Cells(1, 1).Select +End Function + +Private Function ISummaryUpdater_GetVersion() As String + +End Function + +Private Sub ISummaryUpdater_Update(engine As RuleEngine, sheet As Worksheet) + Dim row As RowInfo + Dim pRule As rule + Dim offset As Integer + + + Dim r As Range + Dim c As Range + Set r = sheet.Range(sheet.Range("B1"), sheet.Range("B1").End(xlToRight)) + 'r.Select + + + offset = 1 + ' go through all available rules + For Each pRule In engine.rules + sheet.Range("A2").offset(offset - 1, 0) = pRule.Category + + For Each row In engine.Rows + If row.Category = pRule.Category Then + + For Each c In r + c.offset(offset, 0).Value = c.offset(offset, 0).Value - row.Columns(c.Value) + Next + End If + Next + + + offset = offset + 1 + Next + +End Sub +