20260515指摘対応5

This commit is contained in:
guanxiangwei
2026-05-20 14:33:18 +09:00
parent b359ae916b
commit b25db7d99c
9 changed files with 348 additions and 198 deletions

View File

@@ -31,58 +31,104 @@ Sub Fit_Button()
End Sub
Sub RefreshCache_Button()
On Error GoTo ErrorHandler
Dim exitMsg As String
Dim activeSheetName As String: activeSheetName = ActiveSheet.CodeName
' Determine which cache sheets to refresh based on ActiveSheet
Dim cacheSheets As Variant
If activeSheetName = "C1" Then
cacheSheets = Array("M1", "M2", "Z1", "Z2", "Z3", "Z4", "T1", "T2", "T3", "O1", "O2")
ElseIf activeSheetName = "M1" Then
cacheSheets = Array("Z1", "Z2", "Z3", "Z4", "T1", "T2", "T3", "O1", "O2")
ElseIf activeSheetName = "M2" Then
cacheSheets = Array("M1", "Z1", "Z2", "Z3", "Z4", "T1", "T2", "T3", "O1", "O2")
Else
MsgBox "This sheet does not support cache refresh.", vbExclamation
Exit Sub
End If
' Validate and refresh cache
Debug.Print "1. Validate Z1~Z4, T1~T3, O1~O2 master data"
Dim cacheSheets As Variant: cacheSheets = Array("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
Set ws = ThisWorkbook.Worksheets(CStr(sheetName))
Dim result As Long: result = RunValidationSilent(ws)
If result = 0 Then
Err.Raise ERR_CACHE_EMPTY, "RefreshCache_Button", sheetName & " sheet has no data."
End If
If result < 0 Then
Err.Raise ERR_CACHE_EMPTY, "RefreshCache_Button", "Can't refresh " & sheetName & " cache. Validation error occurs."
End If
Next sheetName
Debug.Print "2. refresh master data"
Call RefreshMasterCache()
' Refresh cache based on activeSheet
Dim result As Boolean: result = RefreshAllCache(activeSheetName)
If result = True Then
' Call active sheet's Refresh method
If ProcedureExists(activeSheetName, "Refresh") Then
On Error Resume Next
Set ws = ActiveSheet
On Error GoTo 0
If Not ws Is Nothing Then
Dim sheetConfDict As Object: Set sheetConfDict = GetSheetConfig()
Dim sheetConf As Object: Set sheetConf = sheetConfDict(activeSheetName)
Dim startRow As Long: startRow = sheetConf("StartRow")
Dim lastDataRow As Long: lastDataRow = GetLastDataRowInRange(ws)
Application.Run activeSheetName & ".Refresh", ws, startRow, lastDataRow
End If
Else
MsgBox "Sheet " & activeSheetName & " does not implement Refresh method.", vbExclamation
End If
MsgBox "master data reload successfully."
Debug.Print "3. Determine which cache sheets to refresh based on ActiveSheet"
Dim refSheets As Variant
If activeSheetName = "C1" Then
' first is M1
Call ValidateKukanCache("M1")
Call RefreshKukanCache("M1")
Call UpdateByMaster("M1")
' second is M2
Call ValidateKukanCache("M2")
Call RefreshKukanCache("M2")
Call UpdateByMaster("M2")
ElseIf activeSheetName = "M2" Then
Call ValidateKukanCache("M1")
Call RefreshKukanCache("M1")
Call UpdateByMaster("M1")
End If
Debug.Print "4. update content by other master data"
Call UpdateByMaster(activeSheetName)
Exit Sub
ErrorHandler:
HandleError "RefreshCache_Button"
If exitMsg <> "" Then
MsgBox "RefreshCache_Button: " & exitMsg, vbExclamation
Else
MsgBox "RefreshCache_Button: " & Err.Description, vbExclamation
End If
End Sub
Private Sub ValidateKukanCache(ByVal sheetName As String)
On Error GoTo ErrorHandler
Dim exitMsg As String
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets(CStr(sheetName))
Dim result As Long: result = RunValidationSilent(ws)
If result = 0 Then
exitMsg = sheetName & " sheet has no data."
GoTo ErrorHandler
End If
If result < 0 Then
exitMsg = "Can't refresh " & sheetName & " cache. Validation error occurs."
GoTo ErrorHandler
End If
Exit Sub
ErrorHandler:
If exitMsg <> "" Then
MsgBox "ValidateKukanCache: " & exitMsg, vbExclamation
Else
MsgBox "ValidateKukanCache: " & Err.Description, vbExclamation
End If
End Sub
Private Sub UpdateByMaster(ByVal sheetName As String)
On Error GoTo ErrorHandler
Dim exitMsg As String
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets(CStr(sheetName))
Dim sheetConfDict As Object: Set sheetConfDict = GetSheetConfig()
Dim sheetConf As Object: Set sheetConf = sheetConfDict(sheetName)
Dim startRow As Long: startRow = sheetConf("StartRow")
Dim lastDataRow As Long: lastDataRow = GetLastDataRowInRange(ws)
Application.Run sheetName & ".Refresh", ws, startRow, lastDataRow
Exit Sub
ErrorHandler:
If exitMsg <> "" Then
MsgBox "UpdateByMaster: " & exitMsg, vbExclamation
Else
MsgBox "UpdateByMaster: " & Err.Description, vbExclamation
End If
End Sub
@@ -149,27 +195,28 @@ End Sub
'
Private Sub Do_Validation(ws As Excel.Worksheet)
On Error GoTo ErrorHandler
Dim exitMsg As String
' 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
Dim result As Long: result = RunValidationSilent(ws)
If result = -1 Then
exitMsg = "Validation has errors."
GoTo ErrorHandler
ElseIf result = 0 Then
exitMsg = "No data to validate."
GoTo ErrorHandler
Else
If ws.CodeName <> "C1" Then
RefreshCache(ws.CodeName)
WriteCachesSheet(ws.CodeName)
End If
MsgBox "Validation complete. Errors: 0", vbInformation
MsgBox "Validation complete. Success: " & result, vbInformation
End If
' step2. ValidateWarn for M1 sheet
If ws.CodeName = "M1" Then
Dim lastDataRow As Long: lastDataRow = GetLastDataRowInRange(ws)
Application.Run "M1.ValidateWarn", ws, lastDataRow
End If
Do_Fit ws
@@ -177,43 +224,41 @@ Private Sub Do_Validation(ws As Excel.Worksheet)
Exit Sub
ErrorHandler:
MsgBox "Error: " & Err.Description, vbCritical
Exit Sub
HandleError "Do_Validation"
' If exitMsg <> "" Then
' MsgBox "Do_Validation: " & exitMsg, vbExclamation
' Else
' MsgBox "Do_Validation: " & Err.Description, vbExclamation
' End If
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)
' === Step 1: Validate all rows before export ===
Dim result As Long: result = RunValidationSilent(ws)
Dim lastDataRow As Long: lastDataRow = GetLastDataRowInRange(ws)
Dim startRow As Long: startRow = sheetConf("StartRow")
If lastDataRow < startRow Then
If result = 0 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
If result < 0 Then
MsgBox "Validation failed. Export aborted.", vbCritical
Exit Sub
End If
' === Step 2: Select save path ===
Dim savePath As String: savePath = GetSaveCSVPath()
If savePath = "" Then Exit Sub
Dim sheetConfDict As Object: Set sheetConfDict = GetSheetConfig()
Dim sheetConf As Object: Set sheetConf = sheetConfDict(ws.CodeName)
' === Step 3: Count data rows ===
Dim lastDataRow As Long: lastDataRow = GetLastDataRowInRange(ws)
Dim startRow As Long: startRow = sheetConf("StartRow")
Dim rowCount As Long: rowCount = lastDataRow - startRow + 1
' === Step 4: Count data columns ===
@@ -333,61 +378,50 @@ ErrorHandler:
End Sub
' RunValidationSilent
Public Function RunValidationSilent(ws As Worksheet, Optional ByRef errorCountOut As Long = 0) As Boolean
On Error GoTo ErrorHandler
' Positive number = success (number of rows with no errors)
' 0 = no data
' -1 = has errors
' -2 = runtime error
Public Function RunValidationSilent(ws As Worksheet) As Long
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
RunValidationSilent = 0
Exit Function
End If
Dim r As Long
errorCountOut = 0
Dim hasError As Boolean: hasError = False
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
hasError = True
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)
If hasError = True Then
RunValidationSilent = -1
Exit Function
End If
If Err.Number <> 0 Then ProcedureExists = False
On Error GoTo 0
End Function
RunValidationSilent = lastDataRow - startRow + 1
Exit Function
End Function
Public Sub HandleError(sourceProcedure As String)
Dim msg As String
Select Case Err.Number
Case ERR_CACHE_EMPTY
msg = Err.Description
MsgBox msg, vbExclamation
End Select
End Sub