From e282184fa5afcdf3d2aea5817d7601a47b391bb7 Mon Sep 17 00:00:00 2001 From: updsv7 Date: Mon, 13 Apr 2026 18:15:54 +0900 Subject: [PATCH] Add common file selection and CSV reading functions --- vba_code_common.txt | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/vba_code_common.txt b/vba_code_common.txt index 0a52e05..fc92493 100644 --- a/vba_code_common.txt +++ b/vba_code_common.txt @@ -64,3 +64,49 @@ Sub ClearDataRows(ByVal ws As Worksheet, ByVal startRow As Long, ByVal columnNum ws.Range(ws.Cells(startRow, 1), ws.Cells(lastRow, 20)).ClearContents End If End Sub + +' ============================================================ +' File Dialog - Select CSV file +' Returns: file path or "" if cancelled +' ============================================================ +Function SelectCSVFile() As String + Dim fileDialog As FileDialog + Set fileDialog = Application.FileDialog(msoFileDialogFilePicker) + + With fileDialog + .Filters.Clear + .Filters.Add "CSV Files", "*.csv" + .AllowMultiSelect = False + If .Show <> -1 Then + SelectCSVFile = "" + Exit Function + End If + SelectCSVFile = .SelectedItems(1) + End With +End Function + +' ============================================================ +' Read CSV file with Shift-JIS encoding +' Returns: array of lines +' ============================================================ +Function ReadCSVFile(ByVal filePath As String) As Variant + If filePath = "" Then + ReadCSVFile = Array() + Exit Function + End If + + Dim stream As Object + Dim textContent As String + + Set stream = CreateObject("ADODB.Stream") + With stream + .Type = 2 + .Charset = "shift_jis" + .Open + .LoadFromFile filePath + textContent = .ReadText + .Close + End With + + ReadCSVFile = Split(textContent, vbLf) +End Function