如何自动分割栏?

时间:2016-09-18 20:43:35

标签: database excel text organization

我有多个txt文件(关闭1000),我想导入到Excel。我使用文本导入向导然后选择固定宽度,我的问题是如何为每个我不需要每次调整的文件使用相同的格式。

这是示例链接:http://www.fhwa.dot.gov/bridge/nbi/1992/AL92.txt

这是记录格式:http://www.fhwa.dot.gov/bridge/nbi/format.cfm

1 个答案:

答案 0 :(得分:0)

为了节省您一些时间,我根据@ 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