【发布时间】:2017-04-21 14:52:59
【问题描述】:
我有大约 900 个 CSV 文件,所有这些文件都是从跟踪软件中导出的。不幸的是,该软件在具有许多标题的逐帧数据顶部逐行导入大约 52 行汇总数据。
我正在寻找一种方法:
1)打开csv文件
2)将汇总数据保存为单独的电子表格,文件名为“Original_Summary”
3) 将逐帧数据(包括标题)保存到单独的 Excel 文件中,并将原始文件名作为工作表的新名称。
以前,我用大约 124 个文件为每个文件手动完成剪切/粘贴,但由于文件数量已经失控,我不确定手动执行此操作是否是最佳选择。
我已经编写了另一个脚本,将这些 excel 文件作为单独的表导入 Access,但现在我需要一种方法将它们从 CSV 传输,并将顶部的所有额外摘要数据移动到单独的文件。
有没有办法可以做到这一点?
谢谢!
Sub ImportManyTXTs_test()
Dim strFile As String
Dim foldername As String
Dim ws As Worksheet
strFile = Dir("C:\Users\Jared\Desktop\Processed\Text\*.txt")
Do While strFile <> vbNullString
Set ws = Sheets.Add
With ws.QueryTables.Add(Connection:= _
"TEXT;" & "C:\Users\Jared\Desktop\Processed\Text\" & strFile, Destination:=Range("$A$1"))
.Name = strFile
'.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 = 52
'.TextFileParseType = xlFixedWidth
'.TextFileTextQualifier = xlTextQualifierDoubleQuote
'.TextFileConsecutiveDelimiter = False
'.TextFileTabDelimiter = False
'.TextFileSemicolonDelimiter = False
'.TextFileCommaDelimiter = False
'.TextFileSpaceDelimiter = False
'.TextFileColumnDataTypes = Array(xlYMDFormat, 1, 1)
'.TextFileFixedColumnWidths = Array(22, 13, 13)
'.TextFileTrailingMinusNumbers = True
'.Refresh BackgroundQuery:=False
'.CommandType = 0
'.Name = "T15_070916_B"
.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 = 52
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
ActiveSheet.Name = strFile
strFile = Dir
Loop
End Sub
我试过这个,它似乎没有上传我所有的文件,只有前 99 个左右,它也不会将它们导入到新的工作簿中,而只是一个带有原始扩展名的新工作表。出于某种原因,在我必须删除文件并重新开始之前,它也只能工作 1 次。这很奇怪。
我对编码还是有点陌生,所以任何帮助都将不胜感激!
【问题讨论】:
-
从手动录制宏开始?然后利用循环打开所有文件。
-
所以当我尝试这个时,我遇到了程序将每个文件作为新工作表直接添加到工作簿的问题,并且文件名和扩展名是我不想要的。我想让他们每个人都有自己的工作簿,工作表保留原始文件名,我不知道该怎么做。
标签: excel vba csv ms-access import