Skip to content

Instantly share code, notes, and snippets.

@KotorinChunChun
Created December 25, 2021 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KotorinChunChun/69137b4a6148666280bd3fee46929d51 to your computer and use it in GitHub Desktop.
Save KotorinChunChun/69137b4a6148666280bd3fee46929d51 to your computer and use it in GitHub Desktop.
即興で書いたCSV読み込み関数
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