简体   繁体   English

使用VBA以准确的数据格式将CSV文件从Web导入到Excel工作表

[英]CSV file import to excel sheet from Web using VBA with exact data format

I have tried the below code to import csv file to an excel sheet (Courtesty: http://investexcel.net/download-finviz-data/ ) and it is working fine. 我已经尝试了下面的代码将csv文件导入到Excel工作表(Courtesty: http ://investexcel.net/download-finviz-data/),并且工作正常。 After importing the data, data type was not proper. 导入数据后,数据类型不正确。 Please see the screenshot. 请看截图。

在此处输入图片说明

The zero prefix was removed 2nd column after importing into excel. 导入excel后,第二列中删除了零前缀。 Is there any property like '.TextFileColumnDataTypes' for QueryTables.Add(Connection:="URL;"... ? 是否有QueryTables.Add(Connection:=“ URL;” ...的任何属性,例如'.TextFileColumnDataTypes'?

Sub GetWebCsvData()

Dim str As String
 Dim myarray() As Variant
'Delete existing data
Sheets("Data").Activate 'Name of sheet the data will be downloaded into. Change as required.
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents


str = "http://somedomain.com/filename.csv"
QueryQuote:
            With Sheets("Data").QueryTables.Add(Connection:="URL;" & str, Destination:=Sheets("Data").Range("a1"))
                .BackgroundQuery = True
                .TablesOnlyFromHTML = false
                .Refresh BackgroundQuery:=False
                .SaveData = True

            End With

Sheets("Data").Range("a1").CurrentRegion.TextToColumns Destination:=Sheets("Data").Range("a1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, other:=True, OtherChar:=",", FieldInfo:=Array(1, 2)

Sheets("Data").Columns("A:B").ColumnWidth = 12
Range("A1").Select

End Sub

This worked quite nicely: 这工作得很好:

Option Explicit

Sub TestMe()

    Dim filePath As String: filePath = "C:\\file.csv"        
    Cells.Delete

    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & filePath, _
                                                      Destination:=Range("A1"))
        .Name = "test"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 1252
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 2, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False            
    End With        
End Sub

The correct property is .TextFileColumnDataTypes = Array(1, 2, 1, 1, 1, 1, 1, 1, 1, 1) . 正确的属性是.TextFileColumnDataTypes = Array(1, 2, 1, 1, 1, 1, 1, 1, 1, 1) The 2 in the array stands for Text: 数组中的2代表Text:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM