Update Z1 export to use common functions

This commit is contained in:
updsv7
2026-04-13 18:19:37 +09:00
parent aa1af0c409
commit 9338e4adbf
2 changed files with 112 additions and 38 deletions

View File

@@ -224,66 +224,50 @@ Sub Z1_ExportMasterDetailData()
Dim ws As Worksheet
Set ws = ActiveSheet
' Get last data row
Dim lastDataRow As Long
lastDataRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
lastDataRow = GetLastDataRow(ws, 3)
If lastDataRow < 7 Then
MsgBox "No data rows to output.", vbExclamation
Exit Sub
End If
' Get save path
Dim savePath As String
savePath = Application.GetSaveAsFilename( _
FileFilter:="CSV Files (*.csv), *.csv", _
Title:="Save CSV")
savePath = GetSaveCSVPath()
If savePath = "" Then Exit Sub
If savePath = "False" Then Exit Sub
If InStr(1, savePath, ".csv", vbTextCompare) = 0 Then
savePath = savePath & ".csv"
End If
' Define columns to export (C, I-R = 3, 9-18)
Dim dataColumns(0 To 9) As Long
dataColumns(0) = 3
dataColumns(1) = 9
dataColumns(2) = 10
dataColumns(3) = 11
dataColumns(4) = 12
dataColumns(5) = 13
dataColumns(6) = 14
dataColumns(7) = 15
dataColumns(8) = 16
dataColumns(9) = 17
dataColumns(10) = 18
' Build header from row 5 (columns C, G-P)
' Build CSV content (with header from row 5)
Dim csvContent As String
csvContent = Trim(ws.Cells(5, 3).Value)
Dim j As Long
For j = 7 To 16
csvContent = csvContent & "," & Trim(ws.Cells(5, j).Value)
Next j
csvContent = csvContent & vbLf
csvContent = BuildCSVContent(ws, 7, lastDataRow, dataColumns, 5)
' Row counter
' Count rows
Dim rowCount As Long
rowCount = 0
' Data: C,G,H,I,J,K,L,M,N,O,P (skip D,E,F)
Dim r As Long
For r = 7 To lastDataRow
If Len(Trim(ws.Cells(r, 3).Value & "")) > 0 Then
rowCount = rowCount + 1
' CSV col1 -> C column
csvContent = csvContent & CleanCSVField(ws.Cells(r, 3).Value)
' CSV col2-11 -> I-R column
For j = 9 To 18
csvContent = csvContent & "," & CleanCSVField(ws.Cells(r, j).Value)
Next j
csvContent = csvContent & vbLf
End If
Next r
' Trim trailing empty lines
Do While Right(csvContent, 1) = vbLf
csvContent = Left(csvContent, Len(csvContent) - 1)
Loop
' Write file
Dim stream As Object
Set stream = CreateObject("ADODB.Stream")
stream.Type = 2
stream.Charset = "shift_jis"
stream.Open
stream.WriteText csvContent, 1
stream.SaveToFile savePath, 2
stream.Close
Call WriteCSVFile(savePath, csvContent)
MsgBox "CSV export completed. Total data rows: " & rowCount, vbInformation
End Sub