add new write csv common module

This commit is contained in:
updsv7
2026-04-14 15:46:59 +09:00
parent 6ef093aca5
commit b87618dcf3
3 changed files with 144 additions and 16 deletions

View File

@@ -53,10 +53,11 @@ Sub Z1_ExportMasterDetailData()
Dim ws As Worksheet
Dim lastDataRow As Long
Dim savePath As String
Dim csvContent As String
Dim r As Long
Dim j As Long
Dim rowCount As Long
Dim dataArray() As Variant
Dim dataIdx As Long
Dim j As Long
Set ws = ActiveSheet
@@ -70,28 +71,36 @@ Sub Z1_ExportMasterDetailData()
savePath = GetSaveCSVPath()
If savePath = "" Then Exit Sub
' Build data rows (no header) - CSV 1-7 = C-I
' Count valid rows first (C column non-empty from row 7 onward)
rowCount = 0
For r = 7 To lastDataRow
If Len(Trim(ws.Cells(r, 3).Value & "")) > 0 Then
rowCount = rowCount + 1
For j = 3 To 9
If j = 3 Then
csvContent = csvContent & CleanCSVField(ws.Cells(r, j).Value)
Else
csvContent = csvContent & "," & CleanCSVField(ws.Cells(r, j).Value)
End If
Next j
csvContent = csvContent & vbLf
End If
Next r
' Trim trailing
Do While Right(csvContent, 1) = vbLf
csvContent = Left(csvContent, Len(csvContent) - 1)
Loop
' If no data, exit
If rowCount = 0 Then
MsgBox "No data rows to output.", vbExclamation
Exit Sub
End If
Call WriteCSVFile(savePath, csvContent)
' Initialize 2D array: (1 To rowCount, 1 To 7) for columns C-I (3 to 9)
ReDim dataArray(1 To rowCount, 1 To 7)
' Fill the array
dataIdx = 0
For r = 7 To lastDataRow
If Len(Trim(ws.Cells(r, 3).Value & "")) > 0 Then
dataIdx = dataIdx + 1
For j = 3 To 9
dataArray(dataIdx, j - 2) = ws.Cells(r, j).Value ' C->1, D->2, ..., I->7
Next j
End If
Next r
' Write using the new array-based CSV writer
Call WriteCSVFromArray(savePath, dataArray, "utf-8", True)
MsgBox "CSV export completed. Total data rows: " & rowCount, vbInformation
End Sub