update,
This commit is contained in:
296
jimmycheung93/task4/_poc/main_xlsm/AgentPerformanceAnalysis.bas
Normal file
296
jimmycheung93/task4/_poc/main_xlsm/AgentPerformanceAnalysis.bas
Normal file
@@ -0,0 +1,296 @@
|
||||
Attribute VB_Name = "AgentPerformanceAnalysis"
|
||||
|
||||
|
||||
Global AgentPerformanceAnalysisHelloworld as string
|
||||
Global AgentPerformanceAnalysisRankOfEachMonth(12,5) as string
|
||||
Global TempMonthlyComission(12) as String
|
||||
Global SortedMonthlyComission(12) as Double
|
||||
|
||||
Global sortedArray as Variant
|
||||
Global sort_result as variant
|
||||
Global monthly_top_5_comission_value(12,5) as Double
|
||||
Global monthly_top_5_comission_name(12,5) as String
|
||||
|
||||
Function Calc(ByVal TablesMeta As Variant) As Variant
|
||||
Dim agent_sales_table As Variant
|
||||
Dim product_sales_meta As Variant
|
||||
Dim product_sales_table As Variant
|
||||
|
||||
agent_sales_table = TablesMeta(0)
|
||||
Dim agent_sales_table_rows As Variant
|
||||
agent_sales_table_rows = agent_sales_table(0)
|
||||
|
||||
product_sales_table = TablesMeta(1)
|
||||
Dim product_sales_table_row As Variant
|
||||
product_sales_table_row = product_sales_table(0)
|
||||
|
||||
agent_working_performance = TablesMeta(2)
|
||||
|
||||
Dim val_product_sales_comission As String
|
||||
Dim val_product_sales_selling_price As String
|
||||
Dim val_product_sales_selling_unit As String
|
||||
|
||||
Dim val_agent_sale_commision_pct As String
|
||||
Dim val_agent_sale_agent_name As String
|
||||
Dim val_agent_sale_agent_team As String
|
||||
|
||||
Dim monthly_commission(12) As Double
|
||||
|
||||
Dim montyly_commission_TA(12) As Double
|
||||
Dim montyly_commission_TB(12) As Double
|
||||
|
||||
Dim quartely_commission_TA(12) As Double
|
||||
Dim quartely_commission_TB(12) As Double
|
||||
|
||||
For ps = 1 To product_sales_table(1)
|
||||
val_product_sales_date = product_sales_table_row(ps, 2)
|
||||
|
||||
dt = DateValue(val_product_sales_date)
|
||||
intMonth = month(dt)
|
||||
intQuarter = Int((intMonth - 1) / 3) + 1
|
||||
|
||||
val_product_sales_selling_unit = product_sales_table_row(ps, 4)
|
||||
val_product_sales_selling_price = product_sales_table_row(ps, 5)
|
||||
|
||||
val_agent_sale_agent_name = agent_sales_table_rows(ps, 3)
|
||||
val_agent_sale_agent_team = agent_sales_table_rows(ps, 4)
|
||||
val_agent_sale_commision_pct = agent_sales_table_rows(ps, 6)
|
||||
|
||||
val_product_sales_comission = val_product_sales_selling_unit * val_product_sales_selling_price * val_agent_sale_commision_pct
|
||||
product_sales_table_row(ps, 6) = val_product_sales_comission
|
||||
|
||||
|
||||
Dim temp_key As String
|
||||
temp_key = val_agent_sale_agent_name + "," + CStr(intMonth)
|
||||
|
||||
If (Not (IsEmpty(temp_key)) And Not (monthly_commission_per_agent.exists(temp_key))) Then
|
||||
For j = 1 To 12
|
||||
monthly_commission_per_agent(val_agent_sale_agent_name + "," + CStr(j)) = 0
|
||||
|
||||
Next j
|
||||
End If
|
||||
|
||||
monthly_commission_per_agent(temp_key) = monthly_commission_per_agent(temp_key) + val_product_sales_comission
|
||||
|
||||
monthly_commission(intMonth) = monthly_commission(intMonth) + val_product_sales_comission
|
||||
|
||||
If (val_agent_sale_agent_team = "A") Then
|
||||
montyly_commission_TA(intMonth) = montyly_commission_TA(intMonth) + val_product_sales_comission
|
||||
End If
|
||||
If (val_agent_sale_agent_team = "B") Then
|
||||
montyly_commission_TB(intMonth) = montyly_commission_TB(intMonth) + val_product_sales_comission
|
||||
End If
|
||||
|
||||
If (val_agent_sale_agent_team = "A") Then
|
||||
quartely_commission_TA(intQuarter) = quartely_commission_TA(intQuarter) + val_product_sales_comission
|
||||
End If
|
||||
If (val_agent_sale_agent_team = "B") Then
|
||||
quartely_commission_TB(intQuarter) = quartely_commission_TB(intQuarter) + val_product_sales_comission
|
||||
End If
|
||||
|
||||
Next ps
|
||||
|
||||
for m = 1 to 12
|
||||
|
||||
Dim temp_sort(20) As Double
|
||||
i = 0
|
||||
FOR each sales_name in SALES_ARRAY
|
||||
temp_key = sales_name + "," + CStr(m)
|
||||
temp_sort(i) = monthly_commission_per_agent(temp_key)
|
||||
i = i + 1
|
||||
next sales_name
|
||||
|
||||
|
||||
sort_result = SortArray(temp_sort)
|
||||
|
||||
monthly_top_5_comission_value(m, 0 ) = sort_result(0)
|
||||
monthly_top_5_comission_value(m, 1 ) = sort_result(1)
|
||||
monthly_top_5_comission_value(m, 2 ) = sort_result(2)
|
||||
monthly_top_5_comission_value(m, 3 ) = sort_result(3)
|
||||
monthly_top_5_comission_value(m, 4 ) = sort_result(4)
|
||||
|
||||
FOR each sales_name in SALES_ARRAY
|
||||
temp_key = sales_name + "," + CStr(m)
|
||||
if (monthly_top_5_comission_value(m, 0 ) = monthly_commission_per_agent(temp_key)) Then
|
||||
monthly_top_5_comission_name(m, 0 ) = sales_name
|
||||
end if
|
||||
|
||||
if (monthly_top_5_comission_value(m, 1 ) = monthly_commission_per_agent(temp_key)) Then
|
||||
monthly_top_5_comission_name(m, 1 ) = sales_name
|
||||
end if
|
||||
|
||||
if (monthly_top_5_comission_value(m, 2 ) = monthly_commission_per_agent(temp_key)) Then
|
||||
monthly_top_5_comission_name(m, 2 ) = sales_name
|
||||
end if
|
||||
|
||||
if (monthly_top_5_comission_value(m, 3 ) = monthly_commission_per_agent(temp_key)) Then
|
||||
monthly_top_5_comission_name(m, 3 ) = sales_name
|
||||
end if
|
||||
|
||||
if (monthly_top_5_comission_value(m, 4 ) = monthly_commission_per_agent(temp_key)) Then
|
||||
monthly_top_5_comission_name(m, 4 ) = sales_name
|
||||
end if
|
||||
|
||||
next sales_name
|
||||
next m
|
||||
|
||||
Debug.print "helloworld"
|
||||
|
||||
|
||||
|
||||
Calc = Array(monthly_commission_per_agent, monthly_commission, montyly_commission_TA, montyly_commission_TB, quartely_commission_TA, quartely_commission_TB)
|
||||
End Function
|
||||
|
||||
Function WriteTable(ByVal calc_result As Variant, ByVal FILE_PATH As String)
|
||||
|
||||
Dim monthly_commission As Variant
|
||||
Dim montyly_commission_TA As Variant
|
||||
Dim montyly_commission_TB As Variant
|
||||
|
||||
monthly_commission = calc_result(1)
|
||||
montyly_commission_TA = calc_result(2)
|
||||
montyly_commission_TB = calc_result(3)
|
||||
quartely_commission_TA = calc_result(4)
|
||||
quartely_commission_TB = calc_result(5)
|
||||
|
||||
Const TOP_ROW As Long = 1
|
||||
Const START_CELL As String = "A2"
|
||||
Dim wb As Workbook
|
||||
Dim ws As Worksheet
|
||||
Dim startCell As Range
|
||||
Dim i As Long
|
||||
Dim tempArray() As Variant
|
||||
|
||||
' Open the workbook
|
||||
Set wb = Workbooks.Open(FILE_PATH)
|
||||
|
||||
' Check if the sheet exists
|
||||
On Error Resume Next
|
||||
Set ws = wb.Sheets("Agent performance analysis")
|
||||
On Error GoTo 0
|
||||
|
||||
' If not found, create a new sheet
|
||||
If ws Is Nothing Then
|
||||
Set ws = wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count))
|
||||
ws.Name = "Agent performance analysis"
|
||||
Else
|
||||
' Empty the entire sheet
|
||||
ws.UsedRange.ClearContents
|
||||
End If
|
||||
|
||||
' Write month names
|
||||
Set startCell = ws.Range("B1")
|
||||
|
||||
tempArray = Array("January", "Feburary", "March", "April", "May", "June", "July", "Augest", "September", "October", "November", "December")
|
||||
For i = LBound(tempArray) To UBound(tempArray)
|
||||
startCell.Offset(0, i).Value = tempArray(i)
|
||||
Next i
|
||||
|
||||
' Write month names
|
||||
Set startCell = ws.Range("A2")
|
||||
|
||||
For i = LBound(SALES_ARRAY) To UBound(SALES_ARRAY)
|
||||
startCell.Offset(i, 0).Value = SALES_ARRAY(i)
|
||||
Next i
|
||||
|
||||
' Write month names
|
||||
Set startCell = ws.Range("A14")
|
||||
tempArray = Array("Total", "", "Team A total", "Team B Total", "Team A Quartely", "Team B Quartely")
|
||||
For i = LBound(tempArray) To UBound(tempArray)
|
||||
startCell.Offset(i, 0).Value = tempArray(i)
|
||||
Next i
|
||||
|
||||
Set startCell = ws.Range("B2")
|
||||
|
||||
|
||||
For m = 1 To 12
|
||||
For i = LBound(SALES_ARRAY) To UBound(SALES_ARRAY)
|
||||
Dim sales_name As String
|
||||
sale_name_comma_month = SALES_ARRAY(i) + "," + CStr(m)
|
||||
|
||||
startCell.Offset(i, m - 1).Value = monthly_commission_per_agent(sale_name_comma_month)
|
||||
|
||||
Next i
|
||||
Next m
|
||||
|
||||
' Write month names
|
||||
Set startCell = ws.Range("B14")
|
||||
tempArray = Array("Total", "", "Team A total", "Team B Total", "Team A Quartely", "Team B Quartely")
|
||||
' For i = LBound(tempArray) To UBound(tempArray)
|
||||
' startCell.Offset(i, 0).Value = tempArray(i)
|
||||
' Next i
|
||||
For m = 1 To 12
|
||||
Dim intQuarter As Integer
|
||||
intQuarter = Int((m - 1) / 3) + 1
|
||||
startCell.Offset(0, m - 1).Value = monthly_commission(m)
|
||||
startCell.Offset(2, m - 1).Value = montyly_commission_TA(m)
|
||||
startCell.Offset(3, m - 1).Value = montyly_commission_TB(m)
|
||||
startCell.Offset(4, m - 1).Value = quartely_commission_TA(intQuarter)
|
||||
startCell.Offset(5, m - 1).Value = quartely_commission_TB(intQuarter)
|
||||
Next m
|
||||
|
||||
' Rank for each month
|
||||
Set startCell = ws.Range("A21")
|
||||
startCell.Value = "Rank for each month"
|
||||
|
||||
tempArray = Array("January", "Feburary", "March", "April", "May", "June")
|
||||
For i = 0 to 5
|
||||
startCell.Offset(1, (i*2)+1).Value = tempArray(i)
|
||||
Next i
|
||||
For i = 1 to 5
|
||||
startCell.Offset(i+1, 0).Value = i
|
||||
Next i
|
||||
|
||||
tempArray = Array("July", "Augest", "September", "October", "November", "December")
|
||||
For i = 0 to 5
|
||||
startCell.Offset(8, (i*2)+1).Value = tempArray(i)
|
||||
Next i
|
||||
For i = 1 to 5
|
||||
startCell.Offset(i+8, 0).Value = i
|
||||
Next i
|
||||
|
||||
for m = 1 to 6
|
||||
i = m - 1
|
||||
startCell.Offset(2, (i*2)+1).Value = monthly_top_5_comission_value(m,0)
|
||||
startCell.Offset(3, (i*2)+1).Value = monthly_top_5_comission_value(m,1)
|
||||
startCell.Offset(4, (i*2)+1).Value = monthly_top_5_comission_value(m,2)
|
||||
startCell.Offset(5, (i*2)+1).Value = monthly_top_5_comission_value(m,3)
|
||||
startCell.Offset(6, (i*2)+1).Value = monthly_top_5_comission_value(m,4)
|
||||
|
||||
startCell.Offset(2, (i*2)+2).Value = monthly_top_5_comission_name(m,0)
|
||||
startCell.Offset(3, (i*2)+2).Value = monthly_top_5_comission_name(m,1)
|
||||
startCell.Offset(4, (i*2)+2).Value = monthly_top_5_comission_name(m,2)
|
||||
startCell.Offset(5, (i*2)+2).Value = monthly_top_5_comission_name(m,3)
|
||||
startCell.Offset(6, (i*2)+2).Value = monthly_top_5_comission_name(m,4)
|
||||
Next m
|
||||
|
||||
|
||||
for m = 7 to 12
|
||||
i = m - 7
|
||||
startCell.Offset(9, (i*2)+1).Value = monthly_top_5_comission_value(m,0)
|
||||
startCell.Offset(10, (i*2)+1).Value = monthly_top_5_comission_value(m,1)
|
||||
startCell.Offset(11, (i*2)+1).Value = monthly_top_5_comission_value(m,2)
|
||||
startCell.Offset(12, (i*2)+1).Value = monthly_top_5_comission_value(m,3)
|
||||
startCell.Offset(13, (i*2)+1).Value = monthly_top_5_comission_value(m,4)
|
||||
|
||||
startCell.Offset(9, (i*2)+2).Value = monthly_top_5_comission_name(m,0)
|
||||
startCell.Offset(10, (i*2)+2).Value = monthly_top_5_comission_name(m,1)
|
||||
startCell.Offset(11, (i*2)+2).Value = monthly_top_5_comission_name(m,2)
|
||||
startCell.Offset(12, (i*2)+2).Value = monthly_top_5_comission_name(m,3)
|
||||
startCell.Offset(13, (i*2)+2).Value = monthly_top_5_comission_name(m,4)
|
||||
Next m
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
' Save and close the workbook
|
||||
' wb.Close SaveChanges:=True
|
||||
End Function
|
||||
|
64
jimmycheung93/task4/_poc/main_xlsm/Common.bas
Normal file
64
jimmycheung93/task4/_poc/main_xlsm/Common.bas
Normal file
@@ -0,0 +1,64 @@
|
||||
Attribute VB_Name = "Common"
|
||||
|
||||
Sub test()
|
||||
Dim sPath As String
|
||||
Dim calc_result As Variant
|
||||
|
||||
Config.init
|
||||
|
||||
sPath = "D:\_workspace\carousell-comission-playlist\jimmycheung93\task4\_poc\parse_xlsx\parse_Agent_Sales\Agent_Sales.xlsx"
|
||||
AgentSalesMeta = ReadAgentSalesWorkbook.Run(sPath)
|
||||
|
||||
sPath = "D:\_workspace\carousell-comission-playlist\jimmycheung93\task4\_poc\parse_xlsx\parse_Product_Sales\Product_Sales.xlsx"
|
||||
ProductSalesMeta = ReadProductSalesWorkbook.Run(sPath)
|
||||
|
||||
sPath = "D:\_workspace\carousell-comission-playlist\jimmycheung93\task4\_poc\parse_xlsx\parse_Agent_Working_Performance\Agent_Working_Performance.xlsx"
|
||||
AgentWorkingPerformanceMeta = ReadAgentWorkingPerfWorkbook.Run(sPath)
|
||||
|
||||
CombinedSalesMeta = Array(AgentSalesMeta, ProductSalesMeta, AgentWorkingPerformanceMeta)
|
||||
|
||||
Const FILE_PATH As String = "D:\_workspace\carousell-comission-playlist\jimmycheung93\task4\_poc\main_xlsm\helloworld.xlsx"
|
||||
|
||||
calc_result = SalesAnalysisMonthlyAndQuart.CalcSalesAnalysisMonthlyAndQuart(CombinedSalesMeta)
|
||||
WriteSalesAnalysisMonthlyAndQuart calc_result, FILE_PATH
|
||||
|
||||
' write sales analysis product category
|
||||
calc_result = CalcSalesAnalysisFileSalesAmount(CombinedSalesMeta)
|
||||
WriteSalesAnalysisFileSalesAmount calc_result, FILE_PATH
|
||||
|
||||
calc_result = CalcSalesAnalysisFileSalesUnit(CombinedSalesMeta)
|
||||
WriteSalesAnalysisFileSalesUnit calc_result, FILE_PATH
|
||||
|
||||
calc_result = AgentPerformanceAnalysis.Calc(CombinedSalesMeta)
|
||||
AgentPerformanceAnalysis.WriteTable calc_result, FILE_PATH
|
||||
|
||||
calc_result = NoOfCasesTable.Calc(CombinedSalesMeta)
|
||||
NoOfCasesTable.WriteTable calc_result, FILE_PATH
|
||||
|
||||
Debug.Print "done"
|
||||
|
||||
End Sub
|
||||
|
||||
Function GetQuarterFromMonth(ByVal month As Integer) As Integer
|
||||
GetQuarterFromMonth = Int((month - 1) / 3) + 1
|
||||
End Function
|
||||
|
||||
|
||||
|
||||
|
||||
Function SortArray(ByRef arr() As Double) As Double()
|
||||
Dim i As Long, j As Long
|
||||
Dim temp As Variant
|
||||
|
||||
For i = LBound(arr) To UBound(arr) - 1
|
||||
For j = i + 1 To UBound(arr)
|
||||
If arr(i) < arr(j) Then
|
||||
temp = arr(i)
|
||||
arr(i) = arr(j)
|
||||
arr(j) = temp
|
||||
End If
|
||||
Next j
|
||||
Next i
|
||||
|
||||
SortArray = arr
|
||||
End Function
|
74
jimmycheung93/task4/_poc/main_xlsm/Config.bas
Normal file
74
jimmycheung93/task4/_poc/main_xlsm/Config.bas
Normal file
@@ -0,0 +1,74 @@
|
||||
Attribute VB_Name = "Config"
|
||||
|
||||
Global NumOfSales As Integer
|
||||
|
||||
Global SALES_ANALYSIS_COL_MONTH As String
|
||||
Global SALES_ANALYSIS_COL_QUARTER As String
|
||||
Global SALES_ANALYSIS_COL_PRODUCT_CATEGORY As String
|
||||
Global SALES_ANALYSIS_COL_SELLING_UNIT As String
|
||||
Global SALES_ANALYSIS_COL_MONTHLY_SALES As String
|
||||
Global SALES_ANALYSIS_COL_QUARTELY_SALES As String
|
||||
Global SALES_ANALYSIS_COL_COMMISSION As String
|
||||
Global SALES_ANALYSIS_COL_MONTHLY_MARGIN As String
|
||||
Global SALES_ANALYSIS_COL_QUATERLY_MARGIN As String
|
||||
Global SALES_ANALYSIS_COL_QUATERLY_COMMISSION_ As String
|
||||
|
||||
'row
|
||||
Global SALES_ANALYSIS_ROW_JANUARY As String
|
||||
Global SALES_ANALYSIS_ROW_FEBURARY As String
|
||||
Global SALES_ANALYSIS_ROW_MARCH As String
|
||||
Global SALES_ANALYSIS_ROW_APRIL As String
|
||||
Global SALES_ANALYSIS_ROW_MAY As String
|
||||
Global SALES_ANALYSIS_ROW_JUNE As String
|
||||
Global SALES_ANALYSIS_ROW_JULY As String
|
||||
Global SALES_ANALYSIS_ROW_AUGEST As String
|
||||
Global SALES_ANALYSIS_ROW_SEPTEMBER As String
|
||||
Global SALES_ANALYSIS_ROW_OCTOBER As String
|
||||
Global SALES_ANALYSIS_ROW_NOVEMBER As String
|
||||
Global SALES_ANALYSIS_ROW_DECEMBER As String
|
||||
|
||||
Global SALES_ARRAY As Variant
|
||||
|
||||
Global monthly_commission_per_agent As Object
|
||||
|
||||
Global STRING_SEPERATOR as string
|
||||
|
||||
Sub init()
|
||||
STRING_SEPERATOR = "#_STRING_SEPERATOR_#"
|
||||
|
||||
SALES_ANALYSIS_COL_MONTH = "A"
|
||||
SALES_ANALYSIS_COL_QUARTER = "B"
|
||||
SALES_ANALYSIS_COL_PRODUCT_CATEGORY = "C"
|
||||
SALES_ANALYSIS_COL_SELLING_UNIT = "D"
|
||||
SALES_ANALYSIS_COL_MONTHLY_SALES = "E"
|
||||
SALES_ANALYSIS_COL_QUARTELY_SALES = "F"
|
||||
SALES_ANALYSIS_COL_COMMISSION = "G"
|
||||
SALES_ANALYSIS_COL_MONTHLY_MARGIN = "H"
|
||||
SALES_ANALYSIS_COL_QUATERLY_MARGIN = "I"
|
||||
SALES_ANALYSIS_COL_QUATERLY_COMMISSION_ = "J"
|
||||
|
||||
'row
|
||||
SALES_ANALYSIS_ROW_JANUARY = "2"
|
||||
SALES_ANALYSIS_ROW_FEBURARY = "3"
|
||||
SALES_ANALYSIS_ROW_MARCH = "4"
|
||||
SALES_ANALYSIS_ROW_APRIL = "5"
|
||||
SALES_ANALYSIS_ROW_MAY = "6"
|
||||
SALES_ANALYSIS_ROW_JUNE = "7"
|
||||
SALES_ANALYSIS_ROW_JULY = "8"
|
||||
SALES_ANALYSIS_ROW_AUGEST = "9"
|
||||
SALES_ANALYSIS_ROW_SEPTEMBER = "10"
|
||||
SALES_ANALYSIS_ROW_OCTOBER = "11"
|
||||
SALES_ANALYSIS_ROW_NOVEMBER = "12"
|
||||
SALES_ANALYSIS_ROW_DECEMBER = "13"
|
||||
|
||||
NumOfSales = 20
|
||||
SALES_ARRAY = Array("Alex", "Ben", "Candy", "Danny", "Eason", "Filex", "Gary", "Henry", "Irene", "Jenny")
|
||||
|
||||
Dim top_comission(12, 3) As Double
|
||||
|
||||
Set monthly_commission_per_agent = CreateObject("Scripting.Dictionary")
|
||||
|
||||
|
||||
End Sub
|
||||
|
||||
|
204
jimmycheung93/task4/_poc/main_xlsm/NoOfCasesTable.bas
Normal file
204
jimmycheung93/task4/_poc/main_xlsm/NoOfCasesTable.bas
Normal file
@@ -0,0 +1,204 @@
|
||||
Attribute VB_Name = "NoOfCasesTable"
|
||||
|
||||
Global new_case_per_agent As Object
|
||||
Global collapsed_case_per_agent As Object
|
||||
|
||||
Global new_case_team_a As Double
|
||||
Global collapsed_case_team_a As Double
|
||||
|
||||
Global new_case_team_b As Double
|
||||
Global collapsed_case_team_b As Double
|
||||
|
||||
Global case_persistency_by_agent As Object
|
||||
Global case_persistency_team_a As Double
|
||||
Global case_persistency_team_b As Double
|
||||
|
||||
Function Calc(ByVal TablesMeta As Variant) As Variant
|
||||
Debug.Print "helloworld"
|
||||
Set new_case_per_agent = CreateObject("Scripting.Dictionary")
|
||||
Set collapsed_case_per_agent = CreateObject("Scripting.Dictionary")
|
||||
Set case_persistency_by_agent = CreateObject("Scripting.Dictionary")
|
||||
|
||||
Dim agent_sales_table As Variant
|
||||
Dim product_sales_meta As Variant
|
||||
Dim product_sales_table As Variant
|
||||
|
||||
Dim val_agent_name as string
|
||||
Dim val_team as string
|
||||
|
||||
agent_sales_table = TablesMeta(0)
|
||||
Dim agent_sales_table_rows As Variant
|
||||
agent_sales_table_rows = agent_sales_table(0)
|
||||
|
||||
product_sales_table = TablesMeta(1)
|
||||
Dim product_sales_table_row As Variant
|
||||
product_sales_table_row = product_sales_table(0)
|
||||
|
||||
agent_working_performance_table = TablesMeta(2)
|
||||
Dim agent_working_performance_table_rows As Variant
|
||||
agent_working_performance_table_rows = agent_working_performance_table(0)
|
||||
|
||||
Dim val_product_sales_selling_unit As String
|
||||
|
||||
Dim sales_selling_unit_TA As Integer
|
||||
Dim sales_selling_unit_TB As Integer
|
||||
Dim team As String
|
||||
|
||||
For ps = 1 To agent_working_performance_table(1)
|
||||
val_agent_name = agent_working_performance_table_rows(ps, 2)
|
||||
val_team = agent_working_performance_table_rows(ps, 3)
|
||||
num_new_case = agent_working_performance_table_rows(ps, 4)
|
||||
num_collapsed_case = agent_working_performance_table_rows(ps, 5)
|
||||
|
||||
|
||||
If (Not (IsEmpty(val_agent_name)) And Not (new_case_per_agent.exists(val_agent_name))) Then
|
||||
new_case_per_agent(val_agent_name) = 0
|
||||
End If
|
||||
new_case_per_agent(val_agent_name) = new_case_per_agent(val_agent_name) + num_new_case
|
||||
|
||||
If (Not (IsEmpty(val_agent_name)) And Not (collapsed_case_per_agent.exists(val_agent_name))) Then
|
||||
collapsed_case_per_agent(val_agent_name) = 0
|
||||
End If
|
||||
collapsed_case_per_agent(val_agent_name) = collapsed_case_per_agent(val_agent_name) + num_collapsed_case
|
||||
|
||||
if (val_team = "A") Then
|
||||
new_case_team_a = new_case_team_a + num_new_case
|
||||
collapsed_case_team_a = collapsed_case_team_a + num_collapsed_case
|
||||
end if
|
||||
|
||||
if (val_team = "B") Then
|
||||
new_case_team_b = new_case_team_b + num_new_case
|
||||
collapsed_case_team_b = collapsed_case_team_b + num_collapsed_case
|
||||
end if
|
||||
|
||||
Next ps
|
||||
|
||||
for s = 0 to UBound(SALES_ARRAY)
|
||||
sales_name = SALES_ARRAY(s)
|
||||
collapsed_case = collapsed_case_per_agent(sales_name)
|
||||
new_case = new_case_per_agent(sales_name)
|
||||
|
||||
case_persistency_by_agent(sales_name) = ( new_case - collapsed_case ) / new_case
|
||||
|
||||
next s
|
||||
|
||||
case_persistency_team_a = (new_case_team_a - collapsed_case_team_a ) / new_case_team_a
|
||||
case_persistency_team_b = (new_case_team_b - collapsed_case_team_b ) / new_case_team_b
|
||||
|
||||
|
||||
Calc = Array(1, sales_selling_unit_TA, sales_selling_unit_TB)
|
||||
|
||||
End Function
|
||||
|
||||
Function WriteTable(ByVal calc_result As Variant, ByVal FILE_PATH As String)
|
||||
Debug.Print "helloworld"
|
||||
|
||||
Dim sales_selling_unit_TA As Integer
|
||||
Dim sales_selling_unit_TB As Integer
|
||||
sales_selling_unit_TA = calc_result(1)
|
||||
sales_selling_unit_TB = calc_result(2)
|
||||
|
||||
Const TOP_ROW As Long = 1
|
||||
Dim wb As Workbook
|
||||
Dim ws As Worksheet
|
||||
Dim startCell As Range
|
||||
Dim tempArray() As Variant
|
||||
Dim i As Long
|
||||
|
||||
' Open the workbook
|
||||
Set wb = Workbooks.Open(FILE_PATH)
|
||||
|
||||
' Check if the sheet exists
|
||||
On Error Resume Next
|
||||
Set ws = wb.Sheets("No. of cases")
|
||||
On Error GoTo 0
|
||||
|
||||
' If not found, create a new sheet
|
||||
If ws Is Nothing Then
|
||||
Set ws = wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count))
|
||||
ws.Name = "No. of cases"
|
||||
Else
|
||||
' Empty the entire sheet
|
||||
ws.UsedRange.ClearContents
|
||||
End If
|
||||
|
||||
|
||||
' Write month names
|
||||
Set startCell = ws.Range("A1")
|
||||
tempArray = Array("Case Persistency")
|
||||
For i = LBound(tempArray) To UBound(tempArray)
|
||||
startCell.Offset(0, i).Value = tempArray(i)
|
||||
Next i
|
||||
|
||||
' Write month names
|
||||
Set startCell = ws.Range("A2")
|
||||
tempArray = Array("Name", "No of new case", "No. of collpased case", "Case Persistency")
|
||||
For i = LBound(tempArray) To UBound(tempArray)
|
||||
startCell.Offset(0, i).Value = tempArray(i)
|
||||
Next i
|
||||
|
||||
' Write month names
|
||||
Set startCell = ws.Range("A3")
|
||||
For i = LBound(SALES_ARRAY) To UBound(SALES_ARRAY)
|
||||
startCell.Offset(i, 0).Value = SALES_ARRAY(i)
|
||||
Next i
|
||||
|
||||
' Write month names
|
||||
Set startCell = ws.Range("B3")
|
||||
' For sales_name = LBound(SALES_ARRAY) To UBound(SALES_ARRAY)
|
||||
for s = 0 to UBound(SALES_ARRAY)
|
||||
sales_name = SALES_ARRAY(s)
|
||||
startCell.Offset(s, 0).Value = new_case_per_agent(sales_name)
|
||||
Next s
|
||||
|
||||
' Write month names
|
||||
Set startCell = ws.Range("C3")
|
||||
' For sales_name = LBound(SALES_ARRAY) To UBound(SALES_ARRAY)
|
||||
for s = 0 to UBound(SALES_ARRAY)
|
||||
sales_name = SALES_ARRAY(s)
|
||||
startCell.Offset(s, 0).Value = collapsed_case_per_agent(sales_name)
|
||||
Next s
|
||||
|
||||
' Write month names
|
||||
Set startCell = ws.Range("D3")
|
||||
' For sales_name = LBound(SALES_ARRAY) To UBound(SALES_ARRAY)
|
||||
for s = 0 to UBound(SALES_ARRAY)
|
||||
sales_name = SALES_ARRAY(s)
|
||||
startCell.Offset(s, 0).Value = case_persistency_by_agent(sales_name)
|
||||
startCell.Offset(s, 0).NumberFormat = "0.00%"
|
||||
Next s
|
||||
|
||||
' Write month names
|
||||
Set startCell = ws.Range("A14")
|
||||
tempArray = Array("Team A total", "Team B Total")
|
||||
For i = LBound(tempArray) To UBound(tempArray)
|
||||
startCell.Offset(i, 0).Value = tempArray(i)
|
||||
Next i
|
||||
|
||||
|
||||
' Write month names
|
||||
Set startCell = ws.Range("B14")
|
||||
tempArray = Array("Team A total", "Team B Total")
|
||||
startCell.Offset(0, 0).Value = new_case_team_a
|
||||
startCell.Offset(1, 0).Value = new_case_team_b
|
||||
|
||||
' Write month names
|
||||
Set startCell = ws.Range("C14")
|
||||
tempArray = Array("Team A total", "Team B Total")
|
||||
startCell.Offset(0, 0).Value = collapsed_case_team_a
|
||||
startCell.Offset(1, 0).Value = collapsed_case_team_b
|
||||
|
||||
' Write month names
|
||||
Set startCell = ws.Range("D14")
|
||||
tempArray = Array("Team A total", "Team B Total")
|
||||
startCell.Offset(0, 0).Value = case_persistency_team_a
|
||||
startCell.Offset(1, 0).Value = case_persistency_team_b
|
||||
startCell.Offset(0, 0).NumberFormat = "0.00%"
|
||||
startCell.Offset(1, 0).NumberFormat = "0.00%"
|
||||
|
||||
|
||||
|
||||
|
||||
End Function
|
||||
|
||||
|
@@ -0,0 +1,77 @@
|
||||
Attribute VB_Name = "ReadAgentSalesWorkbook"
|
||||
|
||||
Function Run(ByRef sPath As String) As Variant
|
||||
Dim wb As Workbook
|
||||
Dim arrResults(999, 20) As Variant
|
||||
Dim row() As Variant
|
||||
Dim i As Integer
|
||||
Dim row_count As Integer
|
||||
Dim firstRow As Boolean
|
||||
Dim rw As Range
|
||||
|
||||
' Try to open the workbook
|
||||
On Error Resume Next
|
||||
Set wb = Workbooks.Open(sPath)
|
||||
On Error GoTo 0
|
||||
|
||||
' Check if the opening was successful
|
||||
If wb Is Nothing Then
|
||||
MsgBox "Could not open workbook at " & sPath, vbCritical, "Error"
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
row_count = 0
|
||||
If Not wb.Sheets("Sheet1") Is Nothing Then
|
||||
' Read data from Sheet1
|
||||
For Each rw In wb.Sheets("Sheet1").UsedRange.Rows
|
||||
' Check if first cell is empty
|
||||
If Trim(rw.Cells(1)) <> "" Then
|
||||
' Print value of first column of current row to immediate window
|
||||
row_count = row_count + 1
|
||||
Else
|
||||
' First cell in current row is empty, so break the loop
|
||||
Exit For
|
||||
End If
|
||||
|
||||
Next rw
|
||||
End If
|
||||
|
||||
' read table content
|
||||
i = 1
|
||||
firstRow = True
|
||||
|
||||
' Look for Sheet1 and stop execution if it doesn't exist
|
||||
If Not wb.Sheets("Sheet1") Is Nothing Then
|
||||
' Read data from Sheet1
|
||||
For Each rw In wb.Sheets("Sheet1").UsedRange.Rows
|
||||
' Check if first cell is empty
|
||||
If (firstRow = False) Then
|
||||
If (Trim(rw.Cells(1)) <> "") Then
|
||||
' Print value of first column of current row to immediate window
|
||||
arrResults(i, 1) = rw.Cells(1).Value
|
||||
arrResults(i, 2) = rw.Cells(2).Value
|
||||
arrResults(i, 3) = rw.Cells(3).Value
|
||||
arrResults(i, 4) = rw.Cells(4).Value
|
||||
arrResults(i, 5) = rw.Cells(5).Value
|
||||
arrResults(i, 6) = rw.Cells(6).Value
|
||||
|
||||
i = i + 1
|
||||
Else
|
||||
' First cell in current row is empty, so break the loop
|
||||
Exit For
|
||||
End If
|
||||
End If
|
||||
firstRow = False
|
||||
|
||||
Next rw
|
||||
|
||||
Else
|
||||
MsgBox """Sheet1"" does not exist in workbook at " & sPath, vbExclamation, "Warning"
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
wb.Close SaveChanges:=False
|
||||
|
||||
Run = Array(arrResults, row_count - 1)
|
||||
End Function
|
||||
|
@@ -0,0 +1,82 @@
|
||||
Attribute VB_Name = "ReadAgentWorkingPerfWorkbook"
|
||||
|
||||
|
||||
Function Run(ByRef sPath As String) As Variant
|
||||
Dim wb As Workbook
|
||||
Dim arrResults(999, 20) As Variant
|
||||
Dim row() As Variant
|
||||
Dim i As Integer
|
||||
Dim row_count As Integer
|
||||
Dim firstRow As Boolean
|
||||
Dim rw As Range
|
||||
|
||||
' Try to open the workbook
|
||||
On Error Resume Next
|
||||
Set wb = Workbooks.Open(sPath)
|
||||
On Error GoTo 0
|
||||
|
||||
' Check if the opening was successful
|
||||
If wb Is Nothing Then
|
||||
MsgBox "Could not open workbook at " & sPath, vbCritical, "Error"
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
row_count = 0
|
||||
If Not wb.Sheets("Sheet1") Is Nothing Then
|
||||
' Read data from Sheet1
|
||||
For Each rw In wb.Sheets("Sheet1").UsedRange.Rows
|
||||
' Check if first cell is empty
|
||||
If Trim(rw.Cells(1)) <> "" Then
|
||||
' Print value of first column of current row to immediate window
|
||||
row_count = row_count + 1
|
||||
Else
|
||||
' First cell in current row is empty, so break the loop
|
||||
Exit For
|
||||
End If
|
||||
|
||||
Next rw
|
||||
End If
|
||||
|
||||
' read table content
|
||||
i = 1
|
||||
firstRow = True
|
||||
|
||||
' Look for Sheet1 and stop execution if it doesn't exist
|
||||
If Not wb.Sheets("Sheet1") Is Nothing Then
|
||||
' Read data from Sheet1
|
||||
For Each rw In wb.Sheets("Sheet1").UsedRange.Rows
|
||||
' Check if first cell is empty
|
||||
If (firstRow = False) Then
|
||||
If (Trim(rw.Cells(1)) <> "") Then
|
||||
' Print value of first column of current row to immediate window
|
||||
arrResults(i, 1) = rw.Cells(1).Value
|
||||
arrResults(i, 2) = rw.Cells(2).Value
|
||||
arrResults(i, 3) = rw.Cells(3).Value
|
||||
arrResults(i, 4) = rw.Cells(4).Value
|
||||
arrResults(i, 5) = rw.Cells(5).Value
|
||||
arrResults(i, 6) = rw.Cells(6).Value
|
||||
|
||||
i = i + 1
|
||||
Else
|
||||
' First cell in current row is empty, so break the loop
|
||||
Exit For
|
||||
End If
|
||||
End If
|
||||
firstRow = False
|
||||
|
||||
Next rw
|
||||
|
||||
Else
|
||||
MsgBox """Sheet1"" does not exist in workbook at " & sPath, vbExclamation, "Warning"
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Debug.Print arrResults(150, 2)
|
||||
|
||||
wb.Close SaveChanges:=False
|
||||
|
||||
Run = Array(arrResults, row_count - 1)
|
||||
End Function
|
||||
|
||||
|
||||
|
@@ -0,0 +1,79 @@
|
||||
Attribute VB_Name = "ReadProductSalesWorkbook"
|
||||
|
||||
Function Run(ByRef sPath As String) As Variant
|
||||
Dim wb As Workbook
|
||||
Dim arrResults(999, 20) As Variant
|
||||
Dim row() As Variant
|
||||
Dim i As Integer
|
||||
Dim row_count As Integer
|
||||
Dim firstRow As Boolean
|
||||
Dim rw As Range
|
||||
|
||||
' Try to open the workbook
|
||||
On Error Resume Next
|
||||
Set wb = Workbooks.Open(sPath)
|
||||
On Error GoTo 0
|
||||
|
||||
' Check if the opening was successful
|
||||
If wb Is Nothing Then
|
||||
MsgBox "Could not open workbook at " & sPath, vbCritical, "Error"
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
row_count = 0
|
||||
If Not wb.Sheets("Sheet1") Is Nothing Then
|
||||
' Read data from Sheet1
|
||||
For Each rw In wb.Sheets("Sheet1").UsedRange.Rows
|
||||
' Check if first cell is empty
|
||||
If Trim(rw.Cells(1)) <> "" Then
|
||||
' Print value of first column of current row to immediate window
|
||||
row_count = row_count + 1
|
||||
Else
|
||||
' First cell in current row is empty, so break the loop
|
||||
Exit For
|
||||
End If
|
||||
|
||||
Next rw
|
||||
End If
|
||||
|
||||
' read table content
|
||||
i = 1
|
||||
firstRow = True
|
||||
|
||||
' Look for Sheet1 and stop execution if it doesn't exist
|
||||
If Not wb.Sheets("Sheet1") Is Nothing Then
|
||||
' Read data from Sheet1
|
||||
For Each rw In wb.Sheets("Sheet1").UsedRange.Rows
|
||||
' Check if first cell is empty
|
||||
If (firstRow = False) Then
|
||||
If (Trim(rw.Cells(1)) <> "") Then
|
||||
' Print value of first column of current row to immediate window
|
||||
arrResults(i, 1) = rw.Cells(1).Value
|
||||
arrResults(i, 2) = rw.Cells(2).Value
|
||||
arrResults(i, 3) = rw.Cells(3).Value
|
||||
arrResults(i, 4) = rw.Cells(4).Value
|
||||
arrResults(i, 5) = rw.Cells(5).Value
|
||||
arrResults(i, 6) = rw.Cells(6).Value
|
||||
|
||||
i = i + 1
|
||||
Else
|
||||
' First cell in current row is empty, so break the loop
|
||||
Exit For
|
||||
End If
|
||||
End If
|
||||
firstRow = False
|
||||
|
||||
Next rw
|
||||
|
||||
Else
|
||||
MsgBox """Sheet1"" does not exist in workbook at " & sPath, vbExclamation, "Warning"
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Debug.Print arrResults(150, 1)
|
||||
|
||||
wb.Close
|
||||
|
||||
Run = Array(arrResults, row_count - 1)
|
||||
End Function
|
||||
|
@@ -0,0 +1,274 @@
|
||||
Attribute VB_Name = "SalesAnalysisMonthlyAndQuart"
|
||||
|
||||
Function CalcSalesAnalysisMonthlyAndQuart(ByVal TablesMeta As Variant) As Variant
|
||||
|
||||
Dim quarter(4) As Double
|
||||
Dim quaterly_margin(4) As Double
|
||||
Dim quaterly_comission(4) As Double
|
||||
Dim quaterly_sales(4) As Double
|
||||
|
||||
Dim calc_monthly_product_category(12) As String
|
||||
Dim monthly_product_category(12) As String
|
||||
|
||||
Dim monthly_selling_unit(12) As Double
|
||||
Dim monthly_sales(12) As Double
|
||||
Dim monthly_margin(12) As Double
|
||||
Dim monthly_commission(12) As Double
|
||||
|
||||
|
||||
' Dim monthly_commission_per_agent(999, 2) As String
|
||||
Dim comission_array_for_sorting(999) As Double
|
||||
Dim monthly_commission_per_agent
|
||||
Set monthly_commission_per_agent = CreateObject("Scripting.Dictionary")
|
||||
|
||||
Dim sales_agent_names
|
||||
Set sales_agent_names = CreateObject("Scripting.Dictionary")
|
||||
|
||||
Dim val_agent_sale_sales_num As String
|
||||
Dim val_agent_sale_date As String
|
||||
Dim val_agent_sale_agent_name As String
|
||||
Dim val_agent_sale_team As String
|
||||
Dim val_agent_sale_selling_price As String
|
||||
Dim val_agent_sale_commision_pct As String
|
||||
|
||||
Dim val_product_sales_sales_num As String
|
||||
Dim val_product_sales_date As String
|
||||
Dim val_product_sales_product_category As String
|
||||
Dim val_product_sales_selling_unit As String
|
||||
Dim val_product_sales_selling_price As String
|
||||
Dim val_product_sales_comission As String
|
||||
Dim val_product_sales_total_comission As String
|
||||
|
||||
' product sales calculation
|
||||
Dim val_product_sales_margin As Double
|
||||
|
||||
|
||||
Dim dt As Date
|
||||
Dim intMonth As Integer
|
||||
Dim intQuarter As Integer
|
||||
|
||||
Dim agent_sales_table As Variant
|
||||
Dim product_sales_table As Variant
|
||||
Dim agent_working_performance As Variant
|
||||
|
||||
agent_sales_table = TablesMeta(0)
|
||||
product_sales_table = TablesMeta(1)
|
||||
agent_working_performance = TablesMeta(2)
|
||||
|
||||
|
||||
Dim agent_sales_table_row As Variant
|
||||
agent_sales_table_rows = agent_sales_table(0)
|
||||
|
||||
Dim product_sales_table_row As Variant
|
||||
product_sales_table_row = product_sales_table(0)
|
||||
|
||||
Dim agent_working_performance_row As Variant
|
||||
agent_working_performance_row = agent_working_performance(0)
|
||||
|
||||
|
||||
Dim top_comission(12, 3) As Double
|
||||
|
||||
|
||||
For i = 1 To agent_sales_table(1)
|
||||
val_agent_sale_date = agent_sales_table_rows(i, 2)
|
||||
|
||||
dt = DateValue(val_agent_sale_date)
|
||||
intMonth = month(dt)
|
||||
intQuarter = Int((intMonth - 1) / 3) + 1
|
||||
|
||||
val_agent_sale_sales_num = agent_sales_table_rows(i, 1)
|
||||
|
||||
val_agent_sale_agent_name = agent_sales_table_rows(i, 3)
|
||||
val_agent_sale_team = agent_sales_table_rows(i, 4)
|
||||
val_agent_sale_selling_price = agent_sales_table_rows(i, 5)
|
||||
val_agent_sale_commision_pct = agent_sales_table_rows(i, 6)
|
||||
|
||||
Next i
|
||||
|
||||
For ps = 1 To product_sales_table(1)
|
||||
val_product_sales_date = product_sales_table_row(ps, 2)
|
||||
|
||||
dt = DateValue(val_product_sales_date)
|
||||
intMonth = month(dt)
|
||||
intQuarter = Int((intMonth - 1) / 3) + 1
|
||||
|
||||
val_agent_sale_agent_name = agent_sales_table_rows(ps, 3)
|
||||
|
||||
val_product_sales_sales_num = product_sales_table_row(ps, 1)
|
||||
val_product_sales_product_category = product_sales_table_row(ps, 3)
|
||||
val_product_sales_selling_unit = product_sales_table_row(ps, 4)
|
||||
val_product_sales_selling_price = product_sales_table_row(ps, 5)
|
||||
|
||||
val_agent_sale_commision_pct = agent_sales_table_rows(ps, 6)
|
||||
' val_product_sales_total_comission = product_sales_table_row(i, 7)
|
||||
|
||||
If InStr(calc_monthly_product_category(intMonth), val_product_sales_product_category) < 1 Then
|
||||
calc_monthly_product_category(intMonth) = calc_monthly_product_category(intMonth) + STRING_SEPERATOR + val_product_sales_product_category
|
||||
End If
|
||||
|
||||
|
||||
'calculate comission
|
||||
val_product_sales_comission = val_product_sales_selling_unit * val_product_sales_selling_price * val_agent_sale_commision_pct
|
||||
product_sales_table_row(ps, 6) = val_product_sales_comission
|
||||
|
||||
' calc margin
|
||||
val_product_sales_margin = val_product_sales_selling_price - val_product_sales_comission
|
||||
product_sales_table_row(ps, 7) = val_product_sales_margin
|
||||
|
||||
'monthly calculation
|
||||
monthly_selling_unit(intMonth) = monthly_selling_unit(intMonth) + val_product_sales_selling_unit
|
||||
monthly_sales(intMonth) = monthly_sales(intMonth) + val_product_sales_selling_price
|
||||
monthly_commission(intMonth) = monthly_commission(intMonth) + val_product_sales_comission
|
||||
monthly_margin(intMonth) = monthly_margin(intMonth) + val_product_sales_margin
|
||||
|
||||
'quartely calculation
|
||||
quaterly_sales(intQuarter) = quaterly_sales(intQuarter) + val_product_sales_comission
|
||||
quaterly_margin(intQuarter) = quaterly_margin(intQuarter) + val_product_sales_margin
|
||||
quaterly_comission(intQuarter) = quaterly_comission(intQuarter) + val_product_sales_comission
|
||||
|
||||
Next ps
|
||||
|
||||
|
||||
' get monthly sales by agent
|
||||
For ps = 1 To product_sales_table(1)
|
||||
Dim temp_key As String
|
||||
temp_key = val_agent_sale_agent_name + "," + CStr(intMonth)
|
||||
|
||||
If (Not (IsEmpty(temp_key)) And Not (monthly_commission_per_agent.exists(temp_key))) Then
|
||||
For j = 1 To 12
|
||||
monthly_commission_per_agent(val_agent_sale_agent_name + "," + CStr(j)) = 0
|
||||
|
||||
Next j
|
||||
End If
|
||||
|
||||
monthly_commission_per_agent(temp_key) = monthly_commission_per_agent(temp_key) + val_product_sales_comission
|
||||
Next ps
|
||||
|
||||
|
||||
' get first 3 per comission per month
|
||||
For m = 1 To 12
|
||||
Dim temp_sort(20) As Double
|
||||
|
||||
|
||||
j = 0
|
||||
For Each agent_name_and_month In monthly_commission_per_agent.Keys
|
||||
If (Split(agent_name_and_month, ",")(1) = CStr(m)) Then
|
||||
temp_sort(j) = monthly_commission_per_agent(agent_name_and_month)
|
||||
j = j + 1
|
||||
End If
|
||||
Next agent_name_and_month
|
||||
|
||||
sortedArray = SortArray(temp_sort)
|
||||
|
||||
top_comission(m, 0) = sortedArray(0)
|
||||
top_comission(m, 1) = sortedArray(1)
|
||||
top_comission(m, 2) = sortedArray(2)
|
||||
|
||||
' total comission
|
||||
product_sales_table_row(m, 7) = product_sales_table_row(m, 6) + bonus
|
||||
Next m
|
||||
|
||||
For m = 1 To 12
|
||||
Dim temp() As String
|
||||
temp = Split(calc_monthly_product_category(m), STRING_SEPERATOR)
|
||||
monthly_product_category(m) = CStr(UBound(temp)) + "(" + Join(temp, ",") + ")"
|
||||
Next m
|
||||
|
||||
|
||||
'get the total commission with bonus
|
||||
|
||||
CalcSalesAnalysisMonthlyAndQuart = Array(monthly_product_category, monthly_selling_unit, monthly_sales, quaterly_sales, monthly_commission, monthly_margin, quaterly_margin, quaterly_comission)
|
||||
End Function
|
||||
|
||||
|
||||
|
||||
|
||||
Function WriteSalesAnalysisMonthlyAndQuart(ByVal calc_result As Variant, ByVal FILE_PATH As String)
|
||||
' monthly_product_category,
|
||||
' monthly_selling_unit,
|
||||
' monthly_sales,
|
||||
' quaterly_sales,
|
||||
' monthly_commission,
|
||||
' monthly_margin,
|
||||
' quaterly_margin,
|
||||
' quaterly_comission
|
||||
|
||||
Const HEADER_ROW As Long = 1
|
||||
Const START_CELL As String = "A2"
|
||||
Dim wb As Workbook
|
||||
Dim ws As Worksheet
|
||||
Dim startCell As Range
|
||||
|
||||
' Open the workbook
|
||||
Set wb = Workbooks.Open(FILE_PATH)
|
||||
|
||||
' Check if the sheet exists
|
||||
On Error Resume Next
|
||||
Set ws = wb.Sheets("Sales Analysis Monthly & Quart")
|
||||
On Error GoTo 0
|
||||
|
||||
' If not found, create a new sheet
|
||||
If ws Is Nothing Then
|
||||
Set ws = wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count))
|
||||
ws.Name = "Sales Analysis Monthly & Quart"
|
||||
Else
|
||||
' Empty the entire sheet
|
||||
ws.UsedRange.ClearContents
|
||||
End If
|
||||
|
||||
' Write headers
|
||||
With ws.Rows(HEADER_ROW)
|
||||
.Cells(1).Value = "Month"
|
||||
.Cells(2).Value = "Quarter"
|
||||
.Cells(3).Value = "Product Category"
|
||||
.Cells(4).Value = "Selling Unit"
|
||||
.Cells(5).Value = "Monthly Sales"
|
||||
.Cells(6).Value = "Quartely Sales"
|
||||
.Cells(7).Value = "Commission"
|
||||
.Cells(8).Value = "Monthly Margin"
|
||||
.Cells(9).Value = "Quaterly Margin"
|
||||
.Cells(10).Value = "Quaterly Commission"
|
||||
End With
|
||||
|
||||
' Write month names
|
||||
Set startCell = ws.Range("A2")
|
||||
Dim monthNames() As Variant
|
||||
monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
|
||||
|
||||
Dim monthly_product_category As Variant
|
||||
monthly_product_category = calc_result(0)
|
||||
monthly_selling_unit = calc_result(1)
|
||||
monthly_sales = calc_result(2)
|
||||
quaterly_sales = calc_result(3)
|
||||
monthly_commission = calc_result(4)
|
||||
monthly_margin = calc_result(5)
|
||||
quaterly_margin = calc_result(6)
|
||||
quaterly_comission = calc_result(7)
|
||||
|
||||
'output content
|
||||
For m = LBound(monthNames) To UBound(monthNames)
|
||||
Dim quarter, month As Integer
|
||||
month = m + 1
|
||||
quarter = GetQuarterFromMonth(month)
|
||||
|
||||
startCell.Offset(m, 0).Value = monthNames(m)
|
||||
startCell.Offset(m, 1).Value = monthly_product_category(month)
|
||||
startCell.Offset(m, 2).Value = monthly_selling_unit(month)
|
||||
startCell.Offset(m, 3).Value = monthly_sales(month)
|
||||
startCell.Offset(m, 4).Value = monthly_sales(month)
|
||||
startCell.Offset(m, 5).Value = quaterly_sales(quarter)
|
||||
startCell.Offset(m, 6).Value = monthly_commission(month)
|
||||
startCell.Offset(m, 7).Value = monthly_margin(month)
|
||||
startCell.Offset(m, 8).Value = quaterly_margin(quarter)
|
||||
startCell.Offset(m, 9).Value = quaterly_comission(quarter)
|
||||
|
||||
|
||||
Next m
|
||||
|
||||
|
||||
' Save and close the workbook
|
||||
' wb.Close SaveChanges:=True
|
||||
End Function
|
||||
|
||||
|
||||
|
@@ -0,0 +1,345 @@
|
||||
Attribute VB_Name = "SalesAnalysisProductCategory"
|
||||
|
||||
Function CalcSalesAnalysisFileSalesAmount(ByVal TablesMeta As Variant) As Variant
|
||||
Dim agent_sales_table As Variant
|
||||
Dim product_sales_meta As Variant
|
||||
Dim product_sales_table As Variant
|
||||
Dim ps_rows As Double
|
||||
Dim agent_working_performance As Variant
|
||||
|
||||
' agent_sales_table = TablesMeta(0)
|
||||
' agent_working_performance = TablesMeta(2)
|
||||
|
||||
Dim VehicleInsuranceByMonth(12) As Double
|
||||
Dim TravelInsuranceByMonth(12) As Double
|
||||
Dim LifeInsuranceByMonth(12) As Double
|
||||
Dim HealthInsuranceByMonth(12) As Double
|
||||
Dim AccidentInsuranceByMonth(12) As Double
|
||||
Dim MonthlyTotal(12) As Double
|
||||
Dim QuartelyTotal(4) As Double
|
||||
|
||||
product_sales_table = TablesMeta(1)
|
||||
Dim product_sales_table_row As Variant
|
||||
product_sales_table_row = product_sales_table(0)
|
||||
|
||||
Dim val_agent_sale_sales_num As String
|
||||
Dim val_agent_sale_date As String
|
||||
Dim val_agent_sale_agent_name As String
|
||||
Dim val_agent_sale_team As String
|
||||
Dim val_agent_sale_selling_price As String
|
||||
Dim val_agent_sale_commision_pct As String
|
||||
|
||||
Dim val_product_sales_sales_num As String
|
||||
Dim val_product_sales_date As String
|
||||
Dim val_product_sales_product_category As String
|
||||
Dim val_product_sales_selling_unit As String
|
||||
Dim val_product_sales_selling_price As String
|
||||
Dim val_product_sales_comission As String
|
||||
Dim val_product_sales_total_comission As String
|
||||
|
||||
Dim subColumn() As Variant
|
||||
subColumn = Array("Travel insurance", "Health insurance", "Life insurance", "Vehicle insurance", "Accident insurance", "Monthly Total", "Quaterly Total")
|
||||
|
||||
For ps = 1 To product_sales_table(1)
|
||||
val_product_sales_date = product_sales_table_row(ps, 2)
|
||||
val_product_sales_selling_price = product_sales_table_row(ps, 5)
|
||||
|
||||
dt = DateValue(val_product_sales_date)
|
||||
intMonth = month(dt)
|
||||
intQuarter = Int((intMonth - 1) / 3) + 1
|
||||
|
||||
val_product_sales_product_category = product_sales_table_row(ps, 3)
|
||||
|
||||
If val_product_sales_product_category = "Travel insurance" Then
|
||||
TravelInsuranceByMonth(intMonth) = TravelInsuranceByMonth(intMonth) + val_product_sales_selling_price
|
||||
End If
|
||||
If val_product_sales_product_category = "Health insurance" Then
|
||||
HealthInsuranceByMonth(intMonth) = HealthInsuranceByMonth(intMonth) + val_product_sales_selling_price
|
||||
End If
|
||||
If val_product_sales_product_category = "Life insurance" Then
|
||||
LifeInsuranceByMonth(intMonth) = LifeInsuranceByMonth(intMonth) + val_product_sales_selling_price
|
||||
End If
|
||||
If val_product_sales_product_category = "Vehicle insurance" Then
|
||||
VehicleInsuranceByMonth(intMonth) = VehicleInsuranceByMonth(intMonth) + val_product_sales_selling_price
|
||||
End If
|
||||
If val_product_sales_product_category = "Accident insurance" Then
|
||||
AccidentInsuranceByMonth(intMonth) = AccidentInsuranceByMonth(intMonth) + val_product_sales_selling_price
|
||||
End If
|
||||
|
||||
MonthlyTotal(intMonth) = MonthlyTotal(intMonth) + val_product_sales_selling_price
|
||||
QuartelyTotal(intQuarter) = QuartelyTotal(intQuarter) + val_product_sales_selling_price
|
||||
Next ps
|
||||
|
||||
CalcSalesAnalysisFileSalesAmount = Array(TravelInsuranceByMonth, HealthInsuranceByMonth, LifeInsuranceByMonth, VehicleInsuranceByMonth, AccidentInsuranceByMonth, MonthlyTotal, QuartelyTotal)
|
||||
|
||||
End Function
|
||||
|
||||
|
||||
Function CalcSalesAnalysisFileSalesUnit(ByVal TablesMeta As Variant) As Variant
|
||||
Dim agent_sales_table As Variant
|
||||
Dim product_sales_meta As Variant
|
||||
Dim product_sales_table As Variant
|
||||
Dim ps_rows As Double
|
||||
Dim agent_working_performance As Variant
|
||||
|
||||
' agent_sales_table = TablesMeta(0)
|
||||
' agent_working_performance = TablesMeta(2)
|
||||
|
||||
Dim VehicleInsuranceByMonth(12) As Double
|
||||
Dim TravelInsuranceByMonth(12) As Double
|
||||
Dim LifeInsuranceByMonth(12) As Double
|
||||
Dim HealthInsuranceByMonth(12) As Double
|
||||
Dim AccidentInsuranceByMonth(12) As Double
|
||||
Dim MonthlyTotal(12) As Double
|
||||
Dim QuartelyTotal(4) As Double
|
||||
|
||||
product_sales_table = TablesMeta(1)
|
||||
Dim product_sales_table_row As Variant
|
||||
product_sales_table_row = product_sales_table(0)
|
||||
|
||||
Dim val_agent_sale_sales_num As String
|
||||
Dim val_agent_sale_date As String
|
||||
Dim val_agent_sale_agent_name As String
|
||||
Dim val_agent_sale_team As String
|
||||
Dim val_agent_sale_selling_price As String
|
||||
Dim val_agent_sale_commision_pct As String
|
||||
|
||||
Dim val_product_sales_sales_num As String
|
||||
Dim val_product_sales_date As String
|
||||
Dim val_product_sales_product_category As String
|
||||
Dim val_product_sales_selling_unit As String
|
||||
Dim val_product_sales_comission As String
|
||||
Dim val_product_sales_total_comission As String
|
||||
|
||||
Dim subColumn() As Variant
|
||||
subColumn = Array("Travel insurance", "Health insurance", "Life insurance", "Vehicle insurance", "Accident insurance", "Monthly Total", "Quaterly Total")
|
||||
|
||||
For ps = 1 To product_sales_table(1)
|
||||
val_product_sales_date = product_sales_table_row(ps, 2)
|
||||
val_product_sales_selling_unit = product_sales_table_row(ps, 4)
|
||||
|
||||
dt = DateValue(val_product_sales_date)
|
||||
intMonth = month(dt)
|
||||
intQuarter = Int((intMonth - 1) / 3) + 1
|
||||
|
||||
val_product_sales_product_category = product_sales_table_row(ps, 3)
|
||||
|
||||
If val_product_sales_product_category = "Travel insurance" Then
|
||||
TravelInsuranceByMonth(intMonth) = TravelInsuranceByMonth(intMonth) + val_product_sales_selling_unit
|
||||
End If
|
||||
If val_product_sales_product_category = "Health insurance" Then
|
||||
HealthInsuranceByMonth(intMonth) = HealthInsuranceByMonth(intMonth) + val_product_sales_selling_unit
|
||||
End If
|
||||
If val_product_sales_product_category = "Life insurance" Then
|
||||
LifeInsuranceByMonth(intMonth) = LifeInsuranceByMonth(intMonth) + val_product_sales_selling_unit
|
||||
End If
|
||||
If val_product_sales_product_category = "Vehicle insurance" Then
|
||||
VehicleInsuranceByMonth(intMonth) = VehicleInsuranceByMonth(intMonth) + val_product_sales_selling_unit
|
||||
End If
|
||||
If val_product_sales_product_category = "Accident insurance" Then
|
||||
AccidentInsuranceByMonth(intMonth) = AccidentInsuranceByMonth(intMonth) + val_product_sales_selling_unit
|
||||
End If
|
||||
|
||||
MonthlyTotal(intMonth) = MonthlyTotal(intMonth) + val_product_sales_selling_unit
|
||||
QuartelyTotal(intQuarter) = QuartelyTotal(intQuarter) + val_product_sales_selling_unit
|
||||
Next ps
|
||||
|
||||
CalcSalesAnalysisFileSalesUnit = Array(TravelInsuranceByMonth, HealthInsuranceByMonth, LifeInsuranceByMonth, VehicleInsuranceByMonth, AccidentInsuranceByMonth, MonthlyTotal, QuartelyTotal)
|
||||
|
||||
End Function
|
||||
|
||||
|
||||
Function WriteSalesAnalysisFileSalesAmount(ByVal calc_result As Variant, ByVal FILE_PATH As String)
|
||||
' TravelInsuranceByMonth,
|
||||
' HealthInsuranceByMonth,
|
||||
' LifeInsuranceByMonth,
|
||||
' VehicleInsuranceByMonth,
|
||||
' AccidentInsuranceByMonth,
|
||||
' MonthlyTotal,
|
||||
' QuartelyTotal
|
||||
|
||||
Dim TravelInsuranceByMonth As Variant
|
||||
Dim HealthInsuranceByMonth As Variant
|
||||
Dim LifeInsuranceByMonth As Variant
|
||||
Dim VehicleInsuranceByMonth As Variant
|
||||
Dim AccidentInsuranceByMonth As Variant
|
||||
Dim MonthlyTotal As Variant
|
||||
Dim QuartelyTotal As Variant
|
||||
|
||||
TravelInsuranceByMonth = calc_result(0)
|
||||
HealthInsuranceByMonth = calc_result(1)
|
||||
LifeInsuranceByMonth = calc_result(2)
|
||||
VehicleInsuranceByMonth = calc_result(3)
|
||||
AccidentInsuranceByMonth = calc_result(4)
|
||||
MonthlyTotal = calc_result(5)
|
||||
QuartelyTotal = calc_result(6)
|
||||
|
||||
Const HEADER_ROW As Long = 1
|
||||
Const START_CELL As String = "A2"
|
||||
Dim wb As Workbook
|
||||
Dim ws As Worksheet
|
||||
Dim startCell As Range
|
||||
|
||||
' Open the workbook
|
||||
Set wb = Workbooks.Open(FILE_PATH)
|
||||
|
||||
' Check if the sheet exists
|
||||
On Error Resume Next
|
||||
Set ws = wb.Sheets("Sales Analysis Product Category")
|
||||
On Error GoTo 0
|
||||
|
||||
' If not found, create a new sheet
|
||||
If ws Is Nothing Then
|
||||
Set ws = wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count))
|
||||
ws.Name = "Sales Analysis Product Category"
|
||||
Else
|
||||
' Empty the entire sheet
|
||||
ws.UsedRange.ClearContents
|
||||
End If
|
||||
|
||||
' Write headers
|
||||
With ws.Rows(HEADER_ROW)
|
||||
.Cells(1).Value = "Sales Amount"
|
||||
.Cells(3).Value = "Month"
|
||||
End With
|
||||
|
||||
' Write month names
|
||||
Set startCell = ws.Range("C2")
|
||||
Dim monthNames() As Variant
|
||||
monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
|
||||
Dim i As Long
|
||||
For i = LBound(monthNames) To UBound(monthNames)
|
||||
startCell.Offset(0, i).Value = monthNames(i)
|
||||
Next i
|
||||
|
||||
' Write headers
|
||||
With ws.Rows(3)
|
||||
.Cells(1).Value = "Product Category"
|
||||
End With
|
||||
|
||||
' Write sub-column names
|
||||
Set startCell = ws.Range("B3")
|
||||
Dim subColumn() As Variant
|
||||
subColumn = Array("Travel insurance", "Health insurance", "Life insurance", "Vehicle insurance", "Accident insurance", "Monthly Total", "Quaterly Total")
|
||||
|
||||
For sc = LBound(subColumn) To UBound(subColumn)
|
||||
startCell.Offset(sc, 0).Value = subColumn(sc)
|
||||
Next sc
|
||||
|
||||
Set startCell = ws.Range("C3")
|
||||
For m = LBound(monthNames) To UBound(monthNames)
|
||||
Dim int_quarter, int_month As Integer
|
||||
int_month = m + 1
|
||||
int_quarter = GetQuarterFromMonth(int_month)
|
||||
|
||||
startCell.Offset(0, m).Value = TravelInsuranceByMonth(int_month)
|
||||
startCell.Offset(1, m).Value = HealthInsuranceByMonth(int_month)
|
||||
startCell.Offset(2, m).Value = LifeInsuranceByMonth(int_month)
|
||||
startCell.Offset(3, m).Value = VehicleInsuranceByMonth(int_month)
|
||||
startCell.Offset(4, m).Value = AccidentInsuranceByMonth(int_month)
|
||||
startCell.Offset(5, m).Value = MonthlyTotal(int_month)
|
||||
startCell.Offset(6, m).Value = QuartelyTotal(int_quarter)
|
||||
|
||||
Next m
|
||||
|
||||
' Save and close the workbook
|
||||
'wb.Close SaveChanges:=True
|
||||
End Function
|
||||
|
||||
Function WriteSalesAnalysisFileSalesUnit(ByVal calc_result As Variant, ByVal FILE_PATH As String)
|
||||
' TravelInsuranceByMonth,
|
||||
' HealthInsuranceByMonth,
|
||||
' LifeInsuranceByMonth,
|
||||
' VehicleInsuranceByMonth,
|
||||
' AccidentInsuranceByMonth,
|
||||
' MonthlyTotal,
|
||||
' QuartelyTotal
|
||||
|
||||
Dim TravelInsuranceByMonth As Variant
|
||||
Dim HealthInsuranceByMonth As Variant
|
||||
Dim LifeInsuranceByMonth As Variant
|
||||
Dim VehicleInsuranceByMonth As Variant
|
||||
Dim AccidentInsuranceByMonth As Variant
|
||||
Dim MonthlyTotal As Variant
|
||||
Dim QuartelyTotal As Variant
|
||||
|
||||
TravelInsuranceByMonth = calc_result(0)
|
||||
HealthInsuranceByMonth = calc_result(1)
|
||||
LifeInsuranceByMonth = calc_result(2)
|
||||
VehicleInsuranceByMonth = calc_result(3)
|
||||
AccidentInsuranceByMonth = calc_result(4)
|
||||
MonthlyTotal = calc_result(5)
|
||||
QuartelyTotal = calc_result(6)
|
||||
|
||||
Const TOP_ROW As Long = 10
|
||||
Const START_CELL As String = "A2"
|
||||
Dim wb As Workbook
|
||||
Dim ws As Worksheet
|
||||
Dim startCell As Range
|
||||
|
||||
' Open the workbook
|
||||
Set wb = Workbooks.Open(FILE_PATH)
|
||||
|
||||
' Check if the sheet exists
|
||||
On Error Resume Next
|
||||
Set ws = wb.Sheets("Sales Analysis Product Category")
|
||||
On Error GoTo 0
|
||||
|
||||
' If not found, create a new sheet
|
||||
If ws Is Nothing Then
|
||||
Set ws = wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count))
|
||||
ws.Name = "Sales Analysis Product Category"
|
||||
Else
|
||||
' Empty the entire sheet
|
||||
'ws.UsedRange.ClearContents
|
||||
End If
|
||||
|
||||
' Write headers
|
||||
With ws.Rows(TOP_ROW)
|
||||
.Cells(1).Value = "Sales Unit"
|
||||
.Cells(3).Value = "Month"
|
||||
End With
|
||||
|
||||
' Write month names
|
||||
Set startCell = ws.Range("C11")
|
||||
Dim monthNames() As Variant
|
||||
monthNames = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
|
||||
Dim i As Long
|
||||
For i = LBound(monthNames) To UBound(monthNames)
|
||||
startCell.Offset(0, i).Value = monthNames(i)
|
||||
Next i
|
||||
|
||||
' Write headers
|
||||
With ws.Rows(12)
|
||||
.Cells(1).Value = "Product Category"
|
||||
End With
|
||||
|
||||
' Write sub-column names
|
||||
Set startCell = ws.Range("B12")
|
||||
Dim subColumn() As Variant
|
||||
subColumn = Array("Travel insurance", "Health insurance", "Life insurance", "Vehicle insurance", "Accident insurance", "Monthly Total", "Quaterly Total")
|
||||
For sc = LBound(subColumn) To UBound(subColumn)
|
||||
startCell.Offset(sc, 0).Value = subColumn(sc)
|
||||
Next sc
|
||||
|
||||
Set startCell = ws.Range("C12")
|
||||
For m = LBound(monthNames) To UBound(monthNames)
|
||||
Dim int_quarter, int_month As Integer
|
||||
int_month = m + 1
|
||||
int_quarter = GetQuarterFromMonth(int_month)
|
||||
|
||||
startCell.Offset(0, m).Value = TravelInsuranceByMonth(int_month)
|
||||
startCell.Offset(1, m).Value = HealthInsuranceByMonth(int_month)
|
||||
startCell.Offset(2, m).Value = LifeInsuranceByMonth(int_month)
|
||||
startCell.Offset(3, m).Value = VehicleInsuranceByMonth(int_month)
|
||||
startCell.Offset(4, m).Value = AccidentInsuranceByMonth(int_month)
|
||||
startCell.Offset(5, m).Value = MonthlyTotal(int_month)
|
||||
startCell.Offset(6, m).Value = QuartelyTotal(int_quarter)
|
||||
|
||||
Next m
|
||||
|
||||
|
||||
' Save and close the workbook
|
||||
'wb.Close SaveChanges:=True
|
||||
End Function
|
||||
|
3
jimmycheung93/task4/_poc/main_xlsm/dev.bat
Normal file
3
jimmycheung93/task4/_poc/main_xlsm/dev.bat
Normal file
@@ -0,0 +1,3 @@
|
||||
rmdir *.bas
|
||||
|
||||
xlwings.exe vba edit --file .\main.xlsm
|
BIN
jimmycheung93/task4/_poc/main_xlsm/helloworld.xlsx
Normal file
BIN
jimmycheung93/task4/_poc/main_xlsm/helloworld.xlsx
Normal file
Binary file not shown.
BIN
jimmycheung93/task4/_poc/main_xlsm/helloworld_graph.xlsm
Normal file
BIN
jimmycheung93/task4/_poc/main_xlsm/helloworld_graph.xlsm
Normal file
Binary file not shown.
BIN
jimmycheung93/task4/_poc/main_xlsm/main.xlsm
Normal file
BIN
jimmycheung93/task4/_poc/main_xlsm/main.xlsm
Normal file
Binary file not shown.
Reference in New Issue
Block a user