将部分名称的CSV文件导入excel

时间:2015-06-12 08:34:38

标签: excel vba excel-vba csv import

我有很多CSV文件,我想使用宏将它们导入(并操纵它们)到现有的excel文件中。

问题是我只知道文件的一部分。他们就像:

  • “ThisIsTheFirstFile-01234564893.csv”
  • “ThisIsTheSecondFile-015648942314.csv”......

所以我用这段代码导入了我的文件:

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

但它似乎与文件名不匹配。现在我只想加载一个文件,然后我会操纵它,提取数据,压缩所有加载的数据以加载其他文件。

1 个答案:

答案 0 :(得分:0)

根据thisfile只包含文件名,您必须连接该文件夹。