我正在研究一个宏,对三个工作簿进行重新格式化和编辑。这三本工作手册的名称始终相同,并且来自同一个来源。他们以.csv格式到达。我想让VBA将所有这三个工作簿作为单独的工作表导入到一本书中,并根据每个工作簿的标题中找到的字符串重命名这些工作表。有没有简单的方法将它附加到录制的宏?此外,是否有更好的方式导入和分隔/格式化文件,而不是通过记录宏生成的方式?我已经把代码从下面这个方法:使用VBA导入和重命名.CSV文件
With ActiveSheet.QueryTables.Add(Connection:= _
"FAKENAME.csv" _
, Destination:=Range("$A$1"))
.CommandType = 0
.Name = "FAKENAME"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, _
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 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, 3, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
[你可以从这里开始了另一种方法](http://stackoverflow.com/questions/21010080/copying-data-from-a-text-file-to-an-excel-workbook/21010921#21010921)。该链接提供了加载多个文本文件(我认为也适用于CSV)的解决方案,然后将其复制到工作表中。 – L42
@ l42谢谢,我来看看。 – 114