update,
This commit is contained in:
227
jimmycheung93/task4/1/SAProdCatGraph.bas
Normal file
227
jimmycheung93/task4/1/SAProdCatGraph.bas
Normal file
@@ -0,0 +1,227 @@
|
||||
Attribute VB_Name = "SAProdCatGraph"
|
||||
|
||||
Function SalesAmountTable(ByVal calc_result As Variant, ByVal file_path As String, ByVal int_month As Integer)
|
||||
|
||||
Const START_CELL As String = "A1"
|
||||
Dim wb As Workbook
|
||||
Dim ws As Worksheet
|
||||
Dim startCell As Range
|
||||
|
||||
Dim row As Integer
|
||||
|
||||
' Load sales amount by month for different types of insurance
|
||||
Dim TravelInsuranceByMonth() As Variant
|
||||
Dim HealthInsuranceByMonth() As Variant
|
||||
Dim LifeInsuranceByMonth() As Variant
|
||||
Dim VehicleInsuranceByMonth() As Variant
|
||||
Dim AccidentInsuranceByMonth() As Variant
|
||||
|
||||
TravelInsuranceByMonth = calc_result(0)
|
||||
HealthInsuranceByMonth = calc_result(1)
|
||||
LifeInsuranceByMonth = calc_result(2)
|
||||
VehicleInsuranceByMonth = calc_result(3)
|
||||
AccidentInsuranceByMonth = calc_result(4)
|
||||
|
||||
' Open the workbook
|
||||
Set wb = Workbooks.Open(file_path)
|
||||
Set ws = wb.Sheets("Sales Analysis Product Category")
|
||||
|
||||
Dim InstType() As Variant
|
||||
InstType = Array("Accident insurance", "Vehicle insurance", "Life insurance", "Health insurance", "Travel insurance")
|
||||
|
||||
' Write headers
|
||||
Set startCell = ws.Range(START_CELL)
|
||||
|
||||
'generate graph by month
|
||||
For m = LBound(MONTH_NAMES) To UBound(MONTH_NAMES)
|
||||
month_name = MONTH_NAMES(m)
|
||||
|
||||
row = 0
|
||||
startCell.Offset(row, 0 + (m * 3)).Value = "Sales unit of " & month_name
|
||||
|
||||
row = row + 1
|
||||
startCell.Offset(row, 0 + (m * 3)).Value = "Type"
|
||||
startCell.Offset(row, 1 + (m * 3)).Value = "Sales"
|
||||
|
||||
row = row + 1
|
||||
startCell.Offset(row, 0 + (m * 3)).Value = "Accident insurance"
|
||||
startCell.Offset(row, 1 + (m * 3)).Value = AccidentInsuranceByMonth(m + 1)
|
||||
|
||||
row = row + 1
|
||||
startCell.Offset(row, 0 + (m * 3)).Value = "Vehicle insurance"
|
||||
startCell.Offset(row, 1 + (m * 3)).Value = VehicleInsuranceByMonth(m + 1)
|
||||
|
||||
row = row + 1
|
||||
startCell.Offset(row, 0 + (m * 3)).Value = "Life insurance"
|
||||
startCell.Offset(row, 1 + (m * 3)).Value = LifeInsuranceByMonth(m + 1)
|
||||
|
||||
row = row + 1
|
||||
startCell.Offset(row, 0 + (m * 3)).Value = "Health insurance"
|
||||
startCell.Offset(row, 1 + (m * 3)).Value = HealthInsuranceByMonth(m + 1)
|
||||
|
||||
row = row + 1
|
||||
startCell.Offset(row, 0 + (m * 3)).Value = "Travel insurance"
|
||||
startCell.Offset(row, 1 + (m * 3)).Value = TravelInsuranceByMonth(m + 1)
|
||||
|
||||
Next m
|
||||
|
||||
End Function
|
||||
|
||||
|
||||
Function SalesAmountGraph(ByVal calc_result As Variant, ByVal file_path As String, ByVal int_month As Integer)
|
||||
Dim wb As Workbook
|
||||
Dim ws As Worksheet
|
||||
|
||||
' Define the top and left positions for each graph
|
||||
Dim YEAR_GRAPHS_TOP As Variant
|
||||
Dim YEAR_GRAPHS_LEFT As Variant
|
||||
YEAR_GRAPHS_TOP = Array(0, 0, 0, 300, 300, 300, 600, 600, 600, 900, 900, 900)
|
||||
YEAR_GRAPHS_LEFT = Array(0, 400, 800, 0, 400, 800, 0, 400, 800, 0, 400, 800)
|
||||
|
||||
' Define the range for data of each graph
|
||||
Dim DATA_RANGES As Variant
|
||||
DATA_RANGES = Array("A2:B7", "D2:E7", "G2:H7", "J2:K7", "M2:N7", "P2:Q7", "S2:T7", "V2:W7", "Y2:Z7", "AB2:AC7", "AE2:AF7", "AH2:AI7")
|
||||
|
||||
Set wb = Workbooks.Open(file_path)
|
||||
Set ws = wb.Sheets("Sales Analysis Product Category")
|
||||
|
||||
'generate graph by month
|
||||
For m = LBound(MONTH_NAMES) To UBound(MONTH_NAMES)
|
||||
month_name = MONTH_NAMES(m)
|
||||
data_range = DATA_RANGES(m)
|
||||
|
||||
' insert graph
|
||||
ActiveSheet.Shapes.AddChart2(251, xlPie).Select
|
||||
ActiveChart.SetSourceData Source:=Range(data_range)
|
||||
|
||||
ActiveChart.SetElement (msoElementLegendNone)
|
||||
ActiveChart.SetElement (msoElementDataLabelCallout)
|
||||
ActiveChart.ChartTitle.Select
|
||||
ActiveChart.ChartTitle.Text = "Sales amount of " & month_name
|
||||
Selection.Format.TextFrame2.TextRange.Characters.Text = "Sales amount of " & month_name
|
||||
|
||||
Set cho = ws.ChartObjects(ws.ChartObjects.Count)
|
||||
Set cht = cho.Chart
|
||||
cho.Top = YEAR_GRAPHS_TOP(m)
|
||||
cho.Left = YEAR_GRAPHS_LEFT(m)
|
||||
cho.Width = 400
|
||||
cho.Height = 300
|
||||
|
||||
Next m
|
||||
|
||||
End Function
|
||||
|
||||
|
||||
Function SalesUnitTable(ByVal calc_result As Variant, ByVal file_path As String, ByVal int_month As Integer)
|
||||
Const START_CELL As String = "A10"
|
||||
Dim wb As Workbook
|
||||
Dim ws As Worksheet
|
||||
Dim startCell As Range
|
||||
|
||||
Dim row As Integer
|
||||
|
||||
Dim TravelInsuranceByMonth As Variant
|
||||
Dim HealthInsuranceByMonth As Variant
|
||||
Dim LifeInsuranceByMonth As Variant
|
||||
Dim VehicleInsuranceByMonth As Variant
|
||||
Dim AccidentInsuranceByMonth As Variant
|
||||
|
||||
TravelInsuranceByMonth = calc_result(0)
|
||||
HealthInsuranceByMonth = calc_result(1)
|
||||
LifeInsuranceByMonth = calc_result(2)
|
||||
VehicleInsuranceByMonth = calc_result(3)
|
||||
AccidentInsuranceByMonth = calc_result(4)
|
||||
|
||||
' Open the workbook
|
||||
Set wb = Workbooks.Open(file_path)
|
||||
Set ws = wb.Sheets("Sales Analysis Product Category")
|
||||
|
||||
Dim InstType() As Variant
|
||||
InstType = Array("Accident insurance", "Vehicle insurance", "Life insurance", "Health insurance", "Travel insurance")
|
||||
|
||||
' Write headers
|
||||
Set startCell = ws.Range(START_CELL)
|
||||
|
||||
'generate graph by month
|
||||
For m = LBound(MONTH_NAMES) To UBound(MONTH_NAMES)
|
||||
month_name = MONTH_NAMES(m)
|
||||
|
||||
row = 0
|
||||
startCell.Offset(row, 0 + (m * 3)).Value = "Sales amount of " & month_name
|
||||
|
||||
row = row + 1
|
||||
startCell.Offset(row, 0 + (m * 3)).Value = "Type"
|
||||
startCell.Offset(row, 1 + (m * 3)).Value = "Sales"
|
||||
|
||||
row = row + 1
|
||||
startCell.Offset(row, 0 + (m * 3)).Value = "Accident insurance"
|
||||
startCell.Offset(row, 1 + (m * 3)).Value = AccidentInsuranceByMonth(m + 1)
|
||||
|
||||
row = row + 1
|
||||
startCell.Offset(row, 0 + (m * 3)).Value = "Vehicle insurance"
|
||||
startCell.Offset(row, 1 + (m * 3)).Value = VehicleInsuranceByMonth(m + 1)
|
||||
|
||||
row = row + 1
|
||||
startCell.Offset(row, 0 + (m * 3)).Value = "Life insurance"
|
||||
startCell.Offset(row, 1 + (m * 3)).Value = LifeInsuranceByMonth(m + 1)
|
||||
|
||||
row = row + 1
|
||||
startCell.Offset(row, 0 + (m * 3)).Value = "Health insurance"
|
||||
startCell.Offset(row, 1 + (m * 3)).Value = HealthInsuranceByMonth(m + 1)
|
||||
|
||||
row = row + 1
|
||||
startCell.Offset(row, 0 + (m * 3)).Value = "Travel insurance"
|
||||
startCell.Offset(row, 1 + (m * 3)).Value = TravelInsuranceByMonth(m + 1)
|
||||
|
||||
Next m
|
||||
|
||||
End Function
|
||||
|
||||
|
||||
Function SalesUnitGraph(ByVal calc_result As Variant, ByVal file_path As String, ByVal int_month As Integer)
|
||||
Dim wb As Workbook
|
||||
Dim ws As Worksheet
|
||||
|
||||
Dim YEAR_GRAPHS_TOP As Variant
|
||||
Dim YEAR_GRAPHS_LEFT As Variant
|
||||
YEAR_GRAPHS_TOP = Array(1200, 1200, 1200, 1200 + 300, 1200 + 300, 1200 + 300, 1200 + 600, 1200 + 600, 1200 + 600, 1200 + 900, 1200 + 900, 1200 + 900)
|
||||
YEAR_GRAPHS_LEFT = Array(0, 400, 800, 0, 400, 800, 0, 400, 800, 0, 400, 800)
|
||||
|
||||
Dim DATA_RANGES As Variant
|
||||
DATA_RANGES = Array("A11:B16", "D11:E16", "G11:H16", "J11:K16", "M11:N16", "P11:Q16", "S11:T16", "V11:W16", "Y11:Z16", "AB11:AC16", "AE11:AF16", "AH11:AI16")
|
||||
|
||||
Set wb = Workbooks.Open(file_path)
|
||||
Set ws = wb.Sheets("Sales Analysis Product Category")
|
||||
|
||||
'generate graph by month
|
||||
For m = LBound(MONTH_NAMES) To UBound(MONTH_NAMES)
|
||||
month_name = MONTH_NAMES(m)
|
||||
data_range = DATA_RANGES(m)
|
||||
|
||||
' insert graph
|
||||
ActiveSheet.Shapes.AddChart2(251, xlPie).Select
|
||||
ActiveChart.SetSourceData Source:=Range(data_range)
|
||||
|
||||
ActiveChart.SetElement (msoElementLegendNone)
|
||||
ActiveChart.SetElement (msoElementDataLabelCallout)
|
||||
ActiveChart.ChartTitle.Text = "Sales unit of " & month_name
|
||||
Selection.Format.TextFrame2.TextRange.Characters.Text = "Sales unit of " & month_name
|
||||
|
||||
Set cho = ws.ChartObjects(ws.ChartObjects.Count)
|
||||
Set cht = cho.Chart
|
||||
cho.Top = YEAR_GRAPHS_TOP(m)
|
||||
cho.Left = YEAR_GRAPHS_LEFT(m)
|
||||
cho.Width = 400
|
||||
cho.Height = 300
|
||||
|
||||
Next m
|
||||
|
||||
End Function
|
||||
|
||||
|
||||
Function Helloworld()
|
||||
Debug.Print "helloworld Sales_Analysis_Product_Category"
|
||||
End Function
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user