Automate Text Import in Excel 2007

后端 未结 3 1525
鱼传尺愫
鱼传尺愫 2021-01-03 14:39

I\'m trying to write an Excel macro using VBA to automate importing CSV text into a spreadsheet but I\'ve never done it before. I need to make sure that the Text Import Wiz

3条回答
  •  庸人自扰
    2021-01-03 15:36

    Public Sub Example()
        Const csPath As String = "C:\Test\Example.csv"
        Dim ws As Excel.Worksheet
        Set ws = Excel.ActiveSheet
        With ws.QueryTables.Add("TEXT;" & csPath, ws.Cells(1, 1))
            .FieldNames = True
            .AdjustColumnWidth = True
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileCommaDelimiter = True
            ''// This array will need as many entries as there will be columns:
            .TextFileColumnDataTypes = Array(xlTextFormat, xlTextFormat)
            .Refresh
        End With
    End Sub
    

提交回复
热议问题