我有很多CSV文件,我想使用宏将它们导入(并操纵它们)到现有的excel文件中。
问题是我只知道文件的一部分。他们就像:
所以我用这段代码导入了我的文件:
Sub LoadFromFile()
Dim fileName As String, folder As String
folder = "C:\FolderPath\"
fileName = "ThisIsTheFirstFile-*"
Dim file As String
file = Dir$(folder & fileName & ".*")
ActiveCell.Offset(0, 0).Range("A1").Select
With ActiveSheet.QueryTables _
.Add(Connection:="TEXT;" & file, Destination:=ActiveCell)
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 850
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
但它似乎与文件名不匹配。现在我只想加载一个文件,然后我会操纵它,提取数据,压缩所有加载的数据以加载其他文件。