refactor
This commit is contained in:
@@ -0,0 +1,359 @@
|
||||
Attribute VB_Name = "Common_Button"
|
||||
Option Explicit
|
||||
' ============================================================
|
||||
' Module Name: Common_Button
|
||||
' Module Desc: Common_Button
|
||||
' Module Methods:
|
||||
' - CSV_Import_Button
|
||||
' ============================================================
|
||||
Sub CSV_Import_Button()
|
||||
DO_CSV_Import ActiveSheet
|
||||
End Sub
|
||||
|
||||
Sub Validation_Button()
|
||||
Do_Validation ActiveSheet
|
||||
End Sub
|
||||
|
||||
Sub CSV_Export_Button()
|
||||
DO_CSV_Export ActiveSheet
|
||||
End Sub
|
||||
|
||||
Sub Sort_Button()
|
||||
Do_Sort ActiveSheet
|
||||
End Sub
|
||||
|
||||
Sub Filter_Button()
|
||||
Do_Filter ActiveSheet
|
||||
End Sub
|
||||
|
||||
Sub Fit_Button()
|
||||
Do_Fit ActiveSheet
|
||||
End Sub
|
||||
|
||||
Sub RefreshCache_Button()
|
||||
Dim cacheSheets As Variant: cacheSheets = Array("M1", "M2", "Z1", "Z2", "Z3", "Z4", "T1", "T2", "T3", "O1","O2")
|
||||
|
||||
Dim sheetName As Variant
|
||||
Dim ws As Worksheet
|
||||
For Each sheetName In cacheSheets
|
||||
If ProcedureExists(sheetName, "Validate") Then
|
||||
Dim errorCount As Long
|
||||
On Error Resume Next
|
||||
Set ws = ThisWorkbook.Worksheets(CStr(sheetName))
|
||||
On Error GoTo 0
|
||||
Dim isValid As Boolean: isValid = RunValidationSilent(ws, errorCount)
|
||||
If isValid = False Then
|
||||
MsgBox "Can't refresh " & sheetName & " cache. Validation error occurs."
|
||||
Exit Sub
|
||||
End If
|
||||
End If
|
||||
Next sheetName
|
||||
|
||||
Dim result As Boolean: result = RefreshAllCache()
|
||||
If result = True Then
|
||||
MsgBox "master data reload successfully."
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Private Sub DO_CSV_Import(ws As Excel.Worksheet)
|
||||
On Error GoTo ImportError
|
||||
|
||||
' Step 1: get csv encoding
|
||||
Dim sheetConfDict As Object: Set sheetConfDict = GetSheetConfig()
|
||||
Dim cfg As Object: Set cfg = sheetConfDict(ws.CodeName)
|
||||
Dim expectedColumnCount As Long: expectedColumnCount = cfg("ExpectedColumnCount")
|
||||
|
||||
' Step 2: Select CSV file
|
||||
Dim filePath As String: filePath = SelectCSVFile()
|
||||
If filePath = "" Then Exit Sub
|
||||
|
||||
' Step 3: Read CSV and return 2D array
|
||||
Dim csvData As Variant: csvData = ReadCSVAs2DArrayStrict(filePath, expectedColumnCount, cfg("CSV_Encoding"), cfg("HasHeader"))
|
||||
|
||||
If Not IsArray(csvData) Then
|
||||
MsgBox "No valid data returned from CSV.", vbExclamation
|
||||
GoTo FinallyExit
|
||||
End If
|
||||
|
||||
If UBound(csvData, 1) < 1 Then
|
||||
MsgBox "No data in CSV.", vbExclamation
|
||||
GoTo FinallyExit
|
||||
End If
|
||||
|
||||
' === Step 3:Clear all data rows before import ===
|
||||
Application.ScreenUpdating = False
|
||||
Application.EnableEvents = False
|
||||
Call ClearDataRows(ws)
|
||||
|
||||
' === Step 4: Write CSV data to worksheet ===
|
||||
Dim colLetters As Variant: colLetters = cfg("HeaderColumns")
|
||||
Dim writeRow As Long: writeRow = cfg("StartRow")
|
||||
Dim i As Long
|
||||
' loop row
|
||||
For i = LBound(csvData, 1) To UBound(csvData, 1)
|
||||
Dim j As Long
|
||||
' loop column
|
||||
For j = 0 To expectedColumnCount - 1
|
||||
ws.Cells(writeRow, ws.Range(colLetters(j) & "1").Column).Value = CleanCSVField(CStr(csvData(i, j + 1)))
|
||||
Next j
|
||||
writeRow = writeRow + 1
|
||||
Next i
|
||||
|
||||
' === Step 5: Trigger sheet-specific import handler ===
|
||||
If ProcedureExists(ws.CodeName, "ImportCSVAndTriggerChange") Then
|
||||
Call Application.Run(ws.CodeName & ".ImportCSVAndTriggerChange", ws, writeRow)
|
||||
End If
|
||||
|
||||
MsgBox writeRow - cfg("StartRow") & " rows imported.", vbInformation
|
||||
GoTo FinallyExit
|
||||
|
||||
ImportError:
|
||||
MsgBox "CSV import failed: " & Err.Description, vbExclamation
|
||||
|
||||
FinallyExit:
|
||||
Application.EnableEvents = True
|
||||
Application.ScreenUpdating = True
|
||||
End Sub
|
||||
|
||||
'
|
||||
Private Sub Do_Validation(ws As Excel.Worksheet)
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
' step1. confirm Validate Sub
|
||||
If Not ProcedureExists(ws.CodeName, "Validate") Then
|
||||
MsgBox "worksheet """ & ws.name & """ unimplement methods", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Dim errorCount As Long
|
||||
Dim isValid As Boolean: isValid = RunValidationSilent(ws, errorCount)
|
||||
|
||||
If errorCount = -1 Then
|
||||
MsgBox "worksheet """ & ws.Name & """ unimplement methods", vbExclamation
|
||||
ElseIf errorCount = -2 Then
|
||||
MsgBox "Validation error occurred.", vbCritical
|
||||
ElseIf errorCount > 0 Then
|
||||
MsgBox "Validation complete. Errors: " & errorCount, vbInformation
|
||||
Else
|
||||
If ws.CodeName <> "C1" Then
|
||||
RefreshCache(ws.CodeName)
|
||||
End If
|
||||
MsgBox "Validation complete. Errors: 0", vbInformation
|
||||
End If
|
||||
|
||||
Exit Sub
|
||||
|
||||
ErrorHandler:
|
||||
MsgBox "Error: " & Err.Description, vbCritical
|
||||
Exit Sub
|
||||
End Sub
|
||||
|
||||
'
|
||||
Private Sub DO_CSV_Export(ws As Excel.Worksheet)
|
||||
On Error GoTo ExportError
|
||||
|
||||
Dim sheetConfDict As Object: Set sheetConfDict = GetSheetConfig()
|
||||
Dim sheetConf As Object: Set sheetConf = sheetConfDict(ws.CodeName)
|
||||
|
||||
Dim lastDataRow As Long: lastDataRow = GetLastDataRowInRange(ws)
|
||||
Dim startRow As Long: startRow = sheetConf("StartRow")
|
||||
|
||||
If lastDataRow < startRow Then
|
||||
MsgBox "No data rows to output.", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
' === Step 1: Validate all rows before export ===
|
||||
' Do_Validation
|
||||
Dim errorCount As Long
|
||||
If Not RunValidationSilent(ws, errorCount) Then
|
||||
If errorCount > 0 Then
|
||||
MsgBox "Validation failed (" & errorCount & " errors). Export aborted.", vbCritical
|
||||
Exit Sub
|
||||
Else
|
||||
MsgBox "Validation setup error. Export aborted.", vbCritical
|
||||
Exit Sub
|
||||
End If
|
||||
End If
|
||||
|
||||
' === Step 2: Select save path ===
|
||||
Dim savePath As String: savePath = GetSaveCSVPath()
|
||||
If savePath = "" Then Exit Sub
|
||||
|
||||
' === Step 3: Count data rows ===
|
||||
Dim rowCount As Long: rowCount = lastDataRow - startRow + 1
|
||||
|
||||
' === Step 4: Count data columns ===
|
||||
Dim expectedColumnCount As Long: expectedColumnCount = sheetConf("ExpectedColumnCount")
|
||||
|
||||
' === Step 5: check export csv has header ===
|
||||
Dim hasHeader As Boolean: hasHeader = sheetConf("HasHeader")
|
||||
Dim dataRow As Long: dataRow = 1
|
||||
Dim outputArr As Variant
|
||||
|
||||
' when has header + 1
|
||||
If hasHeader Then
|
||||
ReDim outputArr(1 To rowCount + 1, 1 To expectedColumnCount)
|
||||
Else
|
||||
ReDim outputArr(1 To rowCount, 1 To expectedColumnCount)
|
||||
End If
|
||||
|
||||
' === Step 6: Build array with header and data ===
|
||||
If hasHeader Then
|
||||
Dim headerArr As Variant
|
||||
headerArr = GetCSVHeader(ws)
|
||||
|
||||
Dim colIdx As Long
|
||||
For colIdx = 0 To expectedColumnCount - 1
|
||||
outputArr(1, colIdx + 1) = headerArr(1, colIdx + 1)
|
||||
Next colIdx
|
||||
dataRow = dataRow + 1
|
||||
End If
|
||||
|
||||
Dim colLetters As Variant: colLetters = sheetConf("HeaderColumns")
|
||||
Dim r As Long
|
||||
For r = startRow To lastDataRow
|
||||
For colIdx = 0 To expectedColumnCount - 1
|
||||
outputArr(dataRow, colIdx + 1) = CleanCSVField(ws.Cells(r, Columns(colLetters(colIdx)).Column).Value)
|
||||
Next colIdx
|
||||
dataRow = dataRow + 1
|
||||
Next r
|
||||
|
||||
On Error GoTo ExportError
|
||||
Call WriteCSVFromArray(savePath, outputArr, sheetConf("CSV_Encoding"), sheetConf("AlwaysQuote"))
|
||||
On Error GoTo 0
|
||||
|
||||
MsgBox "CSV export completed. Total data rows: " & rowCount, vbInformation
|
||||
Exit Sub
|
||||
|
||||
ExportError:
|
||||
MsgBox "CSV export failed: " & Err.Description, vbExclamation
|
||||
End Sub
|
||||
|
||||
Private Sub Do_Sort(ws As Excel.Worksheet)
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim sheetConfDict As Object: Set sheetConfDict = GetSheetConfig()
|
||||
Dim sheetConf As Object: Set sheetConf = sheetConfDict(ws.CodeName)
|
||||
|
||||
Dim startCol As String: startCol = sheetConf("StartCol")
|
||||
Dim endCol As String: endCol = sheetConf("EndCol")
|
||||
Dim startRow As Long: startRow = sheetConf("StartRow")
|
||||
Dim lastDataRow As Long: lastDataRow = GetLastDataRowInRange(ws)
|
||||
|
||||
If lastDataRow < startRow Then
|
||||
MsgBox "No data to sort.", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Dim sortRange As Range: Set sortRange = ws.Range(ws.Cells(startRow, ws.Range(startCol & "1").Column), ws.Cells(lastDataRow, ws.Range(endCol & "1").Column))
|
||||
sortRange.Select
|
||||
|
||||
' Show sort dialog
|
||||
Application.Dialogs(xlDialogSort).Show
|
||||
Exit Sub
|
||||
|
||||
ErrorHandler:
|
||||
MsgBox "Error: " & Err.Description, vbCritical
|
||||
End Sub
|
||||
|
||||
Private Sub Do_Filter(ws As Excel.Worksheet)
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim sheetConfDict As Object: Set sheetConfDict = GetSheetConfig()
|
||||
Dim sheetConf As Object: Set sheetConf = sheetConfDict(ws.CodeName)
|
||||
|
||||
' Check if auto filter is already on
|
||||
If ws.AutoFilterMode Then
|
||||
ws.AutoFilterMode = False
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Dim startCol As Long: startCol = ws.Range(sheetConf("StartCol") & "1").Column
|
||||
Dim endCol As Long: endCol = ws.Range(sheetConf("EndCol") & "1").Column
|
||||
|
||||
Dim filterRow As Long: filterRow = sheetConf("FilterRow")
|
||||
|
||||
Dim filterRange As Range: Set filterRange = ws.Range(ws.Cells(filterRow, startCol), ws.Cells(filterRow, endCol))
|
||||
filterRange.AutoFilter
|
||||
Exit Sub
|
||||
|
||||
ErrorHandler:
|
||||
MsgBox "Error: " & Err.Description, vbCritical
|
||||
End Sub
|
||||
|
||||
Private Sub Do_Fit(ws As Excel.Worksheet)
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim sheetConfDict As Object: Set sheetConfDict = GetSheetConfig()
|
||||
Dim sheetConf As Object: Set sheetConf = sheetConfDict(ws.CodeName)
|
||||
|
||||
Dim startCol As String: startCol = sheetConf("StartCol")
|
||||
Dim endCol As String: endCol = sheetConf("EndCol")
|
||||
|
||||
ws.Columns(startCol & ":" & endCol).AutoFit
|
||||
Exit Sub
|
||||
|
||||
ErrorHandler:
|
||||
MsgBox "Error: " & Err.Description, vbCritical
|
||||
End Sub
|
||||
|
||||
' RunValidationSilent
|
||||
Public Function RunValidationSilent(ws As Worksheet, Optional ByRef errorCountOut As Long = 0) As Boolean
|
||||
On Error GoTo ErrorHandler
|
||||
|
||||
Dim sheetConfDict As Object: Set sheetConfDict = GetSheetConfig()
|
||||
Dim sheetConf As Object: Set sheetConf = sheetConfDict(ws.CodeName)
|
||||
|
||||
' check Validate method exist
|
||||
If Not ProcedureExists(ws.CodeName, "Validate") Then
|
||||
errorCountOut = -1
|
||||
RunValidationSilent = False
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Dim validate As String: validate = ws.CodeName & ".Validate"
|
||||
Dim lastDataRow As Long: lastDataRow = GetLastDataRowInRange(ws)
|
||||
Dim startRow As Long: startRow = sheetConf("StartRow")
|
||||
Dim errorCol As Long: errorCol = ws.Range(sheetConf("ErrorCol") & "1").Column
|
||||
|
||||
If lastDataRow < startRow Then
|
||||
errorCountOut = 0
|
||||
RunValidationSilent = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Dim r As Long
|
||||
errorCountOut = 0
|
||||
For r = startRow To lastDataRow
|
||||
Application.Run validate, ws, r, lastDataRow
|
||||
Dim errorMessage As String : errorMessage = Trim(ws.Cells(r, errorCol).Value)
|
||||
Dim errorCode As String: errorCode = GetCode(errorMessage)
|
||||
If errorCode <> "W001" And errorCode <> "" Then
|
||||
errorCountOut = errorCountOut + 1
|
||||
End If
|
||||
Next r
|
||||
|
||||
RunValidationSilent = (errorCountOut = 0)
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
errorCountOut = -2
|
||||
RunValidationSilent = False
|
||||
End Function
|
||||
|
||||
Public Function ProcedureExists(ByVal moduleName As String, ByVal procName As String) As Boolean
|
||||
Dim VBProj As Object, VBComp As Object, CodeMod As Object
|
||||
|
||||
On Error Resume Next
|
||||
|
||||
Set VBProj = ThisWorkbook.VBProject
|
||||
Set VBComp = VBProj.VBComponents(moduleName)
|
||||
If Not VBComp Is Nothing Then
|
||||
Set CodeMod = VBComp.CodeModule
|
||||
ProcedureExists = (CodeMod.ProcStartLine(procName, 0) > 0)
|
||||
End If
|
||||
|
||||
If Err.Number <> 0 Then ProcedureExists = False
|
||||
On Error GoTo 0
|
||||
End Function
|
||||
@@ -0,0 +1,348 @@
|
||||
Attribute VB_Name = "Common_File_Utils"
|
||||
Option Explicit
|
||||
' ============================================================
|
||||
' Module Name: Write_Common
|
||||
' Module Desc: CSV write functions
|
||||
' Module Methods:
|
||||
' - GetSaveCSVPath
|
||||
' - WriteCSVFromArray
|
||||
' ============================================================
|
||||
|
||||
Function GetSaveCSVPath(Optional ByVal defaultName As String = "") As String
|
||||
Dim savePath As String
|
||||
savePath = Application.GetSaveAsFilename( _
|
||||
FileFilter:="CSV Files (*.csv), *.csv", _
|
||||
Title:="Save CSV", _
|
||||
InitialFileName:=defaultName)
|
||||
|
||||
If savePath = "False" Or savePath = "" Then
|
||||
GetSaveCSVPath = ""
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
If InStr(1, savePath, ".csv", vbTextCompare) = 0 Then
|
||||
savePath = savePath & ".csv"
|
||||
End If
|
||||
|
||||
GetSaveCSVPath = savePath
|
||||
End Function
|
||||
|
||||
' Writes a 2D array to a CSV file
|
||||
Sub WriteCSVFromArray( _
|
||||
ByVal filePath As String, _
|
||||
ByVal data As Variant, _
|
||||
Optional ByVal Charset As String = "shift_jis", _
|
||||
Optional ByVal alwaysQuote As Boolean = False _
|
||||
)
|
||||
' === Input validation ===
|
||||
If Not IsArray(data) Then
|
||||
Err.Raise 513, , "Input 'data' must be an array."
|
||||
End If
|
||||
|
||||
Dim numDims As Long
|
||||
On Error Resume Next
|
||||
numDims = ArrayDimensions(data)
|
||||
On Error GoTo 0
|
||||
If numDims <> 2 Then
|
||||
Err.Raise 514, , "Input array must be 2-dimensional."
|
||||
End If
|
||||
|
||||
Dim rows As Long, cols As Long
|
||||
rows = UBound(data, 1) - LBound(data, 1) + 1
|
||||
cols = UBound(data, 2) - LBound(data, 2) + 1
|
||||
|
||||
If rows = 0 Or cols = 0 Then Exit Sub ' Empty array, exit early
|
||||
|
||||
' === Build CSV content ===
|
||||
Dim outputLines As Collection
|
||||
Set outputLines = New Collection
|
||||
|
||||
Dim i As Long, j As Long
|
||||
Dim rowStr As String
|
||||
Dim field As String
|
||||
Dim needsQuote As Boolean
|
||||
|
||||
For i = LBound(data, 1) To UBound(data, 1)
|
||||
Dim fields As Variant
|
||||
ReDim fields(1 To cols)
|
||||
|
||||
For j = LBound(data, 2) To UBound(data, 2)
|
||||
' Safely convert variant to string
|
||||
field = SafeToString(data(i, j))
|
||||
|
||||
' Determine if the field needs quoting (per RFC 4180)
|
||||
needsQuote = alwaysQuote Or (InStr(field, """") > 0) Or _
|
||||
(InStr(field, ",") > 0) Or _
|
||||
(InStr(field, vbLf) > 0) Or _
|
||||
(InStr(field, vbCrLf) > 0) Or _
|
||||
(InStr(field, vbCr) > 0) Or _
|
||||
(Left(field, 1) = " " Or Right(field, 1) = " ")
|
||||
|
||||
If needsQuote Then
|
||||
' Escape double quotes: "" represents a single "
|
||||
field = """" & Replace(field, """", """""") & """"
|
||||
End If
|
||||
field = GetCode(field)
|
||||
|
||||
fields(j - LBound(data, 2) + 1) = field
|
||||
Next j
|
||||
|
||||
rowStr = Join(fields, ",")
|
||||
outputLines.Add rowStr
|
||||
Next i
|
||||
|
||||
' Concatenate all lines
|
||||
Dim finalContent As String
|
||||
finalContent = Join(CollectionToArray(outputLines), vbCrLf)
|
||||
|
||||
' === Write to file ===
|
||||
Dim stream As Object
|
||||
Set stream = CreateObject("ADODB.Stream")
|
||||
With stream
|
||||
.Type = 2 ' adTypeText
|
||||
.Charset = Charset
|
||||
.Open
|
||||
.WriteText finalContent, 0 ' adWriteChar
|
||||
.SaveToFile filePath, 2 ' adSaveCreateOverWrite
|
||||
.Close
|
||||
End With
|
||||
End Sub
|
||||
|
||||
' Helper function: safely convert any Variant to a string
|
||||
Private Function SafeToString(ByVal v As Variant) As String
|
||||
On Error Resume Next
|
||||
If IsNull(v) Or IsEmpty(v) Then
|
||||
SafeToString = ""
|
||||
Else
|
||||
SafeToString = CStr(v)
|
||||
End If
|
||||
On Error GoTo 0
|
||||
End Function
|
||||
|
||||
' Helper function: get the number of dimensions of an array (1, 2, ...)
|
||||
Private Function ArrayDimensions(arr As Variant) As Long
|
||||
Dim dimCount As Long
|
||||
On Error GoTo ExitPoint
|
||||
Do
|
||||
dimCount = dimCount + 1
|
||||
Dim tmp As Long
|
||||
tmp = UBound(arr, dimCount)
|
||||
Loop
|
||||
ExitPoint:
|
||||
ArrayDimensions = dimCount - 1
|
||||
End Function
|
||||
|
||||
' Helper function: convert a Collection to a 1D array (for use with Join)
|
||||
Private Function CollectionToArray(col As Collection) As Variant
|
||||
If col.Count = 0 Then
|
||||
CollectionToArray = Array()
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Dim arr() As String
|
||||
ReDim arr(1 To col.Count)
|
||||
Dim i As Long
|
||||
For i = 1 To col.Count
|
||||
arr(i) = col(i)
|
||||
Next i
|
||||
CollectionToArray = arr
|
||||
End Function
|
||||
|
||||
' ============================================================
|
||||
' Module Name: Read_Common
|
||||
' Module Desc: CSV read functions
|
||||
' Module Methods:
|
||||
' - SelectCSVFile
|
||||
' - ReadCSVAs2DArrayStrict
|
||||
' - ParseCSVLines
|
||||
' ============================================================
|
||||
|
||||
Function SelectCSVFile() As String
|
||||
Dim fileDialog As FileDialog
|
||||
Set fileDialog = Application.FileDialog(msoFileDialogFilePicker)
|
||||
|
||||
With fileDialog
|
||||
.Filters.Clear
|
||||
.Filters.Add "CSV Files", "*.csv"
|
||||
.AllowMultiSelect = False
|
||||
If .Show <> -1 Then
|
||||
SelectCSVFile = ""
|
||||
Exit Function
|
||||
End If
|
||||
SelectCSVFile = .SelectedItems(1)
|
||||
End With
|
||||
End Function
|
||||
|
||||
' Read a CSV file and return its content as a strict 2D array (1-based).
|
||||
' All rows must have the same number of columns as the first row.
|
||||
' If the file is empty and defaultColumnCount > 0, returns an empty 2D array with zero rows and defaultColumnCount columns.
|
||||
' Parameters:
|
||||
' filePath: Full path to the CSV file.
|
||||
' charset: Text encoding (e.g., "cp932", "utf-8").
|
||||
' defaultColumnCount: Optional. Used only if CSV has no rows. Must be >= 0.
|
||||
Function ReadCSVAs2DArrayStrict( _
|
||||
ByVal filePath As String, _
|
||||
ByVal expectedColumnCount As Long, _
|
||||
Optional ByVal charset As String = "cp932", _
|
||||
Optional ByVal hasHeader As Boolean = False) As Variant
|
||||
|
||||
' === validate expectedColumnCount ===
|
||||
If expectedColumnCount <= 0 Then
|
||||
Err.Raise 5001, , "expectedColumnCount must be >= 1."
|
||||
End If
|
||||
|
||||
If Dir(filePath) = "" Then
|
||||
Err.Raise 5002, , "File not found: " & filePath
|
||||
End If
|
||||
|
||||
' === read csv file ===
|
||||
Dim stream As Object
|
||||
Set stream = CreateObject("ADODB.Stream")
|
||||
With stream
|
||||
.Type = 2 ' adTypeText
|
||||
.charset = charset
|
||||
.Open
|
||||
.LoadFromFile filePath
|
||||
Dim textContent As String
|
||||
textContent = .ReadText
|
||||
.Close
|
||||
End With
|
||||
|
||||
' === standardize ===
|
||||
textContent = Replace(textContent, vbCrLf, vbLf)
|
||||
textContent = Replace(textContent, vbCr, vbLf)
|
||||
|
||||
' === transfer into collection ===
|
||||
Dim lines As Collection
|
||||
Set lines = ParseCSVLines(textContent)
|
||||
|
||||
' === validate empty ===
|
||||
If lines.Count = 0 Then
|
||||
Err.Raise 5003, , "CSV file is empty."
|
||||
End If
|
||||
|
||||
If lines.Count = 1 Then
|
||||
If hasHeader Then
|
||||
Err.Raise 5005, , "CSV file data is empty."
|
||||
End If
|
||||
End If
|
||||
|
||||
' === loop the row, validate column count ===
|
||||
Dim i As Long
|
||||
For i = 1 To lines.Count
|
||||
Dim rowArr As Variant
|
||||
rowArr = lines(i)
|
||||
Dim actualCols As Long
|
||||
actualCols = UBound(rowArr) - LBound(rowArr) + 1
|
||||
|
||||
If actualCols <> expectedColumnCount Then
|
||||
Err.Raise 5004, , "Row " & i & ": Expected " & expectedColumnCount & " columns, got " & actualCols & "."
|
||||
End If
|
||||
Next i
|
||||
|
||||
Dim result As Variant
|
||||
Dim startRow As Long
|
||||
If hasHeader Then
|
||||
startRow = 2
|
||||
Else
|
||||
startRow = 1
|
||||
End If
|
||||
|
||||
ReDim result(startRow To lines.Count, 1 To expectedColumnCount)
|
||||
|
||||
For i = startRow To lines.Count
|
||||
rowArr = lines(i)
|
||||
Dim j As Long
|
||||
For j = LBound(rowArr) To UBound(rowArr)
|
||||
result(i, j - LBound(rowArr) + 1) = rowArr(j)
|
||||
Next j
|
||||
Next i
|
||||
|
||||
ReadCSVAs2DArrayStrict = result
|
||||
End Function
|
||||
|
||||
' Helper function: Parse CSV text into collection of string arrays (zero-based per row)
|
||||
Private Function ParseCSVLines(ByVal csvText As String) As Collection
|
||||
Set ParseCSVLines = New Collection
|
||||
Dim length As Long: length = Len(csvText)
|
||||
If length = 0 Then Exit Function
|
||||
|
||||
Dim i As Long: i = 1
|
||||
Dim currentField As String
|
||||
Dim currentRow As Collection: Set currentRow = New Collection
|
||||
Dim inQuotes As Boolean
|
||||
Dim c As String
|
||||
|
||||
Do While i <= length
|
||||
c = Mid$(csvText, i, 1)
|
||||
Select Case c
|
||||
Case """"
|
||||
If inQuotes Then
|
||||
If i < length And Mid$(csvText, i + 1, 1) = """" Then
|
||||
currentField = currentField & """"
|
||||
i = i + 2
|
||||
Else
|
||||
inQuotes = False
|
||||
i = i + 1
|
||||
End If
|
||||
Else
|
||||
inQuotes = True
|
||||
i = i + 1
|
||||
End If
|
||||
Case ","
|
||||
If inQuotes Then
|
||||
currentField = currentField & c
|
||||
i = i + 1
|
||||
Else
|
||||
' Clean field before adding
|
||||
currentField = Trim(currentField)
|
||||
currentField = Replace(currentField, vbCr, "")
|
||||
currentField = Replace(currentField, vbLf, "")
|
||||
currentRow.Add currentField
|
||||
currentField = ""
|
||||
i = i + 1
|
||||
End If
|
||||
Case vbLf
|
||||
If inQuotes Then
|
||||
currentField = currentField & c
|
||||
i = i + 1
|
||||
Else
|
||||
currentRow.Add currentField
|
||||
Dim arr() As String
|
||||
If currentRow.Count > 0 Then
|
||||
ReDim arr(0 To currentRow.Count - 1)
|
||||
Dim k As Long
|
||||
For k = 1 To currentRow.Count
|
||||
arr(k - 1) = currentRow(k)
|
||||
Next k
|
||||
End If
|
||||
ParseCSVLines.Add arr
|
||||
Set currentRow = New Collection
|
||||
currentField = ""
|
||||
inQuotes = False
|
||||
i = i + 1
|
||||
End If
|
||||
Case Else
|
||||
currentField = currentField & c
|
||||
i = i + 1
|
||||
End Select
|
||||
Loop
|
||||
|
||||
' Handle last row without trailing newline
|
||||
If currentField <> "" Or currentRow.Count > 0 Then
|
||||
' Clean field before adding
|
||||
currentField = Trim(currentField)
|
||||
currentField = Replace(currentField, vbCr, "")
|
||||
currentField = Replace(currentField, vbLf, "")
|
||||
currentRow.Add currentField
|
||||
Dim lastArr() As String
|
||||
If currentRow.Count > 0 Then
|
||||
ReDim lastArr(0 To currentRow.Count - 1)
|
||||
Dim m As Long
|
||||
For m = 1 To currentRow.Count
|
||||
lastArr(m - 1) = currentRow(m)
|
||||
Next m
|
||||
End If
|
||||
ParseCSVLines.Add lastArr
|
||||
End If
|
||||
End Function
|
||||
@@ -0,0 +1,486 @@
|
||||
Attribute VB_Name = "Common_Functions"
|
||||
Option Explicit
|
||||
' ============================================================
|
||||
' Module Name: Module_Common
|
||||
' Module Desc: Common utility functions for all modules
|
||||
' Module Methods:
|
||||
' - GetLastDataRowInRange
|
||||
' - ClearDataRows
|
||||
' - ClearDataRow
|
||||
' ============================================================
|
||||
|
||||
' Common Functions
|
||||
|
||||
' Get CSV header from specified row and columns
|
||||
Function GetCSVHeader(ByVal ws As Worksheet) As Variant
|
||||
On Error GoTo ErrorHandler
|
||||
'
|
||||
Dim sheetConfDict As Object: Set sheetConfDict = GetSheetConfig()
|
||||
Dim sheetConf As Object: Set sheetConf = sheetConfDict(ws.CodeName)
|
||||
|
||||
Dim colLetters As Variant: colLetters = sheetConf("HeaderColumns")
|
||||
Dim headerRow As Long: headerRow = sheetConf("HeaderRow")
|
||||
|
||||
Dim colCount As Long: colCount = UBound(colLetters) - LBound(colLetters) + 1
|
||||
Dim headerArr() As String
|
||||
ReDim headerArr(1 To 1, 1 To colCount)
|
||||
|
||||
Dim i As Long
|
||||
Dim cellValue As String
|
||||
For i = 0 To colCount - 1
|
||||
Dim colIndex As Long
|
||||
colIndex = Columns(colLetters(i)).Column
|
||||
|
||||
cellValue = Trim(ws.Cells(headerRow, colIndex).Value)
|
||||
cellValue = Replace(cellValue, vbLf, "")
|
||||
cellValue = Replace(cellValue, vbCr, "")
|
||||
cellValue = Replace(cellValue, vbCrLf, "")
|
||||
headerArr(1, i + 1) = cellValue
|
||||
Next i
|
||||
|
||||
GetCSVHeader = headerArr
|
||||
Exit Function
|
||||
|
||||
ErrorHandler:
|
||||
Err.Raise 1005, "GetCSVHeader", "Invalid column letter in HeaderColumns: '" & colLetters(i) & "'"
|
||||
End Function
|
||||
|
||||
'
|
||||
Function CleanCSVField(ByVal inputStr As String) As String
|
||||
Dim s As String
|
||||
s = Trim(inputStr)
|
||||
|
||||
' calcute
|
||||
If Len(s) > 0 Then
|
||||
Select Case Left(s, 1)
|
||||
Case "=", "+", "-", "@"
|
||||
CleanCSVField = "'" & s
|
||||
Exit Function
|
||||
End Select
|
||||
End If
|
||||
|
||||
CleanCSVField = s
|
||||
End Function
|
||||
|
||||
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 cacheName As String) As Object
|
||||
On Error GoTo ErrHandler
|
||||
|
||||
' --- validate ---
|
||||
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
|
||||
|
||||
' --- obtain worksheet ---
|
||||
On Error Resume Next
|
||||
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets(sheetName)
|
||||
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
|
||||
Dim i As Long
|
||||
For i = LBound(valueCols) To UBound(valueCols)
|
||||
If Not IsNumeric(valueCols(i)) Then Exit Function
|
||||
Dim colNum As Long: colNum = CLng(valueCols(i))
|
||||
If colNum < 1 Then Exit Function
|
||||
If colNum < minCol Then minCol = colNum
|
||||
If colNum > maxCol Then maxCol = colNum
|
||||
Next i
|
||||
|
||||
' --- read ---
|
||||
Dim dataRange As Range
|
||||
Set dataRange = ws.Range(ws.Cells(startRow, minCol), ws.Cells(lastRow, maxCol))
|
||||
Dim data As Variant: data = dataRange.Value
|
||||
|
||||
' --- Ensure data is a 2D array ---
|
||||
If Not IsArray(data) Then
|
||||
' Single cell case
|
||||
Dim temp As Variant
|
||||
ReDim temp(1 To 1, 1 To (maxCol - minCol + 1))
|
||||
temp(1, 1) = data
|
||||
data = temp
|
||||
End If
|
||||
|
||||
' --- build ---
|
||||
Dim keyOffset As Long: keyOffset = keyCol - minCol + 1
|
||||
Dim valOffsets() As Long: ReDim valOffsets(0 To nValCols - 1)
|
||||
For i = 0 To nValCols - 1
|
||||
valOffsets(i) = valueCols(LBound(valueCols) + i) - minCol + 1
|
||||
Next i
|
||||
|
||||
' --- write into ---
|
||||
dict.CompareMode = vbTextCompare
|
||||
|
||||
Dim r As Long
|
||||
For r = 1 To UBound(data, 1)
|
||||
Dim key As String: key = Trim(data(r, keyOffset))
|
||||
If key <> "" Then
|
||||
Dim vals() As String: ReDim vals(0 To nValCols - 1)
|
||||
Dim j As Long
|
||||
For j = 0 To nValCols - 1
|
||||
vals(j) = Trim(data(r, valOffsets(j)))
|
||||
Next j
|
||||
dict(key) = vals
|
||||
End If
|
||||
Next r
|
||||
|
||||
Set LoadLookup = dict
|
||||
Exit Function
|
||||
|
||||
ErrHandler:
|
||||
Err.Raise Err.Number, Err.Source, Err.Description
|
||||
End Function
|
||||
|
||||
' obtain
|
||||
Function GetLastDataRowInRange(ws As Worksheet) As Long
|
||||
|
||||
Dim sheetConfDict As Object: Set sheetConfDict = GetSheetConfig()
|
||||
|
||||
If sheetConfDict.Exists(ws.CodeName) Then
|
||||
Dim sheetConf As Object: Set sheetConf = sheetConfDict(ws.CodeName)
|
||||
|
||||
Dim startCol As Long, endCol As Long, startRow As Long
|
||||
On Error GoTo InvalidColumn
|
||||
startCol = ws.Range(sheetConf("StartCol") & "1").Column
|
||||
endCol = ws.Range(sheetConf("EndCol") & "1").Column
|
||||
startRow = sheetConf("StartRow")
|
||||
On Error GoTo 0
|
||||
|
||||
' --- query max row ---
|
||||
Dim colIndex As Long, lastRow As Long, maxRow As Long
|
||||
|
||||
maxRow = startRow - 1
|
||||
For colIndex = startCol To endCol
|
||||
lastRow = ws.Cells(ws.Rows.Count, colIndex).End(xlUp).Row
|
||||
If lastRow > maxRow Then maxRow = lastRow
|
||||
Next colIndex
|
||||
|
||||
GetLastDataRowInRange = maxRow
|
||||
Else
|
||||
Err.Raise 1004, "GetLastDataRowInRange", "Sheet not configured: " & ws.CodeName
|
||||
End If
|
||||
Exit Function
|
||||
|
||||
InvalidColumn:
|
||||
Err.Raise 1005, "GetLastDataRowInRange", "Invalid column letter for sheet: " & ws.CodeName
|
||||
End Function
|
||||
|
||||
Function ClearDataRow(ByVal ws As Worksheet, ByVal startCol As Long, ByVal endCol As Long, ByVal rowRow As Long, Optional ByVal errorCol As Long = 2)
|
||||
If rowRow >= 7 Then
|
||||
Dim clearRange As Range: Set clearRange = ws.Range(ws.Cells(rowRow, startCol), ws.Cells(rowRow, endCol))
|
||||
clearRange.ClearContents
|
||||
clearRange.Interior.Color = vbWhite
|
||||
ws.Range(ws.Cells(rowRow, errorCol), ws.Cells(rowRow, errorCol)).ClearContents
|
||||
End If
|
||||
End Function
|
||||
|
||||
Sub ClearDataRows(ByVal ws As Worksheet)
|
||||
'
|
||||
Dim sheetConfDict As Object: Set sheetConfDict = GetSheetConfig()
|
||||
'
|
||||
If Not sheetConfDict.Exists(ws.CodeName) Then
|
||||
Err.Raise 1004, "ClearDataRows", "Sheet not configured: " & ws.CodeName
|
||||
End If
|
||||
|
||||
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")
|
||||
Dim errorCol As String: errorCol = sheetConf("ErrorCol")
|
||||
'
|
||||
Dim lastDataRow As Long: lastDataRow = GetLastDataRowInRange(ws)
|
||||
|
||||
'
|
||||
If lastDataRow >= startRow Then
|
||||
Dim clearRange As Range
|
||||
Set clearRange = ws.Range(ws.Cells(startRow, ws.Range(startCol & "1").Column), ws.Cells(lastDataRow, ws.Range(endCol & "1").Column))
|
||||
clearRange.ClearContents
|
||||
clearRange.Interior.Color = vbWhite
|
||||
|
||||
If errorCol <> "" Then
|
||||
Dim clearErrorRange As Range
|
||||
Set clearErrorRange = ws.Range(ws.Cells(startRow, ws.Range(errorCol & "1").Column), ws.Cells(lastDataRow, ws.Range(errorCol & "1").Column))
|
||||
clearErrorRange.ClearContents
|
||||
clearErrorRange.Interior.Color = vbWhite
|
||||
End If
|
||||
End If
|
||||
End Sub
|
||||
|
||||
' Format: code:value (no space around colon)
|
||||
Function MakeSelect(ByVal code As String, ByVal value As String) As String
|
||||
MakeSelect = Trim(code) & ":" & Trim(value)
|
||||
End Function
|
||||
|
||||
' Get left part of MakeSelect format (e.g., "1:JR" -> "1")
|
||||
Function GetCode(ByVal text As String) As String
|
||||
Dim pos As Long: pos = InStr(text, ":")
|
||||
If pos > 0 Then
|
||||
GetCode = Left(text, pos - 1)
|
||||
Else
|
||||
GetCode = text
|
||||
End If
|
||||
End Function
|
||||
|
||||
' ============================================================
|
||||
' Format date input: YYYYMMDD or YYMMDD -> YYYY-MM-DD
|
||||
' ============================================================
|
||||
Public Function FormatDateInput(ByVal inputStr As String) As String
|
||||
Dim s As String: s = Trim(inputStr)
|
||||
If s = "" Then Exit Function
|
||||
|
||||
' Only process pure digit strings
|
||||
If Not IsNumeric(s) Then
|
||||
FormatDateInput = inputStr
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Dim yearPart As String, monthPart As String, dayPart As String
|
||||
Dim dateStr As String
|
||||
|
||||
If Len(s) = 8 Then
|
||||
' YYYYMMDD format
|
||||
yearPart = Left(s, 4)
|
||||
monthPart = Mid(s, 5, 2)
|
||||
dayPart = Right(s, 2)
|
||||
ElseIf Len(s) = 6 Then
|
||||
' YYMMDD format - add 20 prefix
|
||||
yearPart = "20" & Left(s, 2)
|
||||
monthPart = Mid(s, 3, 2)
|
||||
dayPart = Right(s, 2)
|
||||
Else
|
||||
FormatDateInput = inputStr
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
' Build date string and validate
|
||||
dateStr = yearPart & "-" & monthPart & "-" & dayPart
|
||||
|
||||
If IsDate(dateStr) Then
|
||||
FormatDateInput = dateStr
|
||||
Else
|
||||
FormatDateInput = inputStr
|
||||
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 = GetCache("errorList")
|
||||
Dim errorMessage As String
|
||||
If errorList.Exists(errorCode) Then
|
||||
errorMessage = Replace(errorList(errorCode)(0), "{0}", param0)
|
||||
errorMessage = Replace(errorMessage, "{1}", param1)
|
||||
errorMessage = MakeSelect(errorCode, errorMessage)
|
||||
End If
|
||||
GetErrorMsg = errorMessage
|
||||
End Function
|
||||
|
||||
Function ColLetter(colNum As Long) As String
|
||||
ColLetter = Split(Cells(1, colNum).Address, "$")(1)
|
||||
End Function
|
||||
|
||||
Function CheckRequired(ByVal ws As Worksheet, ByVal rowNum As Long, ByVal colNum As Long, ByVal errorCol As String) As Boolean
|
||||
Dim checkValue As String: checkValue = Trim(ws.Cells(rowNum, colNum).Value)
|
||||
If checkValue = "" Then
|
||||
Dim letter As String: letter = ColLetter(colNum)
|
||||
ws.Cells(rowNum, errorCol).Value = GetErrorMsg("E002", letter & rowNum)
|
||||
ws.Cells(rowNum, colNum).Interior.Color = RGB(255, 0, 0)
|
||||
CheckRequired = False
|
||||
Exit Function
|
||||
End If
|
||||
CheckRequired = True
|
||||
End Function
|
||||
|
||||
Function CheckChar(ByVal ws As Worksheet, ByVal rowNum As Long, ByVal colNum As Long, ByVal charLength As Long, ByVal errorCol As String)
|
||||
Dim checkValue As String: checkValue = Trim(ws.Cells(rowNum, colNum).Value)
|
||||
If Len(checkValue) <> charLength Then
|
||||
Dim letter As String: letter = ColLetter(colNum)
|
||||
ws.Cells(rowNum, errorCol).Value = GetErrorMsg("E006", letter & rowNum, charLength)
|
||||
ws.Cells(rowNum, colNum).Interior.Color = RGB(255, 0, 0)
|
||||
CheckChar = False
|
||||
Exit Function
|
||||
End If
|
||||
CheckChar = True
|
||||
End Function
|
||||
|
||||
Function CheckAlphanumeric(ByVal ws As Worksheet, ByVal rowNum As Long, ByVal colNum As Long, ByVal charLength As Long, ByVal errorCol As String)
|
||||
Dim checkValue As String: checkValue = Trim(ws.Cells(rowNum, colNum).Value)
|
||||
Dim letter As String: letter = ColLetter(colNum)
|
||||
Dim i As Long
|
||||
Dim ch As String
|
||||
For i = 1 To charLength
|
||||
ch = Mid(checkValue, i, 1)
|
||||
If Not ((ch >= "0" And ch <= "9") Or (ch >= "A" And ch <= "Z") Or (ch >= "a" And ch <= "z")) Then
|
||||
ws.Cells(rowNum, errorCol).Value = GetErrorMsg("E005", letter & rowNum)
|
||||
ws.Cells(rowNum, colNum).Interior.Color = RGB(255, 0, 0)
|
||||
CheckAlphanumeric = False
|
||||
Exit Function
|
||||
End If
|
||||
Next i
|
||||
|
||||
CheckAlphanumeric = True
|
||||
End Function
|
||||
|
||||
Function CheckVarcharOver(ByVal ws As Worksheet, ByVal rowNum As Long, ByVal colNum As Long, ByVal varcharLength As Long, ByVal errorCol As String)
|
||||
Dim checkValue As String: checkValue = Trim(ws.Cells(rowNum, colNum).Value)
|
||||
If Len(checkValue) > varcharLength Then
|
||||
Dim letter As String: letter = ColLetter(colNum)
|
||||
ws.Cells(rowNum, errorCol).Value = GetErrorMsg("E007", letter & rowNum)
|
||||
ws.Cells(rowNum, colNum).Interior.Color = RGB(255, 0, 0)
|
||||
CheckVarcharOver = False
|
||||
Exit Function
|
||||
End If
|
||||
CheckVarcharOver = True
|
||||
End Function
|
||||
|
||||
Function CheckNumberOver(ByVal ws As Worksheet, ByVal rowNum As Long, ByVal colNum As Long, ByVal numberLength As Long, ByVal errorCol As String)
|
||||
Dim checkValue As String: checkValue = Trim(ws.Cells(rowNum, colNum).Value)
|
||||
If Len(checkValue) > numberLength Then
|
||||
Dim letter As String: letter = ColLetter(colNum)
|
||||
ws.Cells(rowNum, errorCol).Value = GetErrorMsg("E014", letter & rowNum, numberLength)
|
||||
ws.Cells(rowNum, colNum).Interior.Color = RGB(255, 0, 0)
|
||||
CheckNumberOver = False
|
||||
Exit Function
|
||||
End If
|
||||
CheckNumberOver = True
|
||||
End Function
|
||||
|
||||
Function Check01(ByVal ws As Worksheet, ByVal rowNum As Long, ByVal colNum As Long, ByVal errorCol As String)
|
||||
Dim checkValue As String: checkValue = Trim(ws.Cells(rowNum, colNum).Value)
|
||||
Dim letter As String: letter = ColLetter(colNum)
|
||||
If checkValue <> "" Then
|
||||
If Len(checkValue) <> 1 Then
|
||||
ws.Cells(rowNum, errorCol).Value = GetErrorMsg("E001", letter & rowNum)
|
||||
ws.Cells(rowNum, colNum).Interior.Color = RGB(255, 0, 0)
|
||||
Check01 = False
|
||||
Exit Function
|
||||
End If
|
||||
If checkValue <> "0" And checkValue <> "1" Then
|
||||
ws.Cells(rowNum, errorCol).Value = GetErrorMsg("E008", letter & rowNum)
|
||||
ws.Cells(rowNum, colNum).Interior.Color = RGB(255, 0, 0)
|
||||
Check01 = False
|
||||
Exit Function
|
||||
End If
|
||||
End If
|
||||
Check01 = True
|
||||
End Function
|
||||
|
||||
Function Check12(ByVal ws As Worksheet, ByVal rowNum As Long, ByVal colNum As Long, ByVal errorCol As String)
|
||||
Dim checkValue As String: checkValue = Trim(ws.Cells(rowNum, colNum).Value)
|
||||
Dim letter As String: letter = ColLetter(colNum)
|
||||
If checkValue <> "" Then
|
||||
If Len(checkValue) <> 1 Then
|
||||
ws.Cells(rowNum, errorCol).Value = GetErrorMsg("E001", letter & rowNum)
|
||||
ws.Cells(rowNum, colNum).Interior.Color = RGB(255, 0, 0)
|
||||
Check12 = False
|
||||
Exit Function
|
||||
End If
|
||||
If checkValue <> "1" And checkValue <> "2" Then
|
||||
ws.Cells(rowNum, errorCol).Value = GetErrorMsg("E009", letter & rowNum)
|
||||
ws.Cells(rowNum, colNum).Interior.Color = RGB(255, 0, 0)
|
||||
Check12 = False
|
||||
Exit Function
|
||||
End If
|
||||
End If
|
||||
Check12 = True
|
||||
End Function
|
||||
|
||||
Function CheckDuplicate(ByVal ws As Worksheet, ByVal rowNum As Long, ByVal colNum As Long, ByVal errorCol As String) As Boolean
|
||||
Dim checkValue As String: checkValue = Trim(ws.Cells(rowNum, colNum).Value)
|
||||
Dim letter As String: letter = ColLetter(colNum)
|
||||
Dim i As Long
|
||||
|
||||
For i = 7 To rowNum - 1
|
||||
If Trim(ws.Cells(i, colNum).Value) = checkValue Then
|
||||
ws.Cells(rowNum, errorCol).Value = GetErrorMsg("E010", letter & rowNum, checkValue)
|
||||
ws.Cells(rowNum, colNum).Interior.Color = RGB(255, 0, 0)
|
||||
CheckDuplicate = False
|
||||
Exit Function
|
||||
End If
|
||||
Next i
|
||||
|
||||
CheckDuplicate = True
|
||||
End Function
|
||||
|
||||
Function CheckNumber(ByVal ws As Worksheet, ByVal rowNum As Long, ByVal colNum As Long, ByVal errorCol As String) As Boolean
|
||||
Dim checkValue As String: checkValue = Trim(ws.Cells(rowNum, colNum).Value)
|
||||
Dim letter As String: letter = ColLetter(colNum)
|
||||
|
||||
If checkValue = "" Then
|
||||
CheckNumber = True
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
If Not IsNumeric(checkValue) Then
|
||||
ws.Cells(rowNum, errorCol).Value = GetErrorMsg("E011", letter & rowNum)
|
||||
ws.Cells(rowNum, colNum).Interior.Color = RGB(255, 0, 0)
|
||||
CheckNumber = False
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
CheckNumber = True
|
||||
End Function
|
||||
@@ -0,0 +1,587 @@
|
||||
Attribute VB_Name = "Common_Global_Cache"
|
||||
Option Explicit
|
||||
' ============================================================
|
||||
' Module Name: Global_Cache
|
||||
' Module Desc: Global Cache Module, Shared caches across all worksheets
|
||||
' Module Methods:
|
||||
' - RefreshM1KukanDCache
|
||||
' - RefreshM2Cache
|
||||
' - RefreshO1Cache
|
||||
' ============================================================
|
||||
Private sheetConfDict As Object
|
||||
|
||||
Public GlobalCache As Object
|
||||
|
||||
Public Sub InitCacheManager()
|
||||
If GlobalCache Is Nothing Then
|
||||
Set GlobalCache = CreateObject("Scripting.Dictionary")
|
||||
GlobalCache.CompareMode = vbTextCompare
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Public Function GetCache(ByVal cacheName As String) As Object
|
||||
Dim cache As Object
|
||||
Dim loadedData As Object
|
||||
|
||||
'
|
||||
On Error GoTo RefreshError
|
||||
|
||||
'
|
||||
If GlobalCache Is Nothing Then InitCacheManager
|
||||
|
||||
'
|
||||
If Not GlobalCache.Exists(cacheName) Then
|
||||
Set GlobalCache(cacheName) = CreateObject("Scripting.Dictionary")
|
||||
GlobalCache(cacheName).CompareMode = vbTextCompare
|
||||
End If
|
||||
|
||||
Set cache = GlobalCache(cacheName)
|
||||
Dim sheetConfDict As Object: Set sheetConfDict = GetSheetConfig()
|
||||
If cache.Count = 0 Then
|
||||
If cacheName = "M1KukanDCache" Then
|
||||
Set loadedData = LookupM1KukanCache()
|
||||
ElseIf cacheName = "M2" Then
|
||||
Set loadedData = LookupM2Cache()
|
||||
ElseIf cacheName = "O1" Then
|
||||
Set loadedData = LookupO1Cache()
|
||||
ElseIf Contains(sheetConfDict("Enum"), cacheName) Then
|
||||
Set loadedData = LoadLookup("Enum", cacheName)
|
||||
Else
|
||||
Set loadedData = LoadLookup(cacheName, cacheName)
|
||||
End If
|
||||
|
||||
If Not loadedData Is Nothing Then
|
||||
Set GlobalCache(cacheName) = loadedData
|
||||
Set cache = loadedData
|
||||
End If
|
||||
End If
|
||||
|
||||
Set GetCache = cache
|
||||
Exit Function
|
||||
|
||||
RefreshError:
|
||||
Err.Raise 1001, "GetCache", "Failed to load " & cacheName & " lookup cache: " & Err.Description
|
||||
End Function
|
||||
|
||||
' before RefreshCache, should validate
|
||||
Public Sub RefreshCache(ByVal cacheName As String)
|
||||
Dim loadedData As Object
|
||||
|
||||
'
|
||||
On Error GoTo RefreshError
|
||||
|
||||
'
|
||||
If GlobalCache Is Nothing Then InitCacheManager
|
||||
|
||||
'
|
||||
If Not GlobalCache.Exists(cacheName) Then
|
||||
Set GlobalCache(cacheName) = CreateObject("Scripting.Dictionary")
|
||||
GlobalCache(cacheName).CompareMode = vbTextCompare
|
||||
End If
|
||||
|
||||
Dim sheetConfDict As Object: Set sheetConfDict = GetSheetConfig()
|
||||
If cacheName = "M1KukanDCache" Then
|
||||
Set loadedData = LookupM1KukanCache()
|
||||
ElseIf cacheName = "M2" Then
|
||||
Set loadedData = LookupM2Cache()
|
||||
ElseIf cacheName = "O1" Then
|
||||
Set loadedData = LookupO1Cache()
|
||||
ElseIf Contains(sheetConfDict("Enum"), cacheName) Then
|
||||
Set loadedData = LoadLookup("Enum", cacheName)
|
||||
Else
|
||||
Set loadedData = LoadLookup(cacheName, cacheName)
|
||||
End If
|
||||
|
||||
If Not loadedData Is Nothing Then
|
||||
Set GlobalCache(cacheName) = loadedData
|
||||
End If
|
||||
|
||||
Exit Sub
|
||||
|
||||
RefreshError:
|
||||
Err.Raise 1001, "RefreshCache", "Failed to load " & cacheName & " lookup cache: " & Err.Description
|
||||
End Sub
|
||||
|
||||
' Refresh M1_KukanD cache - nested dict {D: {F: [G]}}
|
||||
' Structure: { 交通機関区分[D]: { 利用区間発名[F]: [利用区間着名G] } }
|
||||
Private Function LookupM1KukanCache()
|
||||
Dim resultCache As Object
|
||||
Set resultCache = CreateObject("Scripting.Dictionary")
|
||||
|
||||
On Error GoTo ErrHandler
|
||||
|
||||
Dim ws As Worksheet
|
||||
On Error Resume Next
|
||||
Set ws = ThisWorkbook.Worksheets("M1")
|
||||
On Error GoTo ErrHandler
|
||||
|
||||
If ws Is Nothing Then
|
||||
Set LookupM1KukanCache = resultCache
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Dim sheetConf As Object: Set sheetConf = sheetConfDict("M1")
|
||||
Dim startRow As Long: startRow = sheetConf("StartRow")
|
||||
Dim lastRow As Long: lastRow = GetLastDataRowInRange(ws)
|
||||
If lastRow < startRow Then
|
||||
Set LookupM2Cache = resultCache
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Dim r As Long
|
||||
For r = startRow To lastRow
|
||||
Dim dValue As String: dValue = Trim(ws.Cells(r, 4).Value) ' D column
|
||||
Dim fValue As String: fValue = Trim(ws.Cells(r, 6).Value) ' F column
|
||||
Dim gValue As String: gValue = Trim(ws.Cells(r, 7).Value) ' G column
|
||||
|
||||
If dValue = "" Or fValue = "" Then GoTo NextRow2
|
||||
|
||||
' Outer level: D column (交通機関区分)
|
||||
If Not resultCache.Exists(dValue) Then
|
||||
Dim innerDict As Object: Set innerDict = CreateObject("Scripting.Dictionary")
|
||||
resultCache.Add dValue, innerDict
|
||||
End If
|
||||
|
||||
' Inner level: F column (利用区間発名) -> array of G values
|
||||
Set innerDict = resultCache(dValue)
|
||||
If Not innerDict.Exists(fValue) Then
|
||||
Dim arr As Object: Set arr = CreateObject("Scripting.Dictionary")
|
||||
innerDict.Add fValue, arr
|
||||
End If
|
||||
|
||||
Set arr = innerDict(fValue)
|
||||
If gValue <> "" And Not arr.Exists(gValue) Then
|
||||
arr.Add gValue, True
|
||||
End If
|
||||
|
||||
NextRow2:
|
||||
Next r
|
||||
|
||||
Set LookupM1KukanCache = resultCache
|
||||
Exit Function
|
||||
|
||||
ErrHandler:
|
||||
Err.Raise Err.Number, Err.Source, Err.Description
|
||||
End Function
|
||||
|
||||
' ============================================================
|
||||
' M2 Cache - Nested Dictionary
|
||||
' Structure: { 区間コード[C]: { 券種[I]: { コード[J]: K } } }
|
||||
' ============================================================
|
||||
Private Function LookupM2Cache() As Object
|
||||
Dim resultCache As Object
|
||||
Set resultCache = CreateObject("Scripting.Dictionary")
|
||||
|
||||
On Error GoTo ErrHandler
|
||||
|
||||
Dim ws As Worksheet
|
||||
On Error Resume Next
|
||||
Set ws = ThisWorkbook.Worksheets("M2")
|
||||
On Error GoTo ErrHandler
|
||||
|
||||
If ws Is Nothing Then
|
||||
Set LookupM2Cache = resultCache
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Dim sheetConf As Object: Set sheetConf = sheetConfDict("M2")
|
||||
Dim startRow As Long: startRow = sheetConf("StartRow")
|
||||
Dim lastRow As Long: lastRow = GetLastDataRowInRange(ws)
|
||||
If lastRow < startRow Then
|
||||
Set LookupM2Cache = resultCache
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Dim r As Long
|
||||
For r = startRow To lastRow
|
||||
Dim kukanCode As String: kukanCode = Trim(ws.Cells(r, 3).Value) ' C column
|
||||
Dim kanshu As String: kanshu = Trim(ws.Cells(r, 9).Value) ' I column
|
||||
Dim code As String: code = Trim(ws.Cells(r, 10).Value) ' J column
|
||||
Dim name As String: name = Trim(ws.Cells(r, 11).Value) ' K column
|
||||
|
||||
If kukanCode = "" Or kanshu = "" Or code = "" Then GoTo NextRow
|
||||
|
||||
' Outer level: kukanCode
|
||||
If Not resultCache.Exists(kukanCode) Then
|
||||
Dim innerDict As Object: Set innerDict = CreateObject("Scripting.Dictionary")
|
||||
resultCache.Add kukanCode, innerDict
|
||||
End If
|
||||
|
||||
' Middle level: kanshu
|
||||
Set innerDict = resultCache(kukanCode)
|
||||
If Not innerDict.Exists(kanshu) Then
|
||||
Dim innermostDict As Object: Set innermostDict = CreateObject("Scripting.Dictionary")
|
||||
innerDict.Add kanshu, innermostDict
|
||||
End If
|
||||
|
||||
' Inner level: code -> name
|
||||
Set innermostDict = innerDict(kanshu)
|
||||
If Not innermostDict.Exists(code) Then
|
||||
innermostDict.Add code, name
|
||||
End If
|
||||
|
||||
NextRow:
|
||||
Next r
|
||||
|
||||
Set LookupM2Cache = resultCache
|
||||
Exit Function
|
||||
|
||||
ErrHandler:
|
||||
Err.Raise Err.Number, Err.Source, Err.Description
|
||||
End Function
|
||||
|
||||
' ============================================================
|
||||
' O1 Cache
|
||||
' ============================================================
|
||||
Private Function LookupO1Cache() As Object
|
||||
Dim resultCache As Object
|
||||
Set resultCache = CreateObject("Scripting.Dictionary")
|
||||
|
||||
Dim ws As Worksheet
|
||||
On Error Resume Next
|
||||
Set ws = ThisWorkbook.Worksheets("O1")
|
||||
On Error GoTo ErrHandler
|
||||
|
||||
If ws Is Nothing Then
|
||||
Set LookupO1Cache = resultCache
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Dim sheetConf As Object: Set sheetConf = sheetConfDict("O1")
|
||||
Dim startRow As Long: startRow = sheetConf("StartRow")
|
||||
Dim lastRow As Long: lastRow = GetLastDataRowInRange(ws)
|
||||
If lastRow < startRow Then
|
||||
Set LookupO1Cache = resultCache
|
||||
Exit Function
|
||||
End If
|
||||
|
||||
Dim r As Long
|
||||
For r = startRow To lastRow
|
||||
Dim cVal As String
|
||||
cVal = Trim(ws.Cells(r, 3).Value) ' C column
|
||||
Dim eVal As String
|
||||
eVal = Trim(ws.Cells(r, 5).Value) ' E column
|
||||
Dim fVal As String
|
||||
fVal = Trim(ws.Cells(r, 6).Value) ' F column
|
||||
|
||||
If cVal = "" Or eVal = "" Then GoTo NextO1
|
||||
|
||||
' Outer: C column
|
||||
If Not resultCache.Exists(cVal) Then
|
||||
Dim innerDict As Object
|
||||
Set innerDict = CreateObject("Scripting.Dictionary")
|
||||
resultCache.Add cVal, innerDict
|
||||
End If
|
||||
|
||||
' Inner: E column -> array of F values
|
||||
Set innerDict = resultCache(cVal)
|
||||
If Not innerDict.Exists(eVal) Then
|
||||
Dim arr As Object
|
||||
Set arr = CreateObject("Scripting.Dictionary")
|
||||
innerDict.Add eVal, arr
|
||||
End If
|
||||
|
||||
Set arr = innerDict(eVal)
|
||||
If Not arr.Exists(fVal) Then
|
||||
arr.Add fVal, True
|
||||
End If
|
||||
|
||||
NextO1:
|
||||
Next r
|
||||
|
||||
Set LookupO1Cache = resultCache
|
||||
Exit Function
|
||||
|
||||
ErrHandler:
|
||||
Err.Raise Err.Number, Err.Source, Err.Description
|
||||
End Function
|
||||
|
||||
Private Sub RefreshSheetDict()
|
||||
Debug.Print "RefreshSheetDict begin."
|
||||
Set sheetConfDict = CreateObject("Scripting.Dictionary")
|
||||
Dim sheetConf As Object
|
||||
|
||||
' C1
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartCol") = "C"
|
||||
sheetConf("EndCol") = "BC"
|
||||
sheetConf("ErrorCol") = "BD"
|
||||
sheetConf("StartRow") = 8
|
||||
sheetConf("HeaderRow") = 6
|
||||
sheetConf("CSV_Encoding") = "shift_jis"
|
||||
sheetConf("HasHeader") = True
|
||||
sheetConf("ExpectedColumnCount") = 41
|
||||
sheetConf("HeaderColumns") = Array("C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "W", "X", "Y", "Z", "AD", "AE", "AF", "AG", "AK", "AL", "AM", "AN", "AR", "AS", "AT", "AU", "AV", "AW", "AX", "AY", "AZ", "BA", "BB", "BC")
|
||||
sheetConf("AlwaysQuote") = False
|
||||
sheetConf("FilterRow") = 7
|
||||
Set sheetConfDict("C1") = sheetConf
|
||||
Debug.Print "RefreshSheetDict C1 ok."
|
||||
|
||||
' M1
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartCol") = "C"
|
||||
sheetConf("EndCol") = "N"
|
||||
sheetConf("ErrorCol") = "O"
|
||||
sheetConf("StartRow") = 7
|
||||
sheetConf("HeaderRow") = 5
|
||||
sheetConf("CacheName") = "m1Cache"
|
||||
sheetConf("CSV_Encoding") = "shift_jis"
|
||||
sheetConf("HasHeader") = True
|
||||
sheetConf("ExpectedColumnCount") = 12
|
||||
sheetConf("HeaderColumns") = Array("C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N")
|
||||
sheetConf("AlwaysQuote") = False
|
||||
sheetConf("FilterRow") = 6
|
||||
sheetConf("KeyCol") = 3
|
||||
sheetConf("ValueCols") = Array(3, 4, 5, 6, 7, 9, 12)
|
||||
Set sheetConfDict("M1") = sheetConf
|
||||
Debug.Print "RefreshSheetDict M1 ok."
|
||||
|
||||
' M2
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartCol") = "C"
|
||||
sheetConf("EndCol") = "R"
|
||||
sheetConf("ErrorCol") = "S"
|
||||
sheetConf("StartRow") = 7
|
||||
sheetConf("HeaderRow") = 6
|
||||
sheetConf("CSV_Encoding") = "shift_jis"
|
||||
sheetConf("HasHeader") = True
|
||||
sheetConf("ExpectedColumnCount") = 11
|
||||
sheetConf("HeaderColumns") = Array("C", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R")
|
||||
sheetConf("AlwaysQuote") = False
|
||||
sheetConf("FilterRow") = 6
|
||||
Set sheetConfDict("M2") = sheetConf
|
||||
Debug.Print "RefreshSheetDict M2 ok."
|
||||
|
||||
' Z1
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartCol") = "C"
|
||||
sheetConf("EndCol") = "I"
|
||||
sheetConf("ErrorCol") = "B"
|
||||
sheetConf("StartRow") = 7
|
||||
sheetConf("HeaderRow") = 5
|
||||
sheetConf("CSV_Encoding") = "utf-8"
|
||||
sheetConf("HasHeader") = False
|
||||
sheetConf("ExpectedColumnCount") = 7
|
||||
sheetConf("HeaderColumns") = Array("C", "D", "E", "F", "G", "H", "I")
|
||||
sheetConf("AlwaysQuote") = True
|
||||
sheetConf("FilterRow") = 6
|
||||
sheetConf("KeyCol") = 3
|
||||
sheetConf("ValueCols") = Array(4)
|
||||
Set sheetConfDict("Z1") = sheetConf
|
||||
Debug.Print "RefreshSheetDict Z1 ok."
|
||||
|
||||
' Z2
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartCol") = "C"
|
||||
sheetConf("EndCol") = "G"
|
||||
sheetConf("ErrorCol") = "B"
|
||||
sheetConf("StartRow") = 7
|
||||
sheetConf("HeaderRow") = 5
|
||||
sheetConf("CSV_Encoding") = "utf-8"
|
||||
sheetConf("HasHeader") = False
|
||||
sheetConf("ExpectedColumnCount") = 5
|
||||
sheetConf("HeaderColumns") = Array("C", "D", "E", "F", "G")
|
||||
sheetConf("AlwaysQuote") = True
|
||||
sheetConf("FilterRow") = 6
|
||||
sheetConf("KeyCol") = 3
|
||||
sheetConf("ValueCols") = Array(4)
|
||||
Set sheetConfDict("Z2") = sheetConf
|
||||
Debug.Print "RefreshSheetDict Z2 ok."
|
||||
|
||||
' Z3
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartCol") = "C"
|
||||
sheetConf("EndCol") = "H"
|
||||
sheetConf("ErrorCol") = "B"
|
||||
sheetConf("StartRow") = 7
|
||||
sheetConf("HeaderRow") = 5
|
||||
sheetConf("CSV_Encoding") = "utf-8"
|
||||
sheetConf("HasHeader") = False
|
||||
sheetConf("ExpectedColumnCount") = 6
|
||||
sheetConf("HeaderColumns") = Array("C", "D", "E", "F", "G", "H")
|
||||
sheetConf("AlwaysQuote") = True
|
||||
sheetConf("FilterRow") = 6
|
||||
sheetConf("KeyCol") = 3
|
||||
sheetConf("ValueCols") = Array(4)
|
||||
Set sheetConfDict("Z3") = sheetConf
|
||||
Debug.Print "RefreshSheetDict Z3 ok."
|
||||
|
||||
' Z4
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartCol") = "C"
|
||||
sheetConf("EndCol") = "I"
|
||||
sheetConf("ErrorCol") = "B"
|
||||
sheetConf("StartRow") = 7
|
||||
sheetConf("HeaderRow") = 5
|
||||
sheetConf("CSV_Encoding") = "utf-8"
|
||||
sheetConf("HasHeader") = False
|
||||
sheetConf("ExpectedColumnCount") = 7
|
||||
sheetConf("HeaderColumns") = Array("C", "D", "E", "F", "G", "H", "I")
|
||||
sheetConf("AlwaysQuote") = True
|
||||
sheetConf("FilterRow") = 6
|
||||
sheetConf("KeyCol") = 3
|
||||
sheetConf("ValueCols") = Array(4)
|
||||
Set sheetConfDict("Z4") = sheetConf
|
||||
Debug.Print "RefreshSheetDict Z4 ok."
|
||||
|
||||
' T1
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartCol") = "C"
|
||||
sheetConf("EndCol") = "G"
|
||||
sheetConf("ErrorCol") = "B"
|
||||
sheetConf("StartRow") = 7
|
||||
sheetConf("HeaderRow") = 5
|
||||
sheetConf("CSV_Encoding") = "utf-8"
|
||||
sheetConf("HasHeader") = False
|
||||
sheetConf("ExpectedColumnCount") = 5
|
||||
sheetConf("HeaderColumns") = Array("C", "D", "E", "F", "G")
|
||||
sheetConf("AlwaysQuote") = True
|
||||
sheetConf("FilterRow") = 6
|
||||
sheetConf("KeyCol") = 3
|
||||
sheetConf("ValueCols") = Array(4)
|
||||
Set sheetConfDict("T1") = sheetConf
|
||||
Debug.Print "RefreshSheetDict T1 ok."
|
||||
|
||||
' T2
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartCol") = "C"
|
||||
sheetConf("EndCol") = "M"
|
||||
sheetConf("ErrorCol") = "B"
|
||||
sheetConf("StartRow") = 7
|
||||
sheetConf("HeaderRow") = 5
|
||||
sheetConf("CSV_Encoding") = "utf-8"
|
||||
sheetConf("HasHeader") = False
|
||||
sheetConf("ExpectedColumnCount") = 11
|
||||
sheetConf("HeaderColumns") = Array("C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M")
|
||||
sheetConf("AlwaysQuote") = True
|
||||
sheetConf("FilterRow") = 6
|
||||
sheetConf("KeyCol") = 3
|
||||
sheetConf("ValueCols") = Array(4, 6, 8, 9, 10, 11, 12, 13)
|
||||
Set sheetConfDict("T2") = sheetConf
|
||||
Debug.Print "RefreshSheetDict T2 ok."
|
||||
|
||||
' T3
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartCol") = "C"
|
||||
sheetConf("EndCol") = "I"
|
||||
sheetConf("ErrorCol") = "B"
|
||||
sheetConf("StartRow") = 7
|
||||
sheetConf("HeaderRow") = 5
|
||||
sheetConf("CSV_Encoding") = "utf-8"
|
||||
sheetConf("HasHeader") = False
|
||||
sheetConf("ExpectedColumnCount") = 7
|
||||
sheetConf("HeaderColumns") = Array("C", "D", "E", "F", "G", "H", "I")
|
||||
sheetConf("AlwaysQuote") = True
|
||||
sheetConf("FilterRow") = 6
|
||||
sheetConf("KeyCol") = 3
|
||||
sheetConf("ValueCols") = Array(4, 8, 9)
|
||||
Set sheetConfDict("T3") = sheetConf
|
||||
Debug.Print "RefreshSheetDict T3 ok."
|
||||
|
||||
' O1
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartCol") = "C"
|
||||
sheetConf("EndCol") = "F"
|
||||
sheetConf("ErrorCol") = ""
|
||||
sheetConf("StartRow") = 6
|
||||
sheetConf("HeaderRow") = ""
|
||||
sheetConf("CSV_Encoding") = "utf-8"
|
||||
sheetConf("HasHeader") = False
|
||||
sheetConf("ExpectedColumnCount") = 4
|
||||
sheetConf("HeaderColumns") = Array("C", "D", "E", "F")
|
||||
sheetConf("AlwaysQuote") = True
|
||||
sheetConf("FilterRow") = 5
|
||||
Set sheetConfDict("O1") = sheetConf
|
||||
Debug.Print "RefreshSheetDict O1 ok."
|
||||
|
||||
' O2
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartCol") = "C"
|
||||
sheetConf("EndCol") = "O"
|
||||
sheetConf("ErrorCol") = ""
|
||||
sheetConf("StartRow") = 6
|
||||
sheetConf("HeaderRow") = ""
|
||||
sheetConf("CSV_Encoding") = "utf-8"
|
||||
sheetConf("HasHeader") = False
|
||||
sheetConf("ExpectedColumnCount") = 13
|
||||
sheetConf("HeaderColumns") = Array("C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O")
|
||||
sheetConf("AlwaysQuote") = True
|
||||
sheetConf("FilterRow") = 5
|
||||
sheetConf("KeyCol") = 3
|
||||
sheetConf("ValueCols") = Array(4)
|
||||
Set sheetConfDict("O2") = sheetConf
|
||||
Debug.Print "RefreshSheetDict O2 ok."
|
||||
|
||||
' Enum
|
||||
Set sheetConf = Nothing
|
||||
sheetConfDict("Enum") = Array("tokubetuList", "kenshuList", "oufukuList", "koutaiList", "higaitouList", "errorList")
|
||||
Debug.Print "RefreshSheetDict Enum ok."
|
||||
|
||||
' tokubetuList
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartRow") = 3
|
||||
sheetConf("KeyCol") = 1
|
||||
sheetConf("ValueCols") = Array(1)
|
||||
Set sheetConfDict("tokubetuList") = sheetConf
|
||||
Debug.Print "RefreshSheetDict tokubetuList ok."
|
||||
|
||||
' kenshuList
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartRow") = 3
|
||||
sheetConf("KeyCol") = 3
|
||||
sheetConf("ValueCols") = Array(4)
|
||||
Set sheetConfDict("kenshuList") = sheetConf
|
||||
Debug.Print "RefreshSheetDict kenshuList ok."
|
||||
|
||||
' oufukuList
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartRow") = 3
|
||||
sheetConf("KeyCol") = 6
|
||||
sheetConf("ValueCols") = Array(7)
|
||||
Set sheetConfDict("oufukuList") = sheetConf
|
||||
Debug.Print "RefreshSheetDict oufukuList ok."
|
||||
|
||||
' koutaiList
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartRow") = 3
|
||||
sheetConf("KeyCol") = 9
|
||||
sheetConf("ValueCols") = Array(10)
|
||||
Set sheetConfDict("koutaiList") = sheetConf
|
||||
Debug.Print "RefreshSheetDict koutaiList ok."
|
||||
|
||||
' higaitouList
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartRow") = 3
|
||||
sheetConf("KeyCol") = 12
|
||||
sheetConf("ValueCols") = Array(13)
|
||||
Set sheetConfDict("higaitouList") = sheetConf
|
||||
Debug.Print "RefreshSheetDict higaitouList ok."
|
||||
|
||||
' errorList
|
||||
Set sheetConf = CreateObject("Scripting.Dictionary")
|
||||
sheetConf("StartRow") = 3
|
||||
sheetConf("KeyCol") = 15
|
||||
sheetConf("ValueCols") = Array(16)
|
||||
Set sheetConfDict("errorList") = sheetConf
|
||||
Debug.Print "RefreshSheetDict errorList ok."
|
||||
|
||||
Debug.Print "RefreshSheetDict end."
|
||||
End Sub
|
||||
|
||||
Public Function GetSheetConfig() As Object
|
||||
If sheetConfDict Is Nothing Then Call RefreshSheetDict
|
||||
Set GetSheetConfig = sheetConfDict
|
||||
End Function
|
||||
|
||||
Public Function RefreshAllCache() As Boolean
|
||||
' refresh
|
||||
Dim refreshCacheNames As Variant
|
||||
refreshCacheNames = Array("Z1", "Z2", "Z3", "Z4", "T1", "T2", "T3", "M1", "M1KukanDCache", "M2", "O1","O2", _
|
||||
"tokubetuList", "kenshuList", "oufukuList", "koutaiList", "higaitouList", "errorList")
|
||||
Dim refreshCacheName As Variant
|
||||
For Each refreshCacheName In refreshCacheNames
|
||||
Call RefreshCache(refreshCacheName)
|
||||
Next refreshCacheName
|
||||
|
||||
RefreshAllCache = True
|
||||
End Function
|
||||
@@ -0,0 +1,161 @@
|
||||
Attribute VB_Name = "Common_Selector"
|
||||
Option Explicit
|
||||
' ============================================================
|
||||
' Module Name: Build_Select
|
||||
' Module Desc: Commuter allowance editing sheet (no CSV import)
|
||||
' Module Methods:
|
||||
' - Tukin_ValidateRow
|
||||
' - FillTransportFromM1KukanD
|
||||
' - FillDepartureFromM1KukanD
|
||||
' - FillArrivalFromM1KukanD
|
||||
' - FillKukanFromM1
|
||||
' - FillKanshuFromM2
|
||||
' - FillCodeFromM2
|
||||
' - FillAddressFromO1
|
||||
' - FillZ1Dropdown
|
||||
' ============================================================
|
||||
|
||||
' Create transport (T) dropdown from Z1 cache
|
||||
Public Function BuildTransportList()
|
||||
Dim z1Cache As Object: Set z1Cache = GetCache("Z1")
|
||||
|
||||
Dim dropdownList As String
|
||||
Dim key As Variant
|
||||
For Each key In z1Cache.Keys
|
||||
Dim displayText As String
|
||||
displayText = MakeSelect(key, z1Cache(key)(0))
|
||||
If dropdownList = "" Then
|
||||
dropdownList = displayText
|
||||
Else
|
||||
dropdownList = dropdownList & "," & displayText
|
||||
End If
|
||||
Next key
|
||||
|
||||
BuildTransportList = dropdownList
|
||||
End Function
|
||||
|
||||
' Create todoke (G) dropdown
|
||||
Public Function BuildTodokeList()
|
||||
Dim z4Cache As Object: Set z4Cache = GetCache("Z4")
|
||||
|
||||
Dim dropdownList As String
|
||||
Dim key As Variant
|
||||
For Each key In z4Cache.Keys
|
||||
Dim displayText As String
|
||||
displayText = MakeSelect(key, z4Cache(key)(0))
|
||||
If dropdownList = "" Then
|
||||
dropdownList = displayText
|
||||
Else
|
||||
dropdownList = dropdownList & "," & displayText
|
||||
End If
|
||||
Next key
|
||||
BuildTodokeList = dropdownList
|
||||
End Function
|
||||
|
||||
' Create oufuku (M) dropdown
|
||||
Public Function BuildOufukuList()
|
||||
Dim oufukuList As Object: Set oufukuList = GetCache("oufukuList")
|
||||
|
||||
Dim dropdownList As String
|
||||
Dim key As Variant
|
||||
For Each key In oufukuList.Keys
|
||||
Dim displayText As String
|
||||
displayText = MakeSelect(key, oufukuList(key)(0))
|
||||
If dropdownList = "" Then
|
||||
dropdownList = displayText
|
||||
Else
|
||||
dropdownList = dropdownList & "," & displayText
|
||||
End If
|
||||
Next key
|
||||
BuildOufukuList = dropdownList
|
||||
End Function
|
||||
|
||||
' Create Koutai (N) dropdown
|
||||
Public Function BuildKoutaiList()
|
||||
Dim koutaiList As Object: Set koutaiList = GetCache("koutaiList")
|
||||
|
||||
Dim dropdownList As String
|
||||
Dim key As Variant
|
||||
For Each key In koutaiList.Keys
|
||||
Dim displayText As String
|
||||
displayText = MakeSelect(key, koutaiList(key)(0))
|
||||
If dropdownList = "" Then
|
||||
dropdownList = displayText
|
||||
Else
|
||||
dropdownList = dropdownList & "," & displayText
|
||||
End If
|
||||
Next key
|
||||
BuildKoutaiList = dropdownList
|
||||
End Function
|
||||
|
||||
' Create Kettei (AU) dropdown
|
||||
Public Function BuildKetteiList()
|
||||
Dim z2Cache As Object: Set z2Cache = GetCache("Z2")
|
||||
|
||||
Dim dropdownList As String
|
||||
Dim key As Variant
|
||||
For Each key In z2Cache.Keys
|
||||
Dim displayText As String
|
||||
displayText = MakeSelect(key, z2Cache(key)(0))
|
||||
If dropdownList = "" Then
|
||||
dropdownList = displayText
|
||||
Else
|
||||
dropdownList = dropdownList & "," & displayText
|
||||
End If
|
||||
Next key
|
||||
BuildKetteiList = dropdownList
|
||||
End Function
|
||||
|
||||
' Create Higaitou (AW) dropdown
|
||||
Public Function BuildHigaitouList()
|
||||
Dim higaitouList As Object: Set higaitouList = GetCache("higaitouList")
|
||||
|
||||
Dim dropdownList As String
|
||||
Dim key As Variant
|
||||
For Each key In higaitouList.Keys
|
||||
Dim displayText As String
|
||||
displayText = MakeSelect(key, higaitouList(key)(0))
|
||||
If dropdownList = "" Then
|
||||
dropdownList = displayText
|
||||
Else
|
||||
dropdownList = dropdownList & "," & displayText
|
||||
End If
|
||||
Next key
|
||||
BuildHigaitouList = dropdownList
|
||||
End Function
|
||||
|
||||
' Create MonthAmountKbn (AX) dropdown
|
||||
Public Function BuildMonthAmountKbnList()
|
||||
Dim z3Cache As Object: Set z3Cache = GetCache("Z3")
|
||||
|
||||
Dim dropdownList As String
|
||||
Dim key As Variant
|
||||
For Each key In z3Cache.Keys
|
||||
Dim displayText As String
|
||||
displayText = MakeSelect(key, z3Cache(key)(0))
|
||||
If dropdownList = "" Then
|
||||
dropdownList = displayText
|
||||
Else
|
||||
dropdownList = dropdownList & "," & displayText
|
||||
End If
|
||||
Next key
|
||||
BuildMonthAmountKbnList = dropdownList
|
||||
End Function
|
||||
|
||||
' Create Kanshoku (BC) dropdown
|
||||
Public Function BuildKanshokuList()
|
||||
Dim o2Cache As Object: Set o2Cache = GetCache("O2")
|
||||
|
||||
Dim dropdownList As String
|
||||
Dim key As Variant
|
||||
For Each key In o2Cache.Keys
|
||||
Dim displayText As String
|
||||
displayText = MakeSelect(key, o2Cache(key)(0))
|
||||
If dropdownList = "" Then
|
||||
dropdownList = displayText
|
||||
Else
|
||||
dropdownList = dropdownList & "," & displayText
|
||||
End If
|
||||
Next key
|
||||
BuildKanshokuList = dropdownList
|
||||
End Function
|
||||
Reference in New Issue
Block a user