update,
This commit is contained in:
382
task4/1/SAProductCategory.bas
Normal file
382
task4/1/SAProductCategory.bas
Normal file
@@ -0,0 +1,382 @@
|
||||
Attribute VB_Name = "SAProductCategory"
|
||||
|
||||
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, ByRef wb As Workbook)
|
||||
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 ws As Worksheet
|
||||
Dim startCell As Range
|
||||
|
||||
' 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, ByRef wb As Workbook)
|
||||
Application.DisplayAlerts = False
|
||||
|
||||
' Variables to hold the result of the calculation
|
||||
' The calculation results are stored in arrays
|
||||
' Each array holds the sales unit for a specific product category by month
|
||||
' e.g. TravelInsuranceByMonth(1) = Sales unit of travel insurance in January
|
||||
Dim TravelInsuranceByMonth() As Variant ' Sales unit of travel insurance by month
|
||||
Dim HealthInsuranceByMonth() As Variant ' Sales unit of health insurance by month
|
||||
Dim LifeInsuranceByMonth() As Variant ' Sales unit of life insurance by month
|
||||
Dim VehicleInsuranceByMonth() As Variant ' Sales unit of vehicle insurance by month
|
||||
Dim AccidentInsuranceByMonth() As Variant ' Sales unit of accident insurance by month
|
||||
Dim MonthlyTotal() As Variant ' Sales unit of each product category by month (total)
|
||||
Dim QuartelyTotal() As Variant ' Sales unit of each product category by quarter (total)
|
||||
|
||||
TravelInsuranceByMonth = calc_result(0) ' Sales unit of travel insurance by month
|
||||
HealthInsuranceByMonth = calc_result(1) ' Sales unit of health insurance by month
|
||||
LifeInsuranceByMonth = calc_result(2) ' Sales unit of life insurance by month
|
||||
VehicleInsuranceByMonth = calc_result(3) ' Sales unit of vehicle insurance by month
|
||||
AccidentInsuranceByMonth = calc_result(4) ' Sales unit of accident insurance by month
|
||||
MonthlyTotal = calc_result(5) ' Sales unit of each product category by month (total)
|
||||
QuartelyTotal = calc_result(6) ' Sales unit of each product category by quarter (total)
|
||||
|
||||
Const TOP_ROW As Long = 10
|
||||
Const START_CELL As String = "A2"
|
||||
Dim ws As Worksheet
|
||||
Dim startCell As Range
|
||||
|
||||
' 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
|
||||
|
||||
|
||||
For i = 0 To 100
|
||||
Range("A1").Offset(0, i).EntireColumn.AutoFit
|
||||
Next i
|
||||
|
||||
For i = 2 To 14
|
||||
Range("A1").Offset(0, i).EntireColumn.ColumnWidth = 10
|
||||
Next i
|
||||
|
||||
Dim ranges_need_merge As Variant
|
||||
ranges_need_merge = Array("C1:N1", "A3:A7", "C9:E9", "F9:H9", "I9:K9", "L9:N9")
|
||||
For r = LBound(ranges_need_merge) To UBound(ranges_need_merge)
|
||||
range_need_merge = ranges_need_merge(r)
|
||||
With Range(range_need_merge)
|
||||
.Merge
|
||||
.VerticalAlignment = xlVAlignCenter
|
||||
.HorizontalAlignment = xlHAlignCenter
|
||||
End With
|
||||
Next r
|
||||
|
||||
Dim ranges_need_paint As Variant
|
||||
ranges_need_paint = Array("A1", "C2:N2", "A3:A7", "B3:B8")
|
||||
For r = LBound(ranges_need_paint) To UBound(ranges_need_paint)
|
||||
range_need_merge = ranges_need_paint(r)
|
||||
With Range(range_need_merge)
|
||||
.Interior.Color = RGB(182, 215, 168)
|
||||
End With
|
||||
Next r
|
||||
|
||||
Range("B9").Interior.Color = RGB(0, 255, 0)
|
||||
|
||||
ranges_need_merge = Array("C10:N10", "A12:A16", "C18:E18", "F18:H18", "I18:K18", "L18:N18")
|
||||
For r = LBound(ranges_need_merge) To UBound(ranges_need_merge)
|
||||
range_need_merge = ranges_need_merge(r)
|
||||
With Range(range_need_merge)
|
||||
.Merge
|
||||
.VerticalAlignment = xlVAlignCenter
|
||||
.HorizontalAlignment = xlHAlignCenter
|
||||
End With
|
||||
Next r
|
||||
|
||||
' Set background color for specific ranges
|
||||
ranges_need_paint = Array("A10", "C11:N11", "A12:A16", "B12:B17")
|
||||
For r = LBound(ranges_need_paint) To UBound(ranges_need_paint)
|
||||
range_need_merge = ranges_need_paint(r)
|
||||
With Range(range_need_merge)
|
||||
.Interior.Color = RGB(164, 194, 244)
|
||||
End With
|
||||
Next r
|
||||
|
||||
' Highlight total row
|
||||
Range("B18").Interior.Color = RGB(0, 255, 255)
|
||||
|
||||
End Function
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user