7.26.2012

ExcelにCSVを。SQLで、Recordsetとして

SQLが使えるなら射影も選択も楽ちんじゃないか。

Sub test()
    Dim CON As ADODB.Connection
    Dim RS As ADODB.Recordset

    Dim SQLTEXT = "SELECT 商品名 FROM testdata.csv where 種類='CD' order by 金額"
 
    Set CON = New ADODB.Connection
    CON.Provider = "Microsoft.Jet.OLEDB.4.0"
    CON.Properties("Extended Properties") = "Text;HDR=YES"
    CON.ConnectionString = "G:\test"   ’フォルダを指定
    CON.Open
 
    Set RS = CON.Execute(SQLTEXT)
    RS.Filter = "商品名 = '旅の途中 [Maxi]'"          'フィルタも
    Sheet1.Range("B2").CopyFromRecordset RS   'ワークシートに流し込み

    Set RS = Nothing
    Set CN = Nothing
End Sub

0 件のコメント: