refactor
This commit is contained in:
116
src/sheet/Z1.cls
116
src/sheet/Z1.cls
@@ -2,64 +2,31 @@
|
||||
' Module Name: Master_222
|
||||
' Module Desc: Z1 master data management (222)
|
||||
' Module Methods:
|
||||
' - Import
|
||||
' - Export
|
||||
' - SortData
|
||||
' - ToggleAutoFilter
|
||||
' - Validate
|
||||
' ============================================================
|
||||
' ====== (222) =======
|
||||
'
|
||||
Public Sub Validate(ws As Worksheet, ByVal rowNum As Long, ByVal lastDataRow As Long)
|
||||
|
||||
' ====== Constants ======
|
||||
Const START_COL As Long = 3
|
||||
Const END_COL As Long = 9
|
||||
Const ERROR_COL As Long = 2
|
||||
Dim sheetConfDict As Object: Set sheetConfDict = GetSheetConfig()
|
||||
Dim sheetConf As Object: Set sheetConf = sheetConfDict(ws.CodeName)
|
||||
|
||||
' ====== Function ======
|
||||
Private Sub Import()
|
||||
Call Generic_Master_Import(Me, 7)
|
||||
End Sub
|
||||
|
||||
Private Sub Export()
|
||||
Dim lastDataRow As Long: lastDataRow = GetLastDataRowInRange(Me, START_COL, END_COL)
|
||||
|
||||
If lastDataRow < 7 Then
|
||||
MsgBox "No data rows to output.", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Dim r As Long, errorCount As Long
|
||||
For r = 7 To lastDataRow
|
||||
Validate r
|
||||
If Trim(Cells(r, 2).Value & "") <> "" Then
|
||||
errorCount = errorCount + 1
|
||||
End If
|
||||
Next r
|
||||
|
||||
If errorCount > 0 Then
|
||||
MsgBox "Validation failed. Found " & errorCount & " error(s). Please correct them before exporting.", vbCritical
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
Call Generic_Master_Export(Me, 7, lastDataRow)
|
||||
End Sub
|
||||
|
||||
Private Sub validate(ByVal rowNum As Long)
|
||||
Set ws = Me
|
||||
Dim cValue As String: cValue = Trim(ws.Cells(rowNum, 3).Value)
|
||||
Dim startCol As String: startCol = sheetConf("StartCol")
|
||||
Dim endCol As String: endCol = sheetConf("EndCol")
|
||||
Dim errorCol As String: errorCol = sheetConf("ErrorCol")
|
||||
|
||||
' clear C~I columns background color
|
||||
Dim clearRange As Range
|
||||
Set clearRange = ws.Range(ws.Cells(rowNum, START_COL), ws.Cells(rowNum, END_COL))
|
||||
Dim clearRange As Range: Set clearRange = ws.Range(ws.Cells(rowNum, startCol), ws.Cells(rowNum, endCol))
|
||||
clearRange.Interior.Color = vbWhite
|
||||
|
||||
Dim cValue As String: cValue = Trim(ws.Cells(rowNum, 3).Value)
|
||||
If cValue = "" Then
|
||||
ws.Cells(rowNum, 2).Value = "C column is required"
|
||||
ws.Cells(rowNum, errorCol).Value = "C column is required"
|
||||
ws.Cells(rowNum, 3).Interior.Color = RGB(255, 0, 0)
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
If Len(cValue) <> 3 Then
|
||||
ws.Cells(rowNum, 2).Value = "C column must be 3 characters"
|
||||
ws.Cells(rowNum, errorCol).Value = "C column must be 3 characters"
|
||||
ws.Cells(rowNum, 3).Interior.Color = RGB(255, 0, 0)
|
||||
Exit Sub
|
||||
End If
|
||||
@@ -69,7 +36,7 @@ Private Sub validate(ByVal rowNum As Long)
|
||||
For i = 1 To 3
|
||||
ch = Mid(cValue, 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, 2).Value = "C column must be alphanumeric"
|
||||
ws.Cells(rowNum, errorCol).Value = "C column must be alphanumeric"
|
||||
ws.Cells(rowNum, 3).Interior.Color = RGB(255, 0, 0)
|
||||
Exit Sub
|
||||
End If
|
||||
@@ -78,12 +45,12 @@ Private Sub validate(ByVal rowNum As Long)
|
||||
Dim dValue As String
|
||||
dValue = Trim(ws.Cells(rowNum, 4).Value)
|
||||
If dValue = "" Then
|
||||
ws.Cells(rowNum, 2).Value = "D column is required"
|
||||
ws.Cells(rowNum, errorCol).Value = "D column is required"
|
||||
ws.Cells(rowNum, 4).Interior.Color = RGB(255, 0, 0)
|
||||
Exit Sub
|
||||
End If
|
||||
If Len(dValue) > 80 Then
|
||||
ws.Cells(rowNum, 2).Value = "D column must be within 80 characters"
|
||||
ws.Cells(rowNum, errorCol).Value = "D column must be within 80 characters"
|
||||
ws.Cells(rowNum, 4).Interior.Color = RGB(255, 0, 0)
|
||||
Exit Sub
|
||||
End If
|
||||
@@ -91,12 +58,12 @@ Private Sub validate(ByVal rowNum As Long)
|
||||
Dim eValue As String
|
||||
eValue = Trim(ws.Cells(rowNum, 5).Value)
|
||||
If eValue = "" Then
|
||||
ws.Cells(rowNum, 2).Value = "E column is required"
|
||||
ws.Cells(rowNum, errorCol).Value = "E column is required"
|
||||
ws.Cells(rowNum, 5).Interior.Color = RGB(255, 0, 0)
|
||||
Exit Sub
|
||||
End If
|
||||
If Len(eValue) > 80 Then
|
||||
ws.Cells(rowNum, 2).Value = "E column must be within 80 characters"
|
||||
ws.Cells(rowNum, errorCol).Value = "E column must be within 80 characters"
|
||||
ws.Cells(rowNum, 5).Interior.Color = RGB(255, 0, 0)
|
||||
Exit Sub
|
||||
End If
|
||||
@@ -104,7 +71,7 @@ Private Sub validate(ByVal rowNum As Long)
|
||||
Dim fValue As String
|
||||
fValue = Trim(ws.Cells(rowNum, 6).Value)
|
||||
If fValue <> "" And Len(fValue) > 80 Then
|
||||
ws.Cells(rowNum, 2).Value = "F column must be within 80 characters"
|
||||
ws.Cells(rowNum, errorCol).Value = "F column must be within 80 characters"
|
||||
ws.Cells(rowNum, 6).Interior.Color = RGB(255, 0, 0)
|
||||
Exit Sub
|
||||
End If
|
||||
@@ -112,7 +79,7 @@ Private Sub validate(ByVal rowNum As Long)
|
||||
Dim gValue As String
|
||||
gValue = Trim(ws.Cells(rowNum, 7).Value)
|
||||
If gValue <> "" And Len(gValue) > 80 Then
|
||||
ws.Cells(rowNum, 2).Value = "G column must be within 80 characters"
|
||||
ws.Cells(rowNum, errorCol).Value = "G column must be within 80 characters"
|
||||
ws.Cells(rowNum, 7).Interior.Color = RGB(255, 0, 0)
|
||||
Exit Sub
|
||||
End If
|
||||
@@ -121,12 +88,12 @@ Private Sub validate(ByVal rowNum As Long)
|
||||
hValue = Trim(ws.Cells(rowNum, 8).Value)
|
||||
If hValue <> "" Then
|
||||
If Len(hValue) <> 1 Then
|
||||
ws.Cells(rowNum, 2).Value = "H column must be 1 digit"
|
||||
ws.Cells(rowNum, errorCol).Value = "H column must be 1 digit"
|
||||
ws.Cells(rowNum, 8).Interior.Color = RGB(255, 0, 0)
|
||||
Exit Sub
|
||||
End If
|
||||
If hValue <> "0" And hValue <> "1" Then
|
||||
ws.Cells(rowNum, 2).Value = "H column must be 0 or 1"
|
||||
ws.Cells(rowNum, errorCol).Value = "H column must be 0 or 1"
|
||||
ws.Cells(rowNum, 8).Interior.Color = RGB(255, 0, 0)
|
||||
Exit Sub
|
||||
End If
|
||||
@@ -135,47 +102,10 @@ Private Sub validate(ByVal rowNum As Long)
|
||||
Dim iValue As String
|
||||
iValue = Trim(ws.Cells(rowNum, 9).Value)
|
||||
If iValue <> "" And Len(iValue) > 80 Then
|
||||
ws.Cells(rowNum, 2).Value = "I column must be within 80 characters"
|
||||
ws.Cells(rowNum, errorCol).Value = "I column must be within 80 characters"
|
||||
ws.Cells(rowNum, 9).Interior.Color = RGB(255, 0, 0)
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
ws.Cells(rowNum, 2).ClearContents
|
||||
ws.Cells(rowNum, errorCol).ClearContents
|
||||
End Sub
|
||||
|
||||
Private Sub validateButton()
|
||||
Dim lastDataRow As Long, r As Long, errorCount As Long
|
||||
lastDataRow = GetLastDataRowInRange(Me, START_COL, END_COL)
|
||||
|
||||
If lastDataRow < 7 Then
|
||||
MsgBox "No data found.", vbExclamation
|
||||
Exit Sub
|
||||
End If
|
||||
|
||||
errorCount = 0
|
||||
For r = 7 To lastDataRow
|
||||
Validate r
|
||||
If Trim(Cells(r, ERROR_COL).Value) <> "" Then
|
||||
errorCount = errorCount + 1
|
||||
End If
|
||||
Next r
|
||||
|
||||
' === Refresh Z1 cache after validation passes ===
|
||||
If errorCount = 0 Then
|
||||
Call RefreshZ1Cache
|
||||
End If
|
||||
|
||||
MsgBox "Validation complete. Errors: " & errorCount, vbInformation
|
||||
End Sub
|
||||
|
||||
Private Sub Do_Sort()
|
||||
Call SortDataRows(3)
|
||||
End Sub
|
||||
|
||||
Private Sub Do_Filter()
|
||||
Call ToggleAutoFilter(2, END_COL)
|
||||
End Sub
|
||||
|
||||
Private Sub Do_Fit()
|
||||
Call AutoFitColumnWidth(2, END_COL)
|
||||
End Sub
|
||||
Reference in New Issue
Block a user