Optimize validateDetailData: simplify logic and parameters

This commit is contained in:
updsv7
2026-04-13 11:31:05 +09:00
parent f8f9a0bf79
commit 64139ae32c

View File

@@ -285,126 +285,64 @@ Sub ClearRowData(ByVal ws As Worksheet, ByVal rowNum As Long)
ws.Cells(rowNum, 17).ClearContents ' Q column error info
End Sub
Sub validateDetailData(ByVal ws As Worksheet, ByVal rowNum As Long, Optional ByVal isImport As Boolean = False)
' Disable screen update and calculation for performance
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Sub validateDetailData(ByVal ws As Worksheet, ByVal rowNum As Long)
' Check C column not empty
If Trim(ws.Cells(rowNum, 3).Value) = "" Then
ws.Cells(rowNum, 17).ClearContents
Exit Sub
End If
Dim cValue As String
Dim jValue As String
Dim kValue As String
Dim lValue As String
Dim mValue As String
Dim nValue As String
Dim oValue As String
Dim pValue As String
Dim gValue As String
Dim hValue As String
Dim iValue As String
Dim i As Long
Dim lastRow As Long
Dim errorMsg As String
' Check J, K required and numeric
If Trim(ws.Cells(rowNum, 10).Value) = "" Or Not IsNumeric(ws.Cells(rowNum, 10).Value) Then
ws.Cells(rowNum, 17).Value = "J column is required and must be numeric"
Exit Sub
End If
cValue = Trim(ws.Cells(rowNum, 3).Value)
If cValue = "" Then
ws.Cells(rowNum, 17).Value = ""
If Trim(ws.Cells(rowNum, 11).Value) = "" Or Not IsNumeric(ws.Cells(rowNum, 11).Value) Then
ws.Cells(rowNum, 17).Value = "K column is required and must be numeric"
Exit Sub
End If
' Check L-P optional but must be numeric if entered
Dim col As Long
Dim colName As String
Dim colLetter As String
colLetter = "LMNOP"
For col = 12 To 16
If Trim(ws.Cells(rowNum, col).Value) <> "" And Not IsNumeric(ws.Cells(rowNum, col).Value) Then
colName = Mid(colLetter, col - 11, 1)
ws.Cells(rowNum, 17).Value = colName & " column must be numeric"
Exit Sub
End If
End If
Next col
' === J and K columns must be numeric (required) ===
jValue = Trim(ws.Cells(rowNum, 10).Value)
kValue = Trim(ws.Cells(rowNum, 11).Value)
' Check GHI composite key duplicate
Dim g As String, h As String, i As String
Dim r As Long
Dim lastRow As Long
If jValue = "" Then
errorMsg = "J column is required。"
ws.Cells(rowNum, 17).Value = errorMsg
Exit Sub
End If
g = Trim(ws.Cells(rowNum, 7).Value)
h = Trim(ws.Cells(rowNum, 8).Value)
i = Trim(ws.Cells(rowNum, 9).Value)
If Not IsNumeric(jValue) Then
errorMsg = "J columnmust be numeric。"
ws.Cells(rowNum, 17).Value = errorMsg
Exit Sub
End If
If kValue = "" Then
errorMsg = "K column is required。"
ws.Cells(rowNum, 17).Value = errorMsg
Exit Sub
End If
If Not IsNumeric(kValue) Then
errorMsg = "K columnmust be numeric。"
ws.Cells(rowNum, 17).Value = errorMsg
Exit Sub
End If
' === L, M, N, O, P columns must be numeric if entered ===
lValue = Trim(ws.Cells(rowNum, 12).Value)
mValue = Trim(ws.Cells(rowNum, 13).Value)
nValue = Trim(ws.Cells(rowNum, 14).Value)
oValue = Trim(ws.Cells(rowNum, 15).Value)
pValue = Trim(ws.Cells(rowNum, 16).Value)
If lValue <> "" And Not IsNumeric(lValue) Then
errorMsg = "L columnmust be numeric。"
ws.Cells(rowNum, 17).Value = errorMsg
Exit Sub
End If
If mValue <> "" And Not IsNumeric(mValue) Then
errorMsg = "M columnmust be numeric。"
ws.Cells(rowNum, 17).Value = errorMsg
Exit Sub
End If
If nValue <> "" And Not IsNumeric(nValue) Then
errorMsg = "N columnmust be numeric。"
ws.Cells(rowNum, 17).Value = errorMsg
Exit Sub
End If
If oValue <> "" And Not IsNumeric(oValue) Then
errorMsg = "O columnmust be numeric。"
ws.Cells(rowNum, 17).Value = errorMsg
Exit Sub
End If
If pValue <> "" And Not IsNumeric(pValue) Then
errorMsg = "P columnmust be numeric。"
ws.Cells(rowNum, 17).Value = errorMsg
Exit Sub
End If
' === Check G, H, I composite key for duplicates ===
gValue = Trim(ws.Cells(rowNum, 7).Value)
hValue = Trim(ws.Cells(rowNum, 8).Value)
iValue = Trim(ws.Cells(rowNum, 9).Value)
If gValue <> "" And hValue <> "" And iValue <> "" Then
If g <> "" And h <> "" And i <> "" Then
lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
For i = 7 To lastRow
If i <> rowNum Then
If Trim(ws.Cells(i, 3).Value) = cValue Then
If Trim(ws.Cells(i, 7).Value) = gValue And _
Trim(ws.Cells(i, 8).Value) = hValue And _
Trim(ws.Cells(i, 9).Value) = iValue Then
errorMsg = "GHI columncombination already exists。"
ws.Cells(rowNum, 17).Value = errorMsg
Exit Sub
End If
For r = 7 To lastRow
If r <> rowNum And Trim(ws.Cells(r, 3).Value) = Trim(ws.Cells(rowNum, 3).Value) Then
If Trim(ws.Cells(r, 7).Value) = g And _
Trim(ws.Cells(r, 8).Value) = h And _
Trim(ws.Cells(r, 9).Value) = i Then
ws.Cells(rowNum, 17).Value = "GHI combination already exists"
Exit Sub
End If
End If
Next i
Next r
End If
' Validation passed, clear Q column error
ws.Cells(rowNum, 17).Value = ""
' Restore settings
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
' Validation passed
ws.Cells(rowNum, 17).ClearContents
End Sub