简体   繁体   English

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

[英]Error in loading text file into excel

I used Macro recorder record codes to load text file into excel, however, when I was trying to run it. 但是,当我尝试运行它时,我使用了宏记录器记录代码将文本文件加载到excel中。 I keep getting a error message said "Invalid procedure cal or argument". 我不断收到一条错误消息,内容为“无效的过程cal或参数”。 could someone tell me why? 有人可以告诉我为什么吗?

Thanks in advance. 提前致谢。

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

I tested 2 ways of importing a text file and both work fine, consistently: 我测试了两种导入文本文件的方法,并且两种方法都能正常工作:

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

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

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