从“文本”文件导入数据时出错

时间:2013-04-23 21:45:56

标签: excel vba excel-vba

我有超过100个文件是分号分隔文件(* .asc,但打开与文本文件相同)以导入工作表。我没关系,每次都选择文件并根据需要更新脚本中的文件夹。我只是想节省点击菜单的时间。

我可以成功选择该文件,但它在.Refresh BackgroundQuery行之后给出了“错误104 Excel无法找到文本文件以刷新此外部数据范围”。

我没有正确引用某些内容吗?

Sub Macro2input()
'
' Macro2input Macro
'
'
    Dim Start1 As Range
    'Dim SourceCells As Range
    'Dim DestinationCells As Range
    'Dim NextStart As Range
    Set Start1 = ActiveCell
    Dim Finfo As String
    Dim vFilename As Variant
    Dim fileToOpen As Variant

    rootDir = "X:\Lab Tests\13-7242\Re-run Calon\1-B"
    Finfo = "All Files (*.*), *.*"

    'Sets the default folder for selecting a file
    ChDrive "X:"
    ChDir rootDir

    Filename = Application.GetOpenFilename(Finfo, 1, "Select A File To Import")
    vFilename = rootDir & Filename

    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;" + vFilename, Destination:=Start1)
        .Name = Filename
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 932
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
ActiveCell.Offset(0, 8).Select
End Sub

2 个答案:

答案 0 :(得分:0)

现在没有一个非常好的VB wizzard: 您是否真的在“vFilename = rootDir& Filename”行中检查了vFilename中是否有正确的文件名,并且您确定可以找到并允许访问(exccel-process)vFilename中的文件?据我所知,rootDir和Filename之间没有反斜杠,因此不是有效的文件路径。

答案 1 :(得分:0)

我认为Destination:=Start1.

存在问题

请改用Destination:=Range("A" & Lastrow)