选择最近的多个文件,然后导入到一个Excel工作表

时间:2016-03-22 18:23:37

标签: excel vba excel-vba

我无法找到在同一文件夹中选择多个文件的最新版本并将其导入Excel的方法。例如:

文件夹中的文件: Spanish.csv 西班牙(1)的.csv 西班牙语(2)的.csv English.csv 英语(1)的.csv French.csv (这里有更多的语言和文件,但为了简单起见,我只包括这些语言和文件)

从该文件夹中,我想选择这些文件: 西班牙语(2)的.csv 英语(1)的.csv French.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

这段代码实际上并不起作用,因为我将几件拼凑在一起,而且我不知道我是否在正确的轨道上。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

将以下循环放在您编写的区域内:

I WANT TO SOMEHOW GET THE MOST RECENT VERSION OF EACH AND PASS IT TO THE LANGUAGEFILES 

我刚刚为西班牙语文件编写了循环,但您可以轻松添加另一个循环来捕获法语,英语等文件,并调整变量名称以加载到LanguageFiles数组中。

Do While Spanish <> ""

        If InStr(1, Spanish, "(") Then 'test to see if there is a number

            Dim bVersion As Boolean
            bVersion = True 'set this variable to true for later

            'extract which version it is - will work for any digit version number
            Dim iVersionTest As Integer, iVersion As Integer
            iVersionTest = CInt(Mid(Spanish, InStr(1, Spanish, "(") + 1, InStr(1, Spanish, ")") - InStr(1, Spanish, "(") - 1)) 
            'for another method see
            'iVersionTest = CInt(Split(Split(Spanish,"(")(1),")")(0)

            'is current version greater than what is already stored? if so, make it latest version
            Dim sLatestVersion 
            If iVersionTest > iVersion Then
                sLatestVersion = Spanish
                iVersion = iVersionTest
            End If

        Else

            'if there's no other version make the lone file the latest version
            If Not bVersion Then sLatestVersion = Spanish

        End If

        Spanish = Dir

Loop

LanguageFiles(0) = sLatestVersion