将文本文件加载到Excel中时出错

时间:2015-08-10 19:32:43

标签: excel vba excel-vba

我使用宏录制器记录代码将文本文件加载到excel中,但是,当我尝试运行它时。我一直收到一条错误消息“无效的程序校准或参数”。有人可以告诉我为什么吗?

提前致谢。

Sub Pacer()

Set NewBook = Workbooks.Add

Set sh1 = NewBook.Sheets("Sheet1")

sh1.Activate

With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;C:\Users\oyang\Desktop\Daily Recon Raw Data\Report_Bundle_20150810_0834\ALLPORT.LOG" _
    , Destination:=sh1.Cells(1, 1))
    .Name = "ALLPORT"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 437
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = True
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With



End Sub

1 个答案:

答案 0 :(得分:1)

我测试了2种导入文本文件的方法,并且两者都很好地工作:

Option Explicit

Private Const F_NAME1 As String = "C:\Users\oyang\Desktop\Daily Recon Raw Data\"
Private Const F_NAME2 As String = F_NAME1 & "Report_Bundle_20150810_0834\ALLPORT.LOG"
Private Const F_NAME3 As String = "C:\New folder\test.log"

Public Sub importTextFile1()
    Dim wb As Workbook, ws As Worksheet
    Set wb = Workbooks.Add
    Set ws = wb.Sheets("Sheet1")
    With ws.QueryTables.Add(Connection:="TEXT;" & F_NAME3, Destination:=ws.Cells(1, 1))
        .Name = "ALLPORT"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub
Public Sub importTextFile2()
    Workbooks.OpenText Filename:=F_NAME3, _
        Origin:=437, _
        StartRow:=1, _
        DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, _
        ConsecutiveDelimiter:=True, _
        Tab:=False, _
        Semicolon:=False, _
        Comma:=False, _
        Space:=True, _
        Other:=False, _
        FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1)), _
        TrailingMinusNumbers:=True
End Sub