Z1 CSV import - validate 7 columns, no header
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
' CSV Header Constants
|
' CSV Header Constants
|
||||||
Const CSV_HEADER As String = "利用区間コード,券種,コード,名称,1箇月運賃/販売額,定期額/券1(額)/利用額,定期支給期間/券1(枚)/特別料金,特別料金/券2(額),券2(枚),端数(額),特別料金"
|
|
||||||
|
|
||||||
|
|
||||||
Sub ClearRowData(ByVal ws As Worksheet, ByVal rowNum As Long)
|
Sub ClearRowData(ByVal ws As Worksheet, ByVal rowNum As Long)
|
||||||
@@ -47,20 +46,17 @@ Sub ImportMasterDetailData()
|
|||||||
|
|
||||||
lines = Split(textContent, vbLf)
|
lines = Split(textContent, vbLf)
|
||||||
|
|
||||||
' === Validate CSV header ===
|
' === Validate data rows - must have exactly 7 columns ===
|
||||||
If UBound(lines) >= 0 And Trim(lines(0)) <> "" Then
|
Dim lineNum As Long
|
||||||
Dim csvHeader As String
|
For lineNum = 0 To UBound(lines)
|
||||||
csvHeader = Trim(lines(0))
|
If Trim(lines(lineNum)) <> "" Then
|
||||||
' Validate column count
|
dataArray = Split(lines(lineNum), ",")
|
||||||
Dim expectedCount As Long
|
If UBound(dataArray) + 1 <> 7 Then
|
||||||
expectedCount = UBound(Split(CSV_HEADER, ",")) + 1
|
MsgBox "CSV line " & (lineNum + 1) & " has " & (UBound(dataArray) + 1) & " columns. Expected 7.", vbExclamation
|
||||||
Dim headerFields As Variant
|
Exit Sub
|
||||||
headerFields = Split(csvHeader, ",")
|
End If
|
||||||
If UBound(headerFields) + 1 <> expectedCount Then
|
|
||||||
MsgBox "CSV column count mismatch. Expected: " & expectedCount & ", Got: " & UBound(headerFields) + 1, vbExclamation
|
|
||||||
Exit Sub
|
|
||||||
End If
|
End If
|
||||||
End If
|
Next lineNum
|
||||||
|
|
||||||
' === Clear all data rows before import ===
|
' === Clear all data rows before import ===
|
||||||
lastRow = wsTarget.Cells(wsTarget.Rows.Count, "C").End(xlUp).Row
|
lastRow = wsTarget.Cells(wsTarget.Rows.Count, "C").End(xlUp).Row
|
||||||
@@ -77,7 +73,7 @@ Sub ImportMasterDetailData()
|
|||||||
Dim csvData As Object
|
Dim csvData As Object
|
||||||
Set csvData = CreateObject("Scripting.Dictionary")
|
Set csvData = CreateObject("Scripting.Dictionary")
|
||||||
|
|
||||||
For i = 1 To UBound(lines)
|
For i = 0 To UBound(lines)
|
||||||
If Trim(lines(i)) = "" Then GoTo NextCsvLine
|
If Trim(lines(i)) = "" Then GoTo NextCsvLine
|
||||||
dataArray = Split(lines(i), ",")
|
dataArray = Split(lines(i), ",")
|
||||||
If UBound(dataArray) >= 0 Then
|
If UBound(dataArray) >= 0 Then
|
||||||
@@ -98,7 +94,7 @@ NextCsvLine:
|
|||||||
' === Step 6: Write CSV data to next available row ===
|
' === Step 6: Write CSV data to next available row ===
|
||||||
writeRow = 7
|
writeRow = 7
|
||||||
|
|
||||||
For i = 1 To UBound(lines)
|
For i = 0 To UBound(lines)
|
||||||
If Trim(lines(i)) = "" Then GoTo NextLine
|
If Trim(lines(i)) = "" Then GoTo NextLine
|
||||||
|
|
||||||
dataArray = Split(lines(i), ",")
|
dataArray = Split(lines(i), ",")
|
||||||
|
|||||||
Reference in New Issue
Block a user