Importing Delimited File to Excel with Macro (.CommandType = 0)

╄→尐↘猪︶ㄣ 提交于 2020-01-23 01:37:27

问题


Lots of similar questions going on but google and stack overflow are not touching the part I think I need. I am trying to import a pipe delimited text file with a macro. When I record macro, this is what I get:

With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;C:\Users\johnsmith\Desktop\Macro Tinkering\ABC_Financials_ALL(Delimited).txt" _
    , Destination:=Range("$A$1"))
    .CommandType = 0
    .Name = "ABC_Financials_ALL(Delimited)_1"
    .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 = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = False
    .TextFileSpaceDelimiter = False
    .TextFileOtherDelimiter = "|"
    .TextFileColumnDataTypes = Array(2, 2, 2, 2, 2)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With

When I try to run the macro, it fails, and when I debug, it points me in the direction of ".CommandType = 0"

Anyone know if there is a value that will get this macro going? Or are my problems worse than that?


回答1:


Alright.

More confident in just deleting possible trash off Mukul215's suggestion, I kept deleting stuff and retrying.

With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;C:\Users\johnsmith\Desktop\Macro Tinkering\ABC_Financials_ALL(Delimited).txt" _
    , Destination:=Range("$A$1"))
    .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 = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = False
    .TextFileSpaceDelimiter = False
    .TextFileOtherDelimiter = "|"
    .TextFileColumnDataTypes = Array(2, 2, 2, 2, 2)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With

And this one does it.



来源:https://stackoverflow.com/questions/32082226/importing-delimited-file-to-excel-with-macro-commandtype-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!