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 ws.Cells(rowNum, 17).ClearContents ' Q column error info
End Sub End Sub
Sub validateDetailData(ByVal ws As Worksheet, ByVal rowNum As Long, Optional ByVal isImport As Boolean = False) Sub validateDetailData(ByVal ws As Worksheet, ByVal rowNum As Long)
' Disable screen update and calculation for performance ' Check C column not empty
Application.ScreenUpdating = False If Trim(ws.Cells(rowNum, 3).Value) = "" Then
Application.Calculation = xlCalculationManual ws.Cells(rowNum, 17).ClearContents
Exit Sub
End If
Dim cValue As String ' Check J, K required and numeric
Dim jValue As String If Trim(ws.Cells(rowNum, 10).Value) = "" Or Not IsNumeric(ws.Cells(rowNum, 10).Value) Then
Dim kValue As String ws.Cells(rowNum, 17).Value = "J column is required and must be numeric"
Dim lValue As String Exit Sub
Dim mValue As String End If
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
cValue = Trim(ws.Cells(rowNum, 3).Value) If Trim(ws.Cells(rowNum, 11).Value) = "" Or Not IsNumeric(ws.Cells(rowNum, 11).Value) Then
If cValue = "" Then ws.Cells(rowNum, 17).Value = "K column is required and must be numeric"
ws.Cells(rowNum, 17).Value = "" 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 Exit Sub
End If End If
Next col
' === J and K columns must be numeric (required) === ' Check GHI composite key duplicate
jValue = Trim(ws.Cells(rowNum, 10).Value) Dim g As String, h As String, i As String
kValue = Trim(ws.Cells(rowNum, 11).Value) Dim r As Long
Dim lastRow As Long
If jValue = "" Then g = Trim(ws.Cells(rowNum, 7).Value)
errorMsg = "J column is required。" h = Trim(ws.Cells(rowNum, 8).Value)
ws.Cells(rowNum, 17).Value = errorMsg i = Trim(ws.Cells(rowNum, 9).Value)
Exit Sub
End If
If Not IsNumeric(jValue) Then If g <> "" And h <> "" And i <> "" 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
lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
For i = 7 To lastRow For r = 7 To lastRow
If i <> rowNum Then If r <> rowNum And Trim(ws.Cells(r, 3).Value) = Trim(ws.Cells(rowNum, 3).Value) Then
If Trim(ws.Cells(i, 3).Value) = cValue Then If Trim(ws.Cells(r, 7).Value) = g And _
If Trim(ws.Cells(i, 7).Value) = gValue And _ Trim(ws.Cells(r, 8).Value) = h And _
Trim(ws.Cells(i, 8).Value) = hValue And _ Trim(ws.Cells(r, 9).Value) = i Then
Trim(ws.Cells(i, 9).Value) = iValue Then ws.Cells(rowNum, 17).Value = "GHI combination already exists"
errorMsg = "GHI columncombination already exists。" Exit Sub
ws.Cells(rowNum, 17).Value = errorMsg
Exit Sub
End If
End If End If
End If End If
Next i Next r
End If End If
' Validation passed, clear Q column error ' Validation passed
ws.Cells(rowNum, 17).Value = "" ws.Cells(rowNum, 17).ClearContents
' Restore settings
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub End Sub