update Master_M1_Kukan
This commit is contained in:
@@ -6,21 +6,48 @@ Const END_COL As Long = 14 ' N column
|
|||||||
Const ERROR_COL As Long = 15 ' O column
|
Const ERROR_COL As Long = 15 ' O column
|
||||||
|
|
||||||
Private z1Cache As Object ' Z1 cache
|
Private z1Cache As Object ' Z1 cache
|
||||||
|
Private enumCache As Object ' Z1 cache
|
||||||
|
|
||||||
' ====== Function ======
|
' ====== Function ======
|
||||||
Public Sub RefreshZ1Cache()
|
Public Sub RefreshZ1Cache()
|
||||||
|
On Error GoTo RefreshError
|
||||||
Set z1Cache = LoadLookup("Z1", keyCol:=3, valueCols:=Array(4), startRow:=7)
|
Set z1Cache = LoadLookup("Z1", keyCol:=3, valueCols:=Array(4), startRow:=7)
|
||||||
|
On Error GoTo 0
|
||||||
|
|
||||||
|
If z1Cache Is Nothing Or z1Cache.Count = 0 Then
|
||||||
|
Err.Raise 1001, "RefreshZ1Cache", "Z1 reference data is empty"
|
||||||
|
End If
|
||||||
|
|
||||||
|
Exit Sub
|
||||||
|
|
||||||
|
RefreshError:
|
||||||
|
Err.Raise 1002, "RefreshZ1Cache", "Failed to load Z1 lookup cache: " & Err.Description
|
||||||
|
End Sub
|
||||||
|
|
||||||
|
Public Sub RefreshEnumCache()
|
||||||
|
On Error GoTo RefreshError
|
||||||
|
Set tokubetuKbn = LoadLookup("Enum", keyCol:=3, valueCols:=Array(3), startRow:=3)
|
||||||
|
On Error GoTo 0
|
||||||
|
|
||||||
|
If tokubetuKbn Is Nothing Or tokubetuKbn.Count = 0 Then
|
||||||
|
Err.Raise 1003, "RefreshZ1Cache", "Enum reference data is empty"
|
||||||
|
End If
|
||||||
|
|
||||||
|
Exit Sub
|
||||||
|
|
||||||
|
RefreshError:
|
||||||
|
Err.Raise 1001, "RefreshZ1Cache", "Failed to load Z1 lookup cache: " & Err.Description
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
' Get CSV header from row 5 (columns C to N)
|
' Get CSV header from row 5 (columns C to N)
|
||||||
Function GetM1CSVHeader(ByVal ws As Worksheet) As Variant
|
Function GetM1CSVHeader(ByVal ws As Worksheet) As Variant
|
||||||
Dim headerArr(1 To 1, 1 To 12) As String
|
Dim headerArr(1 To 1, 1 To 12) As String
|
||||||
Dim i As Long
|
Dim i As Long
|
||||||
|
|
||||||
For i = 1 To 12
|
For i = 1 To 12
|
||||||
headerArr(1, i) = Trim(ws.Cells(5, i + 2).Value) ' i+2: 1→3(C), 12→14(N)
|
headerArr(1, i) = Trim(ws.Cells(5, i + 2).Value) ' i+2: 1→3(C), 12→14(N)
|
||||||
Next i
|
Next i
|
||||||
|
|
||||||
GetM1CSVHeader = headerArr
|
GetM1CSVHeader = headerArr
|
||||||
End Function
|
End Function
|
||||||
|
|
||||||
@@ -59,7 +86,7 @@ Sub FillFromZ1(ByVal rowNum As Long)
|
|||||||
|
|
||||||
If z1Cache.Exists(code) Then
|
If z1Cache.Exists(code) Then
|
||||||
Dim vals As Variant: vals = z1Cache(code)
|
Dim vals As Variant: vals = z1Cache(code)
|
||||||
|
|
||||||
ws.Cells(rowNum, 4).Value = vals(0)
|
ws.Cells(rowNum, 4).Value = vals(0)
|
||||||
ws.Cells(rowNum, 5).Value = vals(1)
|
ws.Cells(rowNum, 5).Value = vals(1)
|
||||||
Else
|
Else
|
||||||
@@ -80,48 +107,48 @@ Sub M1_Import()
|
|||||||
Dim csvData As Variant
|
Dim csvData As Variant
|
||||||
Dim i As Long
|
Dim i As Long
|
||||||
Dim writeRow As Long
|
Dim writeRow As Long
|
||||||
|
|
||||||
' Target this worksheet
|
' Target this worksheet
|
||||||
Set wsTarget = Me
|
Set wsTarget = Me
|
||||||
|
|
||||||
' === Step 1: Select CSV file ===
|
' === Step 1: Select CSV file ===
|
||||||
Dim filePath As String
|
Dim filePath As String
|
||||||
filePath = SelectCSVFile()
|
filePath = SelectCSVFile()
|
||||||
If filePath = "" Then Exit Sub
|
If filePath = "" Then Exit Sub
|
||||||
|
|
||||||
' === Step 2: Read CSV with Shift-JIS (using common function) ===
|
' === Step 2: Read CSV with Shift-JIS (using common function) ===
|
||||||
On Error GoTo ImportError
|
On Error GoTo ImportError
|
||||||
csvData = ReadCSVAs2DArrayStrict(filePath, 12, "shift_jis", False)
|
csvData = ReadCSVAs2DArrayStrict(filePath, 12, "shift_jis", False)
|
||||||
On Error GoTo 0
|
On Error GoTo 0
|
||||||
|
|
||||||
' === Clear all data rows before import ===
|
' === Clear all data rows before import ===
|
||||||
Dim lastRow As Long
|
Dim lastRow As Long
|
||||||
lastRow = wsTarget.Cells(wsTarget.Rows.Count, "C").End(xlUp).Row
|
lastRow = wsTarget.Cells(wsTarget.Rows.Count, "C").End(xlUp).Row
|
||||||
If lastRow >= 7 Then
|
If lastRow >= 7 Then
|
||||||
wsTarget.Range("A7:N" & lastRow).ClearContents
|
wsTarget.Range("A7:N" & lastRow).ClearContents
|
||||||
End If
|
End If
|
||||||
|
|
||||||
If UBound(csvData, 1) < 1 Then
|
If UBound(csvData, 1) < 1 Then
|
||||||
MsgBox "No data in CSV.", vbExclamation
|
MsgBox "No data in CSV.", vbExclamation
|
||||||
Exit Sub
|
Exit Sub
|
||||||
End If
|
End If
|
||||||
|
|
||||||
' === Step 3: Write CSV data to worksheet (forward order) ===
|
' === Step 3: Write CSV data to worksheet (forward order) ===
|
||||||
writeRow = 7
|
writeRow = 7
|
||||||
|
|
||||||
For i = LBound(csvData, 1) To UBound(csvData, 1)
|
For i = LBound(csvData, 1) To UBound(csvData, 1)
|
||||||
' CSV col 1-12 -> C-N column
|
' CSV col 1-12 -> C-N column
|
||||||
Dim j As Long
|
Dim j As Long
|
||||||
For j = 1 To 12
|
For j = 1 To 12
|
||||||
wsTarget.Cells(writeRow, j + 2).Value = CleanCSVField(CStr(csvData(i, j)))
|
wsTarget.Cells(writeRow, j + 2).Value = CleanCSVField(CStr(csvData(i, j)))
|
||||||
Next j
|
Next j
|
||||||
|
|
||||||
' Auto-fill D, E columns from Z1
|
' Auto-fill D, E columns from Z1
|
||||||
Call FillFromZ1(writeRow)
|
Call FillFromZ1(writeRow)
|
||||||
|
|
||||||
writeRow = writeRow + 1
|
writeRow = writeRow + 1
|
||||||
Next i
|
Next i
|
||||||
|
|
||||||
MsgBox writeRow - 7 & " rows imported.", vbInformation
|
MsgBox writeRow - 7 & " rows imported.", vbInformation
|
||||||
Exit Sub
|
Exit Sub
|
||||||
|
|
||||||
@@ -170,8 +197,7 @@ Sub validate(ByVal rowNum As Long, ByVal lastDataRow As Long)
|
|||||||
|
|
||||||
' Check D and E column in the cache
|
' Check D and E column in the cache
|
||||||
If z1Cache Is Nothing Then Call RefreshZ1Cache
|
If z1Cache Is Nothing Then Call RefreshZ1Cache
|
||||||
If z1Cache Is Nothing Then Exit Sub
|
|
||||||
|
|
||||||
Dim dValue As String: dValue = Trim(ws.Range("D" & rowNum).Value)
|
Dim dValue As String: dValue = Trim(ws.Range("D" & rowNum).Value)
|
||||||
Dim eValue As String: eValue = Trim(ws.Range("E" & rowNum).Value)
|
Dim eValue As String: eValue = Trim(ws.Range("E" & rowNum).Value)
|
||||||
|
|
||||||
@@ -186,26 +212,35 @@ Sub validate(ByVal rowNum As Long, ByVal lastDataRow As Long)
|
|||||||
ws.Cells(rowNum, ERROR_COL).Value = "Invalid reference data for D column."
|
ws.Cells(rowNum, ERROR_COL).Value = "Invalid reference data for D column."
|
||||||
Exit Sub
|
Exit Sub
|
||||||
End If
|
End If
|
||||||
|
|
||||||
Dim expectedEValue As String
|
Dim expectedEValue As String
|
||||||
expectedEValue = Trim(CStr(valueArray(0)))
|
expectedEValue = Trim(CStr(valueArray(0)))
|
||||||
|
|
||||||
If eValue <> expectedEValue Then
|
If eValue <> expectedEValue Then
|
||||||
ws.Cells(rowNum, ERROR_COL).Value = "E column does not match reference data."
|
ws.Cells(rowNum, ERROR_COL).Value = "E column does not match reference data."
|
||||||
ws.Range("E" & rowNum).Interior.Color = RGB(255, 0, 0)
|
ws.Range("E" & rowNum).Interior.Color = RGB(255, 0, 0)
|
||||||
Exit Sub
|
Exit Sub
|
||||||
End If
|
End If
|
||||||
End If
|
End If
|
||||||
|
|
||||||
|
' Check L column in the cache
|
||||||
|
If enumCache Is Nothing Then Call RefreshEnumCache
|
||||||
|
Dim lValue As String: lValue = Trim(ws.Range("L" & rowNum).Value)
|
||||||
|
If Not enumCache.Exists(lValue) Then
|
||||||
|
ws.Cells(rowNum, ERROR_COL).Value = "L column does not exist."
|
||||||
|
ws.Range("L" & rowNum).Interior.Color = RGB(255, 0, 0)
|
||||||
|
Exit Sub
|
||||||
|
End If
|
||||||
|
|
||||||
' Validation passed - clear error
|
' Validation passed - clear error
|
||||||
ws.Cells(rowNum, ERROR_COL).ClearContents
|
ws.Cells(rowNum, ERROR_COL).ClearContents
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
|
' Validate button
|
||||||
Sub validateButton()
|
Sub M1_validateButton()
|
||||||
Dim lastDataRow As Long, r As Long, errorCount As Long
|
Dim lastDataRow As Long, r As Long, errorCount As Long
|
||||||
lastDataRow = GetLastDataRowInRange(Me, START_COL, END_COL)
|
lastDataRow = GetLastDataRowInRange(Me, START_COL, END_COL)
|
||||||
|
|
||||||
If lastDataRow < 7 Then
|
If lastDataRow < 7 Then
|
||||||
MsgBox "No data found.", vbExclamation
|
MsgBox "No data found.", vbExclamation
|
||||||
Exit Sub
|
Exit Sub
|
||||||
@@ -217,7 +252,7 @@ Sub validateButton()
|
|||||||
errorCount = errorCount + 1
|
errorCount = errorCount + 1
|
||||||
End If
|
End If
|
||||||
Next r
|
Next r
|
||||||
|
|
||||||
MsgBox "Validation complete. Errors: " & errorCount, vbInformation
|
MsgBox "Validation complete. Errors: " & errorCount, vbInformation
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
@@ -230,7 +265,7 @@ Sub M1_Export()
|
|||||||
|
|
||||||
Dim ws As Worksheet
|
Dim ws As Worksheet
|
||||||
Set ws = Me
|
Set ws = Me
|
||||||
|
|
||||||
' === Step 1: Validate all rows before export ===
|
' === Step 1: Validate all rows before export ===
|
||||||
Dim r As Long, errorCount As Long
|
Dim r As Long, errorCount As Long
|
||||||
For r = 7 To lastDataRow
|
For r = 7 To lastDataRow
|
||||||
@@ -241,39 +276,39 @@ Sub M1_Export()
|
|||||||
End If
|
End If
|
||||||
End If
|
End If
|
||||||
Next r
|
Next r
|
||||||
|
|
||||||
If errorCount > 0 Then
|
If errorCount > 0 Then
|
||||||
MsgBox "Validation failed. Found " & errorCount & " error(s). Please correct them before exporting.", vbCritical
|
MsgBox "Validation failed. Found " & errorCount & " error(s). Please correct them before exporting.", vbCritical
|
||||||
Exit Sub
|
Exit Sub
|
||||||
End If
|
End If
|
||||||
|
|
||||||
' === Step 2: Select save path ===
|
' === Step 2: Select save path ===
|
||||||
Dim savePath As String
|
Dim savePath As String
|
||||||
savePath = GetSaveCSVPath()
|
savePath = GetSaveCSVPath()
|
||||||
If savePath = "" Then Exit Sub
|
If savePath = "" Then Exit Sub
|
||||||
|
|
||||||
' === Step 3: Get header from row 5 (C-N columns) ===
|
' === Step 3: Get header from row 5 (C-N columns) ===
|
||||||
Dim headerArr As Variant
|
Dim headerArr As Variant
|
||||||
headerArr = GetM1CSVHeader(ws)
|
headerArr = GetM1CSVHeader(ws)
|
||||||
|
|
||||||
' === Step 4: Build data array (skip D, E, F columns) ===
|
' === Step 4: Build data array (skip D, E, F columns) ===
|
||||||
Dim dataArr As Variant
|
Dim dataArr As Variant
|
||||||
Dim rowCount As Long
|
Dim rowCount As Long
|
||||||
rowCount = 0
|
rowCount = 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
|
||||||
rowCount = rowCount + 1
|
rowCount = rowCount + 1
|
||||||
End If
|
End If
|
||||||
Next r
|
Next r
|
||||||
|
|
||||||
If rowCount = 0 Then
|
If rowCount = 0 Then
|
||||||
MsgBox "No data rows to output.", vbExclamation
|
MsgBox "No data rows to output.", vbExclamation
|
||||||
Exit Sub
|
Exit Sub
|
||||||
End If
|
End If
|
||||||
|
|
||||||
ReDim dataArr(1 To rowCount, 1 To 12)
|
ReDim dataArr(1 To rowCount, 1 To 12)
|
||||||
|
|
||||||
Dim dataRow As Long
|
Dim dataRow As Long
|
||||||
dataRow = 0
|
dataRow = 0
|
||||||
For r = 7 To lastDataRow
|
For r = 7 To lastDataRow
|
||||||
@@ -288,17 +323,17 @@ Sub M1_Export()
|
|||||||
Next j
|
Next j
|
||||||
End If
|
End If
|
||||||
Next r
|
Next r
|
||||||
|
|
||||||
' === Step 5: Write to CSV (using common function) ===
|
' === Step 5: Write to CSV (using common function) ===
|
||||||
Dim outputArr As Variant
|
Dim outputArr As Variant
|
||||||
ReDim outputArr(1 To rowCount + 1, 1 To 12)
|
ReDim outputArr(1 To rowCount + 1, 1 To 12)
|
||||||
|
|
||||||
' Copy header to first row
|
' Copy header to first row
|
||||||
Dim colIdx As Long
|
Dim colIdx As Long
|
||||||
For colIdx = 1 To 12
|
For colIdx = 1 To 12
|
||||||
outputArr(1, colIdx) = headerArr(1, colIdx)
|
outputArr(1, colIdx) = headerArr(1, colIdx)
|
||||||
Next colIdx
|
Next colIdx
|
||||||
|
|
||||||
' Copy data to remaining rows
|
' Copy data to remaining rows
|
||||||
Dim dataR As Long
|
Dim dataR As Long
|
||||||
For dataR = 1 To rowCount
|
For dataR = 1 To rowCount
|
||||||
@@ -306,11 +341,11 @@ Sub M1_Export()
|
|||||||
outputArr(dataR + 1, colIdx) = dataArr(dataR, colIdx)
|
outputArr(dataR + 1, colIdx) = dataArr(dataR, colIdx)
|
||||||
Next colIdx
|
Next colIdx
|
||||||
Next dataR
|
Next dataR
|
||||||
|
|
||||||
On Error GoTo ExportError
|
On Error GoTo ExportError
|
||||||
Call WriteCSVFromArray(savePath, outputArr, "shift_jis", False)
|
Call WriteCSVFromArray(savePath, outputArr, "shift_jis", False)
|
||||||
On Error GoTo 0
|
On Error GoTo 0
|
||||||
|
|
||||||
MsgBox "CSV export completed. Total data rows: " & rowCount, vbInformation
|
MsgBox "CSV export completed. Total data rows: " & rowCount, vbInformation
|
||||||
Exit Sub
|
Exit Sub
|
||||||
|
|
||||||
@@ -323,9 +358,9 @@ Sub M1_SortDataRowsByC()
|
|||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Sub M1_ToggleAutoFilter()
|
Sub M1_ToggleAutoFilter()
|
||||||
Call ToggleAutoFilter(3, 14)
|
Call ToggleAutoFilter(START_COL, END_COL)
|
||||||
End Sub
|
End Sub
|
||||||
|
|
||||||
Sub M1_AutoFitColumnWidth()
|
Sub M1_AutoFitColumnWidth()
|
||||||
Call AutoFitColumnWidth(3, 14)
|
Call AutoFitColumnWidth(START_COL, END_COL)
|
||||||
End Sub
|
End Sub
|
||||||
Binary file not shown.
Reference in New Issue
Block a user