Attribute VB_Name = "SAMonthAndQuart" 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, ByRef wb As Workbook) Application.DisplayAlerts = False Const HEADER_ROW As Long = 1 Const START_CELL As String = "A2" 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 Dim monthly_sales 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 Range("A1:J1").Interior.Color = RGB(252, 229, 205) Dim column_used As Variant column_used = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J") For c = LBound(column_used) To UBound(column_used) Columns(column_used(c)).EntireColumn.AutoFit Next c 'rgb(255, 242, 204) Range("A5:J7").Interior.Color = RGB(255, 242, 204) Range("A11:J13").Interior.Color = RGB(255, 242, 204) Dim columns_need_merge As Variant columns_need_merge = Array("F", "I", "J") For c = LBound(columns_need_merge) To UBound(columns_need_merge) Dim column As String column = columns_need_merge(c) With Range(column & "2:" & column & "4") .Merge ' merges cells F5, F6, and F7 into a single cell .VerticalAlignment = xlVAlignCenter ' centering the text vertically within the merged cell .HorizontalAlignment = xlHAlignRight ' centering the text horizontally across the merged cell End With With Range(column & "5:" & column & "7") .Merge ' merges cells F5, F6, and F7 into a single cell .VerticalAlignment = xlVAlignCenter ' centering the text vertically within the merged cell .HorizontalAlignment = xlHAlignRight ' centering the text horizontally across the merged cell End With With Range(column & "8:" & column & "10") .Merge ' merges cells F5, F6, and F7 into a single cell .VerticalAlignment = xlVAlignCenter ' centering the text vertically within the merged cell .HorizontalAlignment = xlHAlignRight ' centering the text horizontally across the merged cell End With With Range(column & "11:" & column & "13") .Merge ' merges cells F5, F6, and F7 into a single cell .VerticalAlignment = xlVAlignCenter ' centering the text vertically within the merged cell .HorizontalAlignment = xlHAlignRight ' centering the text horizontally across the merged cell End With Next c ' Save and close the workbook ' wb.Close SaveChanges:=True End Function