LoginSignup
2
6

CSVを取得する

Last updated at Posted at 2018-03-05

ボタンクリックでCSVを取り込むスクリプト

getCSVSample.vba
Private Sub CommandButton1_Click()

Dim varFileName As Variant
Dim ro As Long
Dim date_time As Variant
Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets(2) 'CSVを書き込むシート番号
'--------< 画面を更新と計算を止める >--------------------
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
'------------------------------------------------------
ro = 0
ro = ws.Cells(Rows.Count, 1).End(xlUp).Row

'-----------------getFilename--------------------------
varFileName = Application.GetOpenFilename(FileFilter:="CSV(*.csv),*.csv", Title:="SelectCSV")
    If varFileName = False Then
        Exit Sub
    End If

i = 1
    Open varFileName For Input As #1
    Do Until EOF(1)
        Line Input #1, buf
        tmp = Split(buf, ",")
        ws.Cells(i, 1).Resize(1, UBound(tmp) + 1).Value = tmp
        i = i + 1
    Loop
    Close #1
    MsgBox (i & "件のCSVデータを取り込みました")
    
'--------< 画面を更新と計算を再開  >--------------------
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
'------------------------------------------------------
End Sub
2
6
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
2
6