From 6f8f5a084d7047a0071b7f3707988264cf047dda Mon Sep 17 00:00:00 2001 From: updsv7 Date: Mon, 13 Apr 2026 17:32:41 +0900 Subject: [PATCH] Z1 CSV import - validate 7 columns, no header --- vba_code_kotsu_master.txt | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/vba_code_kotsu_master.txt b/vba_code_kotsu_master.txt index 04c9f2b..c25225d 100644 --- a/vba_code_kotsu_master.txt +++ b/vba_code_kotsu_master.txt @@ -1,5 +1,4 @@ ' CSV Header Constants -Const CSV_HEADER As String = "利用区間コード,券種,コード,名称,1箇月運賃/販売額,定期額/券1(額)/利用額,定期支給期間/券1(枚)/特別料金,特別料金/券2(額),券2(枚),端数(額),特別料金" Sub ClearRowData(ByVal ws As Worksheet, ByVal rowNum As Long) @@ -47,20 +46,17 @@ Sub ImportMasterDetailData() lines = Split(textContent, vbLf) - ' === Validate CSV header === - If UBound(lines) >= 0 And Trim(lines(0)) <> "" Then - Dim csvHeader As String - csvHeader = Trim(lines(0)) - ' Validate column count - Dim expectedCount As Long - expectedCount = UBound(Split(CSV_HEADER, ",")) + 1 - Dim headerFields As Variant - headerFields = Split(csvHeader, ",") - If UBound(headerFields) + 1 <> expectedCount Then - MsgBox "CSV column count mismatch. Expected: " & expectedCount & ", Got: " & UBound(headerFields) + 1, vbExclamation - Exit Sub + ' === Validate data rows - must have exactly 7 columns === + Dim lineNum As Long + For lineNum = 0 To UBound(lines) + If Trim(lines(lineNum)) <> "" Then + dataArray = Split(lines(lineNum), ",") + If UBound(dataArray) + 1 <> 7 Then + MsgBox "CSV line " & (lineNum + 1) & " has " & (UBound(dataArray) + 1) & " columns. Expected 7.", vbExclamation + Exit Sub + End If End If - End If + Next lineNum ' === Clear all data rows before import === lastRow = wsTarget.Cells(wsTarget.Rows.Count, "C").End(xlUp).Row @@ -77,7 +73,7 @@ Sub ImportMasterDetailData() Dim csvData As Object Set csvData = CreateObject("Scripting.Dictionary") - For i = 1 To UBound(lines) + For i = 0 To UBound(lines) If Trim(lines(i)) = "" Then GoTo NextCsvLine dataArray = Split(lines(i), ",") If UBound(dataArray) >= 0 Then @@ -98,7 +94,7 @@ NextCsvLine: ' === Step 6: Write CSV data to next available row === writeRow = 7 - For i = 1 To UBound(lines) + For i = 0 To UBound(lines) If Trim(lines(i)) = "" Then GoTo NextLine dataArray = Split(lines(i), ",")