update Generic Master bas2
This commit is contained in:
@@ -7,7 +7,7 @@ Sub Generic_Master_ClearRowData(ByVal ws As Worksheet, ByVal rowNum As Long)
|
|||||||
ws.Cells(rowNum, 2).ClearContents
|
ws.Cells(rowNum, 2).ClearContents
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Sub Generic_Master_Import(ByVal ws As Worksheet)
|
Sub Generic_Master_Import(ByVal ws As Worksheet, ByVal expectedColumnCount As Long)
|
||||||
Dim filePath As String
|
Dim filePath As String
|
||||||
Dim lines As Variant
|
Dim lines As Variant
|
||||||
Dim i As Long
|
Dim i As Long
|
||||||
@@ -20,7 +20,7 @@ Sub Generic_Master_Import(ByVal ws As Worksheet)
|
|||||||
If filePath = "" Then Exit Sub
|
If filePath = "" Then Exit Sub
|
||||||
|
|
||||||
' Step 2: Read CSV and return 2D array
|
' Step 2: Read CSV and return 2D array
|
||||||
lines = ReadCSVAs2DArrayStrict(filePath, 7, "utf-8")
|
lines = ReadCSVAs2DArrayStrict(filePath, expectedColumnCount, "utf-8")
|
||||||
|
|
||||||
' Step 3: Clear data rows
|
' Step 3: Clear data rows
|
||||||
Call ClearDataRows(ws, 7, 3)
|
Call ClearDataRows(ws, 7, 3)
|
||||||
@@ -29,15 +29,12 @@ Sub Generic_Master_Import(ByVal ws As Worksheet)
|
|||||||
writeRow = 7
|
writeRow = 7
|
||||||
For i = LBound(lines, 1) To UBound(lines, 1)
|
For i = LBound(lines, 1) To UBound(lines, 1)
|
||||||
If Not isRowEmpty Then
|
If Not isRowEmpty Then
|
||||||
ws.Cells(writeRow, 3).Value = CleanCSVField(CStr(lines(i, 1)))
|
Dim colOffset As Long
|
||||||
ws.Cells(writeRow, 4).Value = CleanCSVField(CStr(lines(i, 2)))
|
For colOffset = 1 To expectedColumnCount
|
||||||
ws.Cells(writeRow, 5).Value = CleanCSVField(CStr(lines(i, 3)))
|
ws.Cells(writeRow, 2 + colOffset).Value = CleanCSVField(CStr(lines(i, colOffset)))
|
||||||
ws.Cells(writeRow, 6).Value = CleanCSVField(CStr(lines(i, 4)))
|
|
||||||
ws.Cells(writeRow, 7).Value = CleanCSVField(CStr(lines(i, 5)))
|
writeRow = writeRow + 1
|
||||||
ws.Cells(writeRow, 8).Value = CleanCSVField(CStr(lines(i, 6)))
|
Next colOffset
|
||||||
ws.Cells(writeRow, 9).Value = CleanCSVField(CStr(lines(i, 7)))
|
|
||||||
|
|
||||||
writeRow = writeRow + 1
|
|
||||||
End If
|
End If
|
||||||
Next i
|
Next i
|
||||||
|
|
||||||
@@ -49,35 +46,13 @@ ErrorHandler:
|
|||||||
MsgBox "Import fails:" & vbCrLf & Err.Description, vbCritical
|
MsgBox "Import fails:" & vbCrLf & Err.Description, vbCritical
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Sub Generic_Master_Export(ByVal ws As Worksheet)
|
Sub Generic_Master_Export(ByVal ws As Worksheet, ByVal expectedColumnCount As Long, ByVal lastDataRow As Long)
|
||||||
Dim lastDataRow As Long
|
|
||||||
Dim savePath As String
|
Dim savePath As String
|
||||||
Dim r As Long
|
Dim r As Long
|
||||||
Dim rowCount As Long
|
Dim rowCount As Long
|
||||||
Dim dataArray() As Variant
|
Dim dataArray() As Variant
|
||||||
Dim dataIdx As Long
|
Dim dataIdx As Long
|
||||||
Dim j As Long
|
Dim j As Long
|
||||||
Dim errorCount As Long
|
|
||||||
|
|
||||||
lastDataRow = GetLastDataRow(ws, 3)
|
|
||||||
|
|
||||||
If lastDataRow < 7 Then
|
|
||||||
MsgBox "No data rows to output.", vbExclamation
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
errorCount = 0
|
|
||||||
For r = 7 To lastDataRow
|
|
||||||
Call Generic_Master_validate(ws, r)
|
|
||||||
If Trim(ws.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
|
|
||||||
|
|
||||||
savePath = GetSaveCSVPath()
|
savePath = GetSaveCSVPath()
|
||||||
If savePath = "" Then Exit Sub
|
If savePath = "" Then Exit Sub
|
||||||
@@ -96,15 +71,15 @@ Sub Generic_Master_Export(ByVal ws As Worksheet)
|
|||||||
Exit Sub
|
Exit Sub
|
||||||
End If
|
End If
|
||||||
|
|
||||||
' Initialize 2D array: (1 To rowCount, 1 To 7) for columns C-I (3 to 9)
|
' Initialize 2D array: (1 To rowCount, 1 To expectedColumnCount) for columns C-I (3 to expectedColumnCount + 2)
|
||||||
ReDim dataArray(1 To rowCount, 1 To 7)
|
ReDim dataArray(1 To rowCount, 1 To expectedColumnCount)
|
||||||
|
|
||||||
' Fill the array
|
' Fill the array
|
||||||
dataIdx = 0
|
dataIdx = 0
|
||||||
For r = 7 To lastDataRow
|
For r = 7 To lastDataRow
|
||||||
If Len(Trim(ws.Cells(r, 3).Value & "")) > 0 Then
|
If Len(Trim(ws.Cells(r, 3).Value & "")) > 0 Then
|
||||||
dataIdx = dataIdx + 1
|
dataIdx = dataIdx + 1
|
||||||
For j = 3 To 9
|
For j = 3 To expectedColumnCount + 2
|
||||||
dataArray(dataIdx, j - 2) = ws.Cells(r, j).Value ' C->1, D->2, ..., I->7
|
dataArray(dataIdx, j - 2) = ws.Cells(r, j).Value ' C->1, D->2, ..., I->7
|
||||||
Next j
|
Next j
|
||||||
End If
|
End If
|
||||||
@@ -114,127 +89,4 @@ Sub Generic_Master_Export(ByVal ws As Worksheet)
|
|||||||
Call WriteCSVFromArray(savePath, dataArray, "utf-8", True)
|
Call WriteCSVFromArray(savePath, dataArray, "utf-8", True)
|
||||||
|
|
||||||
MsgBox "CSV export completed. Total data rows: " & rowCount, vbInformation
|
MsgBox "CSV export completed. Total data rows: " & rowCount, vbInformation
|
||||||
End Sub
|
|
||||||
|
|
||||||
Sub Generic_Master_validate(ByVal ws As Worksheet, ByVal rowNum As Long)
|
|
||||||
Dim cValue As String
|
|
||||||
cValue = Trim(ws.Cells(rowNum, 3).Value)
|
|
||||||
|
|
||||||
' clear C~I columns background color
|
|
||||||
Dim clearRange As Range
|
|
||||||
Set clearRange = ws.Range(ws.Cells(rowNum, 3), ws.Cells(rowNum, 9))
|
|
||||||
clearRange.Interior.Color = vbWhite
|
|
||||||
|
|
||||||
If cValue = "" Then
|
|
||||||
ws.Cells(rowNum, 2).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, 3).Interior.Color = RGB(255, 0, 0)
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
Dim i As Long
|
|
||||||
Dim ch As String
|
|
||||||
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, 3).Interior.Color = RGB(255, 0, 0)
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
Next i
|
|
||||||
|
|
||||||
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, 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, 4).Interior.Color = RGB(255, 0, 0)
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
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, 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, 5).Interior.Color = RGB(255, 0, 0)
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
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, 6).Interior.Color = RGB(255, 0, 0)
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
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, 7).Interior.Color = RGB(255, 0, 0)
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
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, 9).Interior.Color = RGB(255, 0, 0)
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
Dim hValue As String
|
|
||||||
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, 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, 8).Interior.Color = RGB(255, 0, 0)
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
End If
|
|
||||||
|
|
||||||
ws.Cells(rowNum, 2).ClearContents
|
|
||||||
End Sub
|
|
||||||
|
|
||||||
Sub Generic_Master_validateButton(ByVal ws As Worksheet)
|
|
||||||
Dim lastRow As Long
|
|
||||||
Dim r As Long
|
|
||||||
Dim errorCount As Long
|
|
||||||
|
|
||||||
lastRow = GetLastDataRow(ws, 3)
|
|
||||||
|
|
||||||
If lastRow < 7 Then
|
|
||||||
MsgBox "No data found.", vbExclamation
|
|
||||||
Exit Sub
|
|
||||||
End If
|
|
||||||
|
|
||||||
errorCount = 0
|
|
||||||
For r = 7 To lastRow
|
|
||||||
Call Generic_Master_validate(ws, r)
|
|
||||||
If Trim(ws.Cells(r, 2).Value) <> "" Then
|
|
||||||
errorCount = errorCount + 1
|
|
||||||
End If
|
|
||||||
Next r
|
|
||||||
|
|
||||||
MsgBox "Validation complete. Errors: " & errorCount, vbInformation
|
|
||||||
End Sub
|
End Sub
|
||||||
@@ -62,6 +62,12 @@ Function ReadCSVAs2DArrayStrict( _
|
|||||||
Err.Raise 5003, , "CSV file is empty."
|
Err.Raise 5003, , "CSV file is empty."
|
||||||
End If
|
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 ===
|
' === loop the row, validate column count ===
|
||||||
Dim i As Long
|
Dim i As Long
|
||||||
For i = 1 To lines.Count
|
For i = 1 To lines.Count
|
||||||
@@ -76,9 +82,16 @@ Function ReadCSVAs2DArrayStrict( _
|
|||||||
Next i
|
Next i
|
||||||
|
|
||||||
Dim result As Variant
|
Dim result As Variant
|
||||||
ReDim result(1 To lines.Count, 1 To expectedColumnCount)
|
Dim startRow As Long
|
||||||
|
If hasHeader Then
|
||||||
For i = 1 To lines.Count
|
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)
|
rowArr = lines(i)
|
||||||
Dim j As Long
|
Dim j As Long
|
||||||
For j = LBound(rowArr) To UBound(rowArr)
|
For j = LBound(rowArr) To UBound(rowArr)
|
||||||
|
|||||||
@@ -4,19 +4,157 @@ Sub Z1_ClearRowData(ByVal ws As Worksheet, ByVal rowNum As Long)
|
|||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Sub Z1_Import()
|
Sub Z1_Import()
|
||||||
Call Generic_Master_Import(Me)
|
Call Generic_Master_Import(Me, 7)
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Sub Z1_Export()
|
Sub Z1_Export()
|
||||||
Call Generic_Master_Export(Me)
|
Dim lastDataRow As Long: lastDataRow = GetLastDataRow(Me, 3)
|
||||||
|
|
||||||
|
If lastDataRow < 7 Then
|
||||||
|
MsgBox "No data rows to output.", vbExclamation
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
Dim r As Long
|
||||||
|
Dim 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
|
End Sub
|
||||||
|
|
||||||
Sub Z1_validate(ByVal ws As Worksheet, ByVal rowNum As Long)
|
Sub Validate(ByVal rowNum As Long)
|
||||||
Call Generic_Master_validate(ws, rowNum)
|
Dim cValue As String
|
||||||
|
Set ws = Me
|
||||||
|
|
||||||
|
cValue = Trim(ws.Cells(rowNum, 3).Value)
|
||||||
|
|
||||||
|
' clear C~I columns background color
|
||||||
|
Dim clearRange As Range
|
||||||
|
Set clearRange = ws.Range(ws.Cells(rowNum, 3), ws.Cells(rowNum, 9))
|
||||||
|
clearRange.Interior.Color = vbWhite
|
||||||
|
|
||||||
|
If cValue = "" Then
|
||||||
|
ws.Cells(rowNum, 2).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, 3).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
Dim i As Long
|
||||||
|
Dim ch As String
|
||||||
|
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, 3).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
Next i
|
||||||
|
|
||||||
|
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, 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, 4).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
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, 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, 5).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
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, 6).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
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, 7).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
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, 9).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
Dim hValue As String
|
||||||
|
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, 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, 8).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
End If
|
||||||
|
|
||||||
|
ws.Cells(rowNum, 2).ClearContents
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Sub Z1_validateButton()
|
Sub Z1_validateButton()
|
||||||
Call Generic_Master_validateButton(Me)
|
Dim lastRow As Long
|
||||||
|
Dim r As Long
|
||||||
|
Dim errorCount As Long
|
||||||
|
|
||||||
|
lastRow = GetLastDataRow(Me, 3)
|
||||||
|
|
||||||
|
If lastRow < 7 Then
|
||||||
|
MsgBox "No data found.", vbExclamation
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
errorCount = 0
|
||||||
|
For r = 7 To lastRow
|
||||||
|
Validate r
|
||||||
|
If Trim(Cells(r, 2).Value) <> "" Then
|
||||||
|
errorCount = errorCount + 1
|
||||||
|
End If
|
||||||
|
Next r
|
||||||
|
|
||||||
|
MsgBox "Validation complete. Errors: " & errorCount, vbInformation
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Sub Z1_SortDataRowsByC()
|
Sub Z1_SortDataRowsByC()
|
||||||
|
|||||||
@@ -8,15 +8,153 @@ Sub Z2_Import()
|
|||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Sub Z2_Export()
|
Sub Z2_Export()
|
||||||
Call Generic_Master_Export(Me)
|
Dim lastDataRow As Long: lastDataRow = GetLastDataRow(Me, 3)
|
||||||
|
|
||||||
|
If lastDataRow < 7 Then
|
||||||
|
MsgBox "No data rows to output.", vbExclamation
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
Dim r As Long
|
||||||
|
Dim 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
|
End Sub
|
||||||
|
|
||||||
Sub Z2_validate(ByVal ws As Worksheet, ByVal rowNum As Long)
|
Sub Validate(ByVal rowNum As Long)
|
||||||
Call Generic_Master_validate(ws, rowNum)
|
Dim cValue As String
|
||||||
|
Set ws = Me
|
||||||
|
|
||||||
|
cValue = Trim(ws.Cells(rowNum, 3).Value)
|
||||||
|
|
||||||
|
' clear C~I columns background color
|
||||||
|
Dim clearRange As Range
|
||||||
|
Set clearRange = ws.Range(ws.Cells(rowNum, 3), ws.Cells(rowNum, 9))
|
||||||
|
clearRange.Interior.Color = vbWhite
|
||||||
|
|
||||||
|
If cValue = "" Then
|
||||||
|
ws.Cells(rowNum, 2).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, 3).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
Dim i As Long
|
||||||
|
Dim ch As String
|
||||||
|
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, 3).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
Next i
|
||||||
|
|
||||||
|
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, 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, 4).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
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, 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, 5).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
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, 6).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
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, 7).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
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, 9).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
Dim hValue As String
|
||||||
|
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, 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, 8).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
End If
|
||||||
|
|
||||||
|
ws.Cells(rowNum, 2).ClearContents
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Sub Z2_validateButton()
|
Sub Z2_validateButton()
|
||||||
Call Generic_Master_validateButton(Me)
|
Dim lastRow As Long
|
||||||
|
Dim r As Long
|
||||||
|
Dim errorCount As Long
|
||||||
|
|
||||||
|
lastRow = GetLastDataRow(Me, 3)
|
||||||
|
|
||||||
|
If lastRow < 7 Then
|
||||||
|
MsgBox "No data found.", vbExclamation
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
errorCount = 0
|
||||||
|
For r = 7 To lastRow
|
||||||
|
Validate r
|
||||||
|
If Trim(Cells(r, 2).Value) <> "" Then
|
||||||
|
errorCount = errorCount + 1
|
||||||
|
End If
|
||||||
|
Next r
|
||||||
|
|
||||||
|
MsgBox "Validation complete. Errors: " & errorCount, vbInformation
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Sub Z2_SortDataRowsByC()
|
Sub Z2_SortDataRowsByC()
|
||||||
|
|||||||
@@ -8,15 +8,153 @@ Sub Z3_Import()
|
|||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Sub Z3_Export()
|
Sub Z3_Export()
|
||||||
Call Generic_Master_Export(Me)
|
Dim lastDataRow As Long: lastDataRow = GetLastDataRow(Me, 3)
|
||||||
|
|
||||||
|
If lastDataRow < 7 Then
|
||||||
|
MsgBox "No data rows to output.", vbExclamation
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
Dim r As Long
|
||||||
|
Dim 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
|
End Sub
|
||||||
|
|
||||||
Sub Z3_validate(ByVal ws As Worksheet, ByVal rowNum As Long)
|
Sub Validate(ByVal rowNum As Long)
|
||||||
Call Generic_Master_validate(ws, rowNum)
|
Dim cValue As String
|
||||||
|
Set ws = Me
|
||||||
|
|
||||||
|
cValue = Trim(ws.Cells(rowNum, 3).Value)
|
||||||
|
|
||||||
|
' clear C~I columns background color
|
||||||
|
Dim clearRange As Range
|
||||||
|
Set clearRange = ws.Range(ws.Cells(rowNum, 3), ws.Cells(rowNum, 9))
|
||||||
|
clearRange.Interior.Color = vbWhite
|
||||||
|
|
||||||
|
If cValue = "" Then
|
||||||
|
ws.Cells(rowNum, 2).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, 3).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
Dim i As Long
|
||||||
|
Dim ch As String
|
||||||
|
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, 3).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
Next i
|
||||||
|
|
||||||
|
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, 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, 4).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
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, 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, 5).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
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, 6).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
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, 7).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
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, 9).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
Dim hValue As String
|
||||||
|
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, 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, 8).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
End If
|
||||||
|
|
||||||
|
ws.Cells(rowNum, 2).ClearContents
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Sub Z3_validateButton()
|
Sub Z3_validateButton()
|
||||||
Call Generic_Master_validateButton(Me)
|
Dim lastRow As Long
|
||||||
|
Dim r As Long
|
||||||
|
Dim errorCount As Long
|
||||||
|
|
||||||
|
lastRow = GetLastDataRow(Me, 3)
|
||||||
|
|
||||||
|
If lastRow < 7 Then
|
||||||
|
MsgBox "No data found.", vbExclamation
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
|
errorCount = 0
|
||||||
|
For r = 7 To lastRow
|
||||||
|
Validate r
|
||||||
|
If Trim(Cells(r, 2).Value) <> "" Then
|
||||||
|
errorCount = errorCount + 1
|
||||||
|
End If
|
||||||
|
Next r
|
||||||
|
|
||||||
|
MsgBox "Validation complete. Errors: " & errorCount, vbInformation
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Sub Z3_SortDataRowsByC()
|
Sub Z3_SortDataRowsByC()
|
||||||
|
|||||||
51
src/thisWorkbook/Master_507.bas
Normal file
51
src/thisWorkbook/Master_507.bas
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
Sub O2_Import()
|
||||||
|
Dim filePath As String
|
||||||
|
Dim lines As Variant
|
||||||
|
Dim i As Long
|
||||||
|
Dim writeRow As Long
|
||||||
|
|
||||||
|
Set ws = Me
|
||||||
|
|
||||||
|
On Error GoTo ErrorHandler
|
||||||
|
|
||||||
|
' Step 1: Select CSV file
|
||||||
|
filePath = SelectCSVFile()
|
||||||
|
If filePath = "" Then Exit Sub
|
||||||
|
|
||||||
|
' Step 2: Read CSV and return 2D array
|
||||||
|
lines = ReadCSVAs2DArrayStrict(filePath, 4, "shift-jis", True)
|
||||||
|
|
||||||
|
' Step 3: Clear data rows
|
||||||
|
Call ClearDataRows(ws, 7, 3)
|
||||||
|
|
||||||
|
' Step 4: Import data
|
||||||
|
writeRow = 7
|
||||||
|
For i = LBound(lines, 1) To UBound(lines, 1)
|
||||||
|
If Not isRowEmpty Then
|
||||||
|
Dim colOffset As Long
|
||||||
|
For colOffset = 1 To 4
|
||||||
|
ws.Cells(writeRow, 2 + colOffset).Value = CleanCSVField(CStr(lines(i, colOffset)))
|
||||||
|
Next colOffset
|
||||||
|
writeRow = writeRow + 1
|
||||||
|
End If
|
||||||
|
Next i
|
||||||
|
|
||||||
|
MsgBox writeRow - 7 & " rows imported.", vbInformation
|
||||||
|
|
||||||
|
Exit Sub
|
||||||
|
|
||||||
|
ErrorHandler:
|
||||||
|
MsgBox "Import fails:" & vbCrLf & Err.Description, vbCritical
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
Sub O2_SortDataRowsByC()
|
||||||
|
Call SortDataRows(3)
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
Sub O2_ToggleAutoFilter()
|
||||||
|
Call ToggleAutoFilter(6)
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
Sub O2_AutoFitColumnWidth()
|
||||||
|
Call AutoFitColumnWidth(3, 5)
|
||||||
|
End Sub
|
||||||
51
src/thisWorkbook/Master_address.bas
Normal file
51
src/thisWorkbook/Master_address.bas
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
Sub O1_Import()
|
||||||
|
Dim filePath As String
|
||||||
|
Dim lines As Variant
|
||||||
|
Dim i As Long
|
||||||
|
Dim writeRow As Long
|
||||||
|
|
||||||
|
Set ws = Me
|
||||||
|
|
||||||
|
On Error GoTo ErrorHandler
|
||||||
|
|
||||||
|
' Step 1: Select CSV file
|
||||||
|
filePath = SelectCSVFile()
|
||||||
|
If filePath = "" Then Exit Sub
|
||||||
|
|
||||||
|
' Step 2: Read CSV and return 2D array
|
||||||
|
lines = ReadCSVAs2DArrayStrict(filePath, 4, "shift-jis", True)
|
||||||
|
|
||||||
|
' Step 3: Clear data rows
|
||||||
|
Call ClearDataRows(ws, 7, 3)
|
||||||
|
|
||||||
|
' Step 4: Import data
|
||||||
|
writeRow = 7
|
||||||
|
For i = LBound(lines, 1) To UBound(lines, 1)
|
||||||
|
If Not isRowEmpty Then
|
||||||
|
Dim colOffset As Long
|
||||||
|
For colOffset = 1 To 4
|
||||||
|
ws.Cells(writeRow, 2 + colOffset).Value = CleanCSVField(CStr(lines(i, colOffset)))
|
||||||
|
Next colOffset
|
||||||
|
writeRow = writeRow + 1
|
||||||
|
End If
|
||||||
|
Next i
|
||||||
|
|
||||||
|
MsgBox writeRow - 7 & " rows imported.", vbInformation
|
||||||
|
|
||||||
|
Exit Sub
|
||||||
|
|
||||||
|
ErrorHandler:
|
||||||
|
MsgBox "Import fails:" & vbCrLf & Err.Description, vbCritical
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
Sub O1_SortDataRowsByC()
|
||||||
|
Call SortDataRows(3)
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
Sub O1_ToggleAutoFilter()
|
||||||
|
Call ToggleAutoFilter(6)
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
Sub O1_AutoFitColumnWidth()
|
||||||
|
Call AutoFitColumnWidth(3, 5)
|
||||||
|
End Sub
|
||||||
Binary file not shown.
Reference in New Issue
Block a user