Skip to content

Commit

Permalink
Excel VBA Interfaces & Implements; jqGrid
Browse files Browse the repository at this point in the history
Also noted JavaScript jqGrid Demons within http://trirand.com/blog/jqgrid/jqgrid.html as a possible long-term infrastructure approach
  • Loading branch information
phillipsk authored Mar 5, 2018
1 parent 192942c commit 5fa5b0d
Show file tree
Hide file tree
Showing 12 changed files with 360 additions and 39 deletions.
14 changes: 14 additions & 0 deletions Cat.cls
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions Dog.cls
Original file line number Diff line number Diff line change
@@ -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
Binary file added Fruits 63.xlsm
Binary file not shown.
11 changes: 11 additions & 0 deletions IAnimal.cls
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions ICalc.cls
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions ISummaryUpdater.cls
Original file line number Diff line number Diff line change
@@ -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
83 changes: 45 additions & 38 deletions Main.bas
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -72,54 +82,51 @@ 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

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


2 changes: 1 addition & 1 deletion RuleEngine.cls
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions SummaryUpdater.cls
Original file line number Diff line number Diff line change
@@ -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
58 changes: 58 additions & 0 deletions SummaryUpdaterAdd.cls
Original file line number Diff line number Diff line change
@@ -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
64 changes: 64 additions & 0 deletions SummaryUpdaterColorful.cls
Original file line number Diff line number Diff line change
@@ -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

Loading

0 comments on commit 5fa5b0d

Please sign in to comment.