Files
004_comission/jimmycheung93/task4/1/AgentWorkingPerformancePivot.bas
louiscklaw 2e592cb561 update,
2025-01-31 21:36:48 +08:00

192 lines
5.4 KiB
QBasic

Attribute VB_Name = "AgentWorkingPerformancePivot"
Option Explicit
' get the last row for a given xls sheet
Function getLastRow(ByVal start_row As Integer)
Dim last_row As Boolean
Dim last_check_row As Integer
Dim scan_row As Integer
Dim i As Integer
Dim cell_value_1 As String
Dim next_row_1 As Integer
scan_row = start_row
last_check_row = 9999
For i = start_row To last_check_row
scan_row = i
last_row = True
next_row_1 = i + 1
cell_value_1 = ReadCellValue("A" & CStr(next_row_1))
If (cell_value_1 <> "") Then
last_row = False
End If
If (last_row = True) Then
'Debug.Print "last row found"
Exit For
End If
Next i
getLastRow = scan_row
End Function
' get the value from cell for a given address
Function ReadCellValue(cell_addr As String)
ReadCellValue = Worksheets("Sheet1").Range(cell_addr).Value
End Function
Function CountFigures(ws As Worksheet, ByVal start_row As Integer, ByVal last_row As Integer)
Dim SalesNewCaseByMonth As Integer
Dim SalesCollapsedCaseByMonth As Integer
Dim i, j As Integer
' variable for result
Dim agent_month_new_case
Set agent_month_new_case = CreateObject("Scripting.Dictionary")
Dim agent_month_collapsed_case
Set agent_month_collapsed_case = CreateObject("Scripting.Dictionary")
Dim agent_quarter_new_case
Set agent_quarter_new_case = CreateObject("Scripting.Dictionary")
Dim agent_quarter_collapsed_case
Set agent_quarter_collapsed_case = CreateObject("Scripting.Dictionary")
For i = start_row To last_row
Dim date_value As String
Dim month, quarter As Integer
Dim agent_name, new_case, collapsed_case As String
Dim agent_quarter_key, temp_key As String
date_value = ReadCellValue("A" & CStr(i))
month = GetMonthFrDate(date_value)
quarter = GetQuarterFrDate(date_value)
agent_name = ReadCellValue("B" & CStr(i))
new_case = ReadCellValue("D" & CStr(i))
collapsed_case = ReadCellValue("E" & CStr(i))
temp_key = agent_name + "," + CStr(month)
agent_quarter_key = agent_name + "," + CStr(quarter)
' create if not found agent_name
If (Not (IsEmpty(temp_key)) And Not (agent_month_new_case.exists(temp_key))) Then
For j = 1 To 12
agent_month_new_case(agent_name + "," + CStr(j)) = 0
agent_month_collapsed_case(agent_name + "," + CStr(j)) = 0
Next j
For j = 1 To 4
agent_quarter_new_case(agent_name + "," + CStr(j)) = 0
agent_quarter_collapsed_case(agent_name + "," + CStr(j)) = 0
Next j
End If
If (new_case <> "") Then
agent_month_new_case(temp_key) = agent_month_new_case(temp_key) + CInt(new_case)
End If
If (collapsed_case <> "") Then
agent_month_collapsed_case(temp_key) = agent_month_collapsed_case(temp_key) + CInt(collapsed_case)
End If
If (new_case <> "") Then
agent_quarter_new_case(agent_quarter_key) = agent_quarter_new_case(agent_quarter_key) + CInt(new_case)
End If
If (collapsed_case <> "") Then
agent_quarter_collapsed_case(agent_quarter_key) = agent_quarter_collapsed_case(agent_quarter_key) + CInt(collapsed_case)
End If
Next i
' sales_index
' sales_month_new_case
' sales_month_collapsed_case
' sales_quarter_new_case
' sales_quarter_collapsed_case
CountFigures = Array(agent_month_new_case, agent_month_collapsed_case, agent_quarter_new_case, agent_quarter_collapsed_case)
End Function
' get the integer month from date
Function GetMonthFrDate(ByVal date_string As String) As Integer
Dim intMonth As Integer
Dim dt As String
dt = DateValue(date_string)
intMonth = month(dt)
GetMonthFrDate = intMonth
End Function
' get the integer quarter from date
Function GetQuarterFrDate(ByVal date_string As String) As Integer
Dim intQuarter As Integer
Dim intMonth As Integer
Dim dt As String
dt = DateValue(date_string)
intMonth = month(dt)
GetQuarterFrDate = Int((intMonth - 1) / 3) + 1
End Function
Function Run(ws As Worksheet)
' Dim ws As Worksheet
' Set ws = ThisWorkbook.Sheets("Sheet1")
Dim row_count As Integer
Dim rw As Variant
Dim count_figures_result As Variant
Dim start_row, last_row As Integer
start_row = 2
last_row = getLastRow(start_row)
count_figures_result = CountFigures(ws, start_row, last_row)
Dim agent_month_new_case, agent_month_collapsed_case
Dim agent_quarter_new_case, agent_quarter_collapsed_case
Set agent_month_new_case = count_figures_result(0)
Set agent_month_collapsed_case = count_figures_result(1)
Set agent_quarter_new_case = count_figures_result(2)
Set agent_quarter_collapsed_case = count_figures_result(3)
Debug.Print agent_month_new_case("Candy,1")
Run = count_figures_result
Debug.Print "done"
End Function
' canned method to open excel file
Function OpenFile(sPath As String)
Dim wb As Workbook
Set wb = Workbooks.Open(sPath)
Dim count_figures_result As Variant
If Not (wb.Sheets("Sheet1") Is Nothing) Then
count_figures_result = Run(wb.Sheets("Sheet1"))
End If
OpenFile = count_figures_result
wb.Close savechanges:=False
End Function
Sub Helloworld()
Debug.Print "helloworld"
End Sub