我有多個txt文件(關閉1000),我想導入excel。我用文本導入嚮導,然後選擇固定寬度,我的問題是如何使用每個文件相同的格式,我不需要每次調整。如何自動分欄?
這是例子鏈接:http://www.fhwa.dot.gov/bridge/nbi/1992/AL92.txt
這是記錄格式:http://www.fhwa.dot.gov/bridge/nbi/format.cfm
我有多個txt文件(關閉1000),我想導入excel。我用文本導入嚮導,然後選擇固定寬度,我的問題是如何使用每個文件相同的格式,我不需要每次調整。如何自動分欄?
這是例子鏈接:http://www.fhwa.dot.gov/bridge/nbi/1992/AL92.txt
這是記錄格式:http://www.fhwa.dot.gov/bridge/nbi/format.cfm
爲您節省一點時間我準備的數組(從上面鏈接的格式網頁)的querytables /基於@ RonRosenfeld的建議爲你導入,並很快做了一個文本文件導入(我記錄)。 必須粘貼在這裏,因爲它太大了評論。 當然,您必須進行編輯才能適應。
With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\AL92.txt", Destination:=Range("$A$1"))
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileFixedColumnWidths = Array(3, 15, 1, 1, 1, 5, 1, 2, 3, 5, 24, 1, 18, 25, 4, 7, 1, 10, 2, 8, 9, 3, 1, 2, 2, 2, 4, 2, 2, 6, 4, 1, 4, 1, 2, 1, 1, 1, 1, 1, 1, 1, 4, 5, 1, 1, 1, 1, 2, 1, 2, 3, 4, 3, 5, 6, 3, 3, 4, 4, 4, 1, 4, 1, 3, 3, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 2, 1, 6, 4, 2, 3, 3, 3, 4, 4, 4, 6, 6, 6, 4, 3, 2, 15, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 2, 1, 1, 1, 1, 6, 4, 4)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
謝謝,但我想知道它是什麼問題(.Refresh BackgroundQuery:= False) – Jeffrey
如果您在該行發生錯誤,請嘗試刪除BackgroundQuery:= False並查看結果如何。 我剛剛再次測試兩個,它的工作原理是昨天。 –
您可以使用VBA'Range.Parse'方法或'Workbooks.OpenText'方法並適當定義* FieldInfo *。如果您的代碼存在問題,請回復代碼並解釋問題。 –
你也可以使用'QueryTables.Add'方法。無論如何,在你做你想做的事情時錄製一個宏,然後清理它。 –