问题
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