【发布时间】:2016-03-22 18:23:37
【问题描述】:
我无法找到一种方法来选择同一文件夹中多个文件的最新版本并将它们导入 Excel。例如:
文件夹中的文件: 西班牙语.csv 西班牙语(1).csv 西班牙语(2).csv 英文.csv 英文(1).csv 法语.csv (这里还有更多的语言和文件,但为了简单起见,我只包括这些)
从那个文件夹中,我想选择这些文件: 西班牙语(2).csv 英文(1).csv 法语.csv
并将它们导入到一个现有的工作表中。
到目前为止我有:
Sub GetFiles()
Dim MyPath As String
Dim Spanish As String
Dim English As String
Dim French As String
Dim LanguageFiles(2) As String
MyPath = "C:\example\"
'Make sure that the path ends in a backslash
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
Spanish = Dir(MyPath & "Spanish*.csv")
English = Dir(MyPath & "English*.csv")
French = Dir(MyPath & "French*.csv")
I WANT TO SOMEHOW GET THE MOST RECENT VERSION OF EACH AND PASS IT TO THE LANGUAGEFILES ARRAY AND IMPORT IT TO A SINGLE WORKSHEET.
LanguageFiles(0) = Spanish
LanguageFiles(1) = English
LanguageFiles(2) = French
For i = LBound(LanguageFiles) To UBound(LanguageFiles)
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & LanguageFiles(i), Destination:=Range("A" & Rows.Count).End(xlUp).Offset(1, 0))
.Name = "Sample"
.FieldNames = False
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Next i
End Sub
此代码实际上不起作用,因为我将几个部分拼凑在一起,而且我什至不知道我是否走在正确的轨道上。有人可以帮帮我吗?
【问题讨论】:
-
看看使用filesystemobject,还有datelastmodifed,可以在搜索中使用like,所以如果fl.name like "* Spanish *" 那么比较一下日期....
-
这些都是不错的起点......希望对您有所帮助。
-
@Nathan_Sav - 这些都是很好的起点,但请记住,如果有人出于任何原因修改了旧版本的文件,它们可能会失败。