update,
This commit is contained in:
204
jimmycheung93/task4/1/awp_xlsx_case_persistency_pivot.bas
Normal file
204
jimmycheung93/task4/1/awp_xlsx_case_persistency_pivot.bas
Normal file
@@ -0,0 +1,204 @@
|
||||
Attribute VB_Name = "awp_xlsx_case_persistency_pivot"
|
||||
|
||||
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
|
||||
|
||||
' 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 CountFigures(ws As Worksheet, ByVal start_row As Integer, ByVal last_row As Integer)
|
||||
Dim i, j As Integer
|
||||
|
||||
' variable for result
|
||||
Dim agent_month_new_case, agent_month_collapsed_case
|
||||
Set agent_month_new_case = CreateObject("Scripting.Dictionary")
|
||||
Set agent_month_collapsed_case = CreateObject("Scripting.Dictionary")
|
||||
|
||||
Dim agent_quarter_new_case, agent_quarter_collapsed_case
|
||||
Set agent_quarter_new_case = CreateObject("Scripting.Dictionary")
|
||||
Set agent_quarter_collapsed_case = CreateObject("Scripting.Dictionary")
|
||||
|
||||
Dim agent_month_case_persistency, agent_quarter_case_persistency
|
||||
Set agent_month_case_persistency = CreateObject("Scripting.Dictionary")
|
||||
Set agent_quarter_case_persistency = CreateObject("Scripting.Dictionary")
|
||||
|
||||
For i = start_row To last_row
|
||||
Dim date_value As String
|
||||
Dim int_month, quarter As Integer
|
||||
Dim agent_name, new_case, collapsed_case, agent_team As String
|
||||
Dim agent_quarter_key, agent_month_key, temp_key As String
|
||||
|
||||
date_value = ReadCellValue("A" & CStr(i))
|
||||
int_month = GetMonthFrDate(date_value)
|
||||
quarter = GetQuarterFrDate(date_value)
|
||||
|
||||
agent_name = ReadCellValue("B" & CStr(i))
|
||||
agent_team = ReadCellValue("C" & CStr(i))
|
||||
new_case = ReadCellValue("D" & CStr(i))
|
||||
collapsed_case = ReadCellValue("E" & CStr(i))
|
||||
|
||||
agent_month_key = agent_name + "," + CStr(int_month)
|
||||
agent_quarter_key = agent_name + "," + CStr(quarter)
|
||||
|
||||
' create if not found agent_name
|
||||
If (Not (IsEmpty(agent_month_key)) And Not (agent_month_new_case.exists(agent_month_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
|
||||
agent_month_case_persistency(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
|
||||
agent_quarter_case_persistency(agent_name + "," + CStr(j)) = 0
|
||||
Next j
|
||||
End If
|
||||
|
||||
If (new_case <> "") Then
|
||||
agent_month_new_case(agent_month_key) = agent_month_new_case(agent_month_key) + CInt(new_case)
|
||||
agent_quarter_new_case(agent_quarter_key) = agent_quarter_new_case(agent_quarter_key) + CInt(new_case)
|
||||
End If
|
||||
|
||||
If (collapsed_case <> "") Then
|
||||
agent_month_collapsed_case(agent_month_key) = agent_month_collapsed_case(agent_month_key) + CInt(collapsed_case)
|
||||
agent_quarter_collapsed_case(agent_quarter_key) = agent_quarter_collapsed_case(agent_quarter_key) + CInt(collapsed_case)
|
||||
End If
|
||||
|
||||
If (agent_month_new_case(agent_month_key) > 0) Then
|
||||
agent_month_case_persistency(agent_month_key) = agent_month_new_case(agent_month_key) / (agent_month_new_case(agent_month_key) + agent_month_collapsed_case(agent_quarter_key))
|
||||
Else
|
||||
agent_month_case_persistency(agent_month_key) = 0
|
||||
End If
|
||||
|
||||
If (agent_quarter_case_persistency(agent_month_key) > 0) Then
|
||||
agent_quarter_case_persistency(agent_quarter_key) = agent_quarter_new_case(agent_quarter_key) / (agent_quarter_new_case(agent_quarter_key) + agent_quarter_collapsed_case(agent_quarter_key))
|
||||
Else
|
||||
agent_quarter_case_persistency(agent_month_key) = 0
|
||||
End If
|
||||
|
||||
Next i
|
||||
|
||||
CountFigures = Array(agent_month_new_case, agent_month_collapsed_case, _
|
||||
agent_quarter_new_case, agent_quarter_collapsed_case, _
|
||||
agent_month_case_persistency, agent_quarter_case_persistency)
|
||||
|
||||
End Function
|
||||
|
||||
Function Run(ws As Worksheet)
|
||||
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)
|
||||
|
||||
Run = CountFigures(ws, start_row, last_row)
|
||||
End Function
|
||||
|
||||
' canned method to open excel file
|
||||
Function OpenFile(sPath As String)
|
||||
Dim wb As Workbook
|
||||
Set wb = Workbooks.Open(sPath)
|
||||
' wb.Windows(1).Visible = False
|
||||
|
||||
Dim count_figures_result As Variant
|
||||
|
||||
If Not (wb.Sheets("Sheet1") Is Nothing) Then
|
||||
count_figures_result = awp_xlsx_case_persistency_pivot.Run(wb.Sheets("Sheet1"))
|
||||
End If
|
||||
|
||||
OpenFile = count_figures_result
|
||||
|
||||
wb.Close savechanges:=False
|
||||
End Function
|
||||
|
||||
Sub Test()
|
||||
Dim result As Variant
|
||||
Dim agent_month_new_case, agent_month_collapsed_case, agent_quarter_new_case, agent_quarter_collapsed_case, agent_month_case_persistency, agent_quarter_case_persistency
|
||||
result = awp_xlsx_case_persistency_pivot.OpenFile("c:\Temp\xlsx\Agent_Working_Performance.xlsx")
|
||||
|
||||
Set agent_month_new_case = result(0)
|
||||
Set agent_month_collapsed_case = result(1)
|
||||
Set agent_quarter_new_case = result(2)
|
||||
Set agent_quarter_collapsed_case = result(3)
|
||||
Set agent_month_case_persistency = result(4)
|
||||
Set agent_quarter_case_persistency = result(5)
|
||||
|
||||
Debug.Print agent_month_case_persistency("Eason,1")
|
||||
Debug.Print agent_month_case_persistency("Eason,2")
|
||||
|
||||
Debug.Print "done"
|
||||
End Sub
|
||||
|
||||
Sub Helloworld()
|
||||
Debug.Print "helloworld"
|
||||
End Sub
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user