cache refactor
This commit is contained in:
@@ -69,36 +69,61 @@ Function GetLastDataRow(ByVal ws As Worksheet, ByVal columnNum As Long) As Long
|
||||
GetLastDataRow = ws.Cells(ws.Rows.Count, columnNum).End(xlUp).Row
|
||||
End Function
|
||||
|
||||
Function Contains(ByVal arr As Variant, ByVal value As String) As Boolean
|
||||
Dim i As Long
|
||||
For i = 0 To UBound(arr)
|
||||
If arr(i) = value Then
|
||||
Contains = True
|
||||
Exit Function
|
||||
End If
|
||||
Next i
|
||||
|
||||
Contains = False
|
||||
End Function
|
||||
|
||||
' @return dict : key = keyCol,value = Array
|
||||
' @param sheetName
|
||||
' @param keyCol
|
||||
' @param valueCols Array(4,5,6)
|
||||
' @param startRow default is 7
|
||||
Function LoadLookup( _
|
||||
ByVal sheetName As String, _
|
||||
ByVal keyCol As Long, _
|
||||
ByVal valueCols As Variant, _
|
||||
Optional ByVal startRow As Long = 7 _
|
||||
) As Object
|
||||
|
||||
Function LoadLookup(ByVal sheetName As String, ByVal cacheName As String) As Object
|
||||
On Error GoTo ErrHandler
|
||||
|
||||
' --- validate ---
|
||||
If Trim(sheetName) = "" Then Exit Function
|
||||
If Not IsArray(valueCols) Then
|
||||
valueCols = Array(valueCols)
|
||||
If Trim(sheetName) = "" Then Err.Raise 0001, "LoadLookup", "Sheet name cannot be empty."
|
||||
|
||||
Dim sheetConfDict As Object: Set sheetConfDict = GetSheetConfig()
|
||||
If Not sheetConfDict.Exists(sheetName) Then
|
||||
Err.Raise 1004, "LoadLookup", "Sheet not configured: " & sheetName
|
||||
End If
|
||||
Dim nValCols As Long: nValCols = UBound(valueCols) - LBound(valueCols) + 1
|
||||
If nValCols = 0 Then Exit Function
|
||||
|
||||
' --- obtain worksheet ---
|
||||
On Error Resume Next
|
||||
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets(sheetName)
|
||||
On Error GoTo 0
|
||||
If ws Is Nothing Then Exit Function
|
||||
|
||||
' --- obtain data(based on keyCol)---
|
||||
Dim lastRow As Long: lastRow = ws.Cells(ws.Rows.Count, keyCol).End(xlUp).Row
|
||||
If lastRow < startRow Then Exit Function
|
||||
If ws Is Nothing Then Err.Raise 0003, "LoadLookup", "Worksheet named '" & sheetName & "' not found."
|
||||
On Error GoTo ErrHandler
|
||||
|
||||
Dim sheetConf As Object: Set sheetConf = sheetConfDict(cacheName)
|
||||
Dim startRow As Long: startRow = sheetConf("StartRow")
|
||||
Dim keyCol As Long: keyCol = sheetConf("KeyCol")
|
||||
Dim valueCols As Variant: valueCols = sheetConf("ValueCols")
|
||||
|
||||
Dim lastRow As Long
|
||||
If sheetName <> cacheName Then
|
||||
lastRow = ws.Cells(ws.Rows.Count, keyCol).End(xlUp).Row
|
||||
Else
|
||||
lastRow = GetLastDataRowInRange(ws)
|
||||
End If
|
||||
|
||||
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
|
||||
If lastRow < startRow Then
|
||||
Set LoadLookup = dict
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Dim nValCols As Long: nValCols = UBound(valueCols) - LBound(valueCols) + 1
|
||||
If nValCols = 0 Then Err.Raise 0002, "LoadLookup", "Value columns parameter is invalid."
|
||||
|
||||
' --- prepare col ---
|
||||
Dim minCol As Long: minCol = keyCol
|
||||
Dim maxCol As Long: maxCol = keyCol
|
||||
@@ -133,7 +158,6 @@ Function LoadLookup( _
|
||||
Next i
|
||||
|
||||
' --- write into ---
|
||||
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
|
||||
dict.CompareMode = vbTextCompare
|
||||
|
||||
Dim r As Long
|
||||
@@ -150,6 +174,10 @@ Function LoadLookup( _
|
||||
Next r
|
||||
|
||||
Set LoadLookup = dict
|
||||
Exit Function
|
||||
|
||||
ErrHandler:
|
||||
Err.Raise Err.Number, Err.Source, Err.Description
|
||||
End Function
|
||||
|
||||
' obtain
|
||||
@@ -205,7 +233,6 @@ Sub ClearDataRows(ByVal ws As Worksheet)
|
||||
|
||||
Dim sheetConf As Object: Set sheetConf = sheetConfDict(ws.CodeName)
|
||||
|
||||
'
|
||||
Dim startRow As Long: startRow = sheetConf("StartRow")
|
||||
Dim startCol As String: startCol = sheetConf("StartCol")
|
||||
Dim endCol As String: endCol = sheetConf("EndCol")
|
||||
@@ -320,8 +347,27 @@ Public Function FormatDateInput(ByVal inputStr As String) As String
|
||||
End If
|
||||
End Function
|
||||
|
||||
Function CheckHeaderEdit(ByVal ws As Worksheet, ByVal Target As Range) As Boolean
|
||||
Dim sheetConfDict As Object: Set sheetConfDict = GetSheetConfig()
|
||||
Dim sheetConf As Object: Set sheetConf = sheetConfDict(ws.CodeName)
|
||||
Dim headerRow As Long: headerRow = sheetConf("HeaderRow")
|
||||
|
||||
' Check header row (headerRow) cannot be edited
|
||||
If Target.Row = headerRow Then
|
||||
Application.EnableEvents = False
|
||||
MsgBox "Header row can not be edit", vbExclamation
|
||||
Application.Undo
|
||||
Application.EnableEvents = True
|
||||
|
||||
CheckHeaderEdit = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
CheckHeaderEdit = False
|
||||
End Function
|
||||
|
||||
Function GetErrorMsg(ByVal errorCode As String, Optional ByVal param0 As String = "", Optional ByVal param1 As String = "") As String
|
||||
Dim errorList As Object: Set errorList = GetErrorList()
|
||||
Dim errorList As Object: Set errorList = GetCache("errorList")
|
||||
Dim errorMessage As String
|
||||
If errorList.Exists(errorCode) Then
|
||||
errorMessage = Replace(errorList(errorCode)(0), "{0}", param0)
|
||||
|
||||
Reference in New Issue
Block a user