数据连接导入问题

时间:2015-01-29 13:03:24

标签: vba excel-vba excel

我仍然是VBA的新手,我有一点问题。我有一个数据连接设置,导入从表中提取所有数据。我遇到的问题是第三列包含一个16位长的数字。导入发生时,我需要将此列导入为文本字段,否则导入将使用0替换最后一位。这是我导入的代码。任何帮助将不胜感激。

Worksheets("Equip Related").Visible = True
    Dim WK As Worksheet
Application.ScreenUpdating = False

On Error Resume Next
    Application.DisplayAlerts = False
        Sheets("Feedback").Delete
    On Error GoTo 0

    Sheets.Add.Name = "Feedback"

With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;WEB ADDRESS HERE", Destination:= _
        Range("$A$1"))
        .Name = "feedbacklog"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlAllTables
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With

1 个答案:

答案 0 :(得分:0)

您可以将来自querytabel的结果放在resultsobject中并设置每列的数据类型:

Set myResults = ActiveSheet.QueryTables.Add(Connection:= _
    "URL;WEB ADDRESS HERE", Destination:= _
    Range("$A$1"))

With myResults
    .TextFileColumnDataTypes := _ 
        Array(xlGeneralFormat, xlGeneralFormat, xlTextFormat) 
    .Refresh 
End With