0
1

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.

【VBA】CSVファイル読み込み

Posted at
Sub Test()
  Dim wsImport As Worksheet
  Set wsImport = Worksheets("Sheet1") 'CSVデータを取り込み用シート
 
  '読み込むファイル
  Dim strFilePath As String
  strFilePath = "C:\Users\take2\Desktop\VBA\test.csv"
 
  Dim queryTb As QueryTable
  Set queryTb = wsImport.QueryTables.Add(Connection:="TEXT;" & strFilePath, Destination:=wsImport.Range("A1")) ' CSV を開く
  With queryTb
      .TextFilePlatform = 932          ' 文字コードを指定
      .TextFileParseType = xlDelimited ' 区切り文字の形式
      .TextFileCommaDelimiter = True   ' カンマ区切り
      .RefreshStyle = xlOverwriteCells ' セルに書き込む方式
      .Refresh                         ' データを表示
      .Delete                          ' CSVファイルとの接続を解除
  End With
  

  MaxCol = wsImport.UsedRange.Columns(wsImport.UsedRange.Columns.Count).Column
  MaxRow = wsImport.UsedRange.Rows(wsImport.UsedRange.Rows.Count).Row
  arr = Range(Cells(1, 1).Address, Cells(MaxRow, MaxCol).Address)
  
End Sub

Sub aaaa()
  Dim wsImport As Worksheet
  Workbooks.Open "C:\Users\take2\Desktop\VBA\test.csv"
  Set wsImport = Worksheets("test") 'CSVデータを取り込み用シート
  
  MaxCol = wsImport.UsedRange.Columns(wsImport.UsedRange.Columns.Count).Column
  MaxRow = wsImport.UsedRange.Rows(wsImport.UsedRange.Rows.Count).Row
  arr = Range(Cells(1, 1).Address, Cells(MaxRow, MaxCol).Address)
End Sub

参考
https://www.sejuku.net/blog/69321

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?