Want VBA in excel to read very large CSV and create output file of a small subset of the CSV

前端 未结 7 1959
故里飘歌
故里飘歌 2020-11-30 08:18

I have a csv file of 1.2 million records of text. The alphanumeric fields are wrapped in quotation marks, the date/time or numeric fields are not.

For example \"Fre

7条回答
  •  时光取名叫无心
    2020-11-30 08:38

    I used the following derivative of the code given above to successfully open an arbitrary csv file from VBA in Excel.

    Option Explicit
    Public cn As Connection
    Public Sub DoIt()
    Dim strcon As String
    Dim strsql As String
    Dim rs As Recordset

    Set cn = CreateObject("ADODB.Connection")

    strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\bin\HomePlanet\;" _
    & "Extended Properties=""text;HDR=Yes;FMT=Delimited"";"

    cn.Open strcon

    strsql = "SELECT * FROM astuname.csv "
    Set rs = New ADODB.Recordset
    rs.Open strsql, cn
    DoEvents ' pause here to inspect objects and properties rs.Close
    End Sub

    The rs (recordset) has a collection of fields, with a Count property. Each field as a Type property.

    You can reference the fields by sequence number ...

    Debug.Print rs.Fields(rs.Fields.Count - 1).Type

    Is this sufficient?

    If not, post the first several rows of the input file and I'll take it the rest of the way.

提交回复
热议问题