4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CSV読んだよ!

Last updated at Posted at 2015-02-03

・A1セルから書き出し
・エラーチェックなし
・WSHランタイム・ライブラリ参照の必要(Microsoft Scripting Runtim)

結構重い('A`)
もっと事前バインディングしたい(-_-)


Option Explicit

Const Ascii As Integer = 0
Const Unicode As Integer = -1
Const SystemDefault As Integer = -2

Const ReadOnly As Integer = 1
Const CreateNew As Integer = 2
Const Append As Integer = 8

Private Sub f()
    
  Dim line As New Collection
  Dim fo As Object
  Dim fs As Object
  Dim file_name As String
  Dim data
  
  ' ファイル・ダイアログ からファイル名を取得
  
  file_name = Application.GetOpenFilename("text file(*.txt;*.csv), *.txt;*.csv")
  
  Set fo = CreateObject("Scripting.FileSystemObject")
  Set fs = fo.OpenTextFile(file_name, ReadOnly, Unicode)
 

  ' EOFまで コレクションに追加
  ' 改行コードを各行つき手当しておく
  
  Do Until fs.AtEndOfStream
    data = fs.ReadLine()
    line.Add data & vbCrLf
  Loop
  
  ' リソースの開放
  
  fs.Close
  Set fs = Nothing
  
  
  Dim w, elem
  Dim r As Long
  Dim c As Long
  Dim i As Long
  
  r = 0
  c = 0
  
  ' データの取り出し
  
  For Each w In line
    elem = Split(w, ",")
    For i = LBound(elem) To UBound(elem)
      If elem(i) = vbCrLf Then
        c = 0
        r = r + 1
      Else
        [a1].offset(r, c).Value = IIf(elem(i) <> ",", CStr(elem(i)), Empty)
        c = c + 1
      End If
    Next
  Next

      
End Sub

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?