Created
December 25, 2021 09:26
-
-
Save KotorinChunChun/69137b4a6148666280bd3fee46929d51 to your computer and use it in GitHub Desktop.
即興で書いたCSV読み込み関数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rem CSV形式などのテキストファイルを二次元配列へ取り込む | |
Rem | |
Rem @param ssFilePath フルパス | |
Rem @param ssFieldDelimiter 列区切り(既定:カンマ) | |
Rem @param ssRecordDelimiter 行区切り(既定:CRLF) | |
Rem | |
Rem @return As Variant(1 to #, 1 to #) 二次元配列 | |
Rem | |
Function ReadCsvTextFile( _ | |
ssFilePath As String, _ | |
Optional ssFieldDelimiter = ",", _ | |
Optional ssRecordDelimiter = vbCrLf) As Variant | |
Dim txt As String | |
txt = fso.OpenTextFile(ssFilePath, ForReading).ReadAll | |
If Right(txt, Len(ssRecordDelimiter)) = ssRecordDelimiter Then | |
txt = Left(txt, Len(txt) - Len(ssRecordDelimiter)) | |
End If | |
Dim arr | |
arr = Split(txt, ssRecordDelimiter) | |
ReDim Preserve arr(1 To UBound(arr) + 1) | |
Dim data | |
ReDim data(1 To UBound(arr), 1 To 1) | |
Dim rr As Long, cc As Long | |
For rr = 1 To UBound(arr) | |
If arr(rr) <> "" Then | |
Dim rec | |
rec = Split(arr(rr), ssFieldDelimiter) | |
ReDim Preserve rec(1 To UBound(rec) + 1) | |
If UBound(data, 2) < UBound(rec) Then | |
ReDim Preserve data(1 To UBound(data), 1 To UBound(rec)) | |
End If | |
For cc = 1 To UBound(rec) | |
data(rr, cc) = rec(cc) | |
Next | |
End If | |
Next | |
ReadCsvTextFile = data | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment