fork download
  1. Option Explicit
  2.  
  3. Public Sub CSV読み込み()
  4. Dim csvFile As String
  5. Dim wb As Workbook
  6. Dim ws As Worksheet
  7. Dim qTb As QueryTable
  8. Dim qCon As String
  9. Dim qDst As Range
  10. Dim attr(27) As Long
  11. Dim i As Long
  12. ' CSV ファイル名を選択
  13. csvFile = Application.GetOpenFilename(FileFilter:="CSV Files (*.csv), *.csv", Title:="CSVファイルを選択")
  14. If csvFile = "False" Then Exit Sub
  15. '取り込み用作業シートをクリア
  16. Set ws = Worksheets("Sheet1")
  17. ws.Cells.ClearContents
  18. '取り込み列の属性を設定
  19. For i = 0 To UBound(attr)
  20. attr(i) = xlGeneralFormat '一般形式
  21. If i = 5 Then attr(i) = xlTextFormat 'F列は文字形式
  22. If i = 24 Then attr(i) = xlTextFormat 'Y列は文字形式
  23. Next
  24. 'QueryTable実行
  25. qCon = "Text;" & csvFile
  26. Set qDst = ws.Range("A1")
  27. Set qTb = ws.QueryTables.Add(Connection:=qCon, Destination:=qDst)
  28. With qTb
  29. .TextFilePlatform = 932 ' 文字コードを指定
  30. .TextFileParseType = xlDelimited ' 区切り文字の形式
  31. .TextFileCommaDelimiter = True ' カンマ区切り
  32. .RefreshStyle = xlOverwriteCells ' セルに書き込む方式
  33. .TextFileStartRow = 1 '開始行
  34. .TextFileTextQualifier = xlTextQualifierDoubleQuote
  35. .TextFileColumnDataTypes = attr '各列の属性
  36. .Refresh ' データを表示
  37. .Delete ' CSVファイルとの接続を解除
  38. End With
  39. '新規ブックへコピー
  40. ws.Copy
  41. 'このマクロのブックを保存して閉じる
  42. ThisWorkbook.Save
  43. ThisWorkbook.Close
  44.  
  45. End Sub
  46.  
  47.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty