Add common file selection and CSV reading functions
This commit is contained in:
@@ -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
|
ws.Range(ws.Cells(startRow, 1), ws.Cells(lastRow, 20)).ClearContents
|
||||||
End If
|
End If
|
||||||
End Sub
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user