使用SQL

时间:2015-05-15 11:32:17

标签: vba excel-vba csv import excel

我有150个使用pipe |的csv格式的文本文件作为一个分离者。这些文件位于网络上的不同文件夹中。文件名是特定于日期的,并且具有保存日期的后缀。我创建了一个包含3个字段的表:文件位置;文件名不包括后缀;后缀(格式为yymmdd)。

我想创建一个SQL脚本,它将我创建的表中指定的150个文件中的每个文件导入150个单独的Access 2007表,这些表名为不包括后缀的文件名。我尝试使用VBA执行此操作,但文件名包含完整停止,VBA不喜欢这样。

每个文件都有不同的列结构,但第一行始终是标题。我只能使用Access 2007本机功能,因为我工作的组织不允许第三方插件或应用程序。我没有SQL服务器可供我使用。

对于Access而言,我是一个完全的新手,并且正在努力实现接近这一目标的任何事情。你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

我努力在Access中实现我想要的东西,所以转而使用Excel。下面的代码将每个文本文件创建为单个Excel工作簿中的工作表,主表将文件名/路径等作为工作簿中的“主”工作表。它会在重新创建之前删除“Master”以外的所有工作表,如果找不到文件,则会留下一张空白工作表。

Sub ImportFiles()

'This script looks at a Master list of files for import and imports each to their own tab
'The files are pipe (|) delimited and can be in any addressable location
'If any files are not found, the import of that file is skipped, leaving a blank worksheet

'Close the Report File before starting the import
    Application.DisplayAlerts = False
    On Error Resume Next
    Windows("Report.xlsx").Activate
    Windows("Report.xlsx").Close SaveChanges:=True
    On Error GoTo 0
    Application.DisplayAlerts = True

'Start by looking at the sheet which contains the Master list of files
    Sheets("Master").Activate

'declare three arrays of unknown length
    Dim FileName() As String
    Dim FilePath() As String
    Dim FullName() As String

'initially there are no files
    Dim NumberFiles As Integer
    NumberFiles = 0

'loop over all of the file cells
'The master file needs to be structured FileName, FilePath and FullName in that order
'Change C2 to the cell containing the first FileName in the Master List
    Dim FileCell As Range
    Dim TopCell As Range
    Dim FileRange As Range

    Set TopCell = Range("C2")
    Set FileRange = Range(TopCell, TopCell.End(xlDown))
    For Each FileCell In FileRange

'we've found another file!
    NumberFiles = NumberFiles + 1

'for each file found, extend all arrays
    ReDim Preserve FileName(NumberFiles - 1)
    ReDim Preserve FilePath(NumberFiles - 1)
    ReDim Preserve FullName(NumberFiles - 1)

'now store the filename, path and fullname of the new file
    FileName(NumberFiles - 1) = FileCell.Value
    FilePath(NumberFiles - 1) = FileCell.Offset(0, 1).Value
    FullName(NumberFiles - 1) = FileCell.Offset(0, 2).Value

Next FileCell

'delete any existing sheets except Master and create new blank sheets
For Each Sheet In Application.Worksheets
    Application.DisplayAlerts = False
    If Sheet.Name <> "Master" Then
    Sheet.Delete
    End If
    Next Sheet
    Sheets("Master").Activate
    For i = 1 To (NumberFiles)
    Worksheets.Add(After:=Sheets(Sheets.Count)).Name = "Sheet" & i
    Next i
    ThisWorkbook.Sheets("Sheet1").Select

'Start the process of import

'create a workbook object for the workbook where the current macro is running
    Dim ws As Worksheet

'import each file into its own sheet
    For i = 0 To (NumberFiles - 1)

    Set ws = ThisWorkbook.Sheets("Sheet" & i + 1) 'the current sheet

'Ignore any missing files and carry on processing
    Application.DisplayAlerts = False
    On Error Resume Next

'This imports the delimited files
    With ws.QueryTables.Add(Connection:="TEXT;" & FullName(i),     Destination:=ws.Range("$A$1"))
        .Name = "a" & i
        .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 = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileOtherDelimiter = "|"
        .TextFileColumnDataTypes = Array(1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False

    End With

'Rename tabs to name on master list
    ws.Name = "" & FileName(i)

    Next i

'Reopen the Report File
    Workbooks.Open FileName:=ThisWorkbook.Path & "\Report.xlsx"

End Sub