【问题标题】:Exporting data from Access to Excel workbooks/sheets based on numerous conditions根据多种条件将数据从 Access 导出到 Excel 工作簿/工作表
【发布时间】:2015-09-23 05:07:50
【问题描述】:

我有一些数据结构如下:

sglAccNumber    intDaysOld      intRouterLocation   intDaysInLocation
1638828663      614             Customer Service    05. - 61-90 Days
1955963013      348             Advertising         03. 16-45 Days
1198680816      1678            Accounting          09. 401-730 Days
1892708307      1860            Accounting          010. 730+ Days
1785581943      1005            Asset Management    02. 6-15 Days
1942406908      1853            Finances            09. 401-730 Days

等等...有60,000行数据。

我希望根据 intRouterLocation 名称将数据从 Access 表移动到许多不同的工作簿。我正在努力解决的问题是,在每个单独的工作簿中,还要将数据移动到名为 intDaysInLocation 的工作表中。

例如,使用上述数据,会计工作簿将生成两张工作表,一张用于 09。 401-730 天,一个用于 010。 730+ 天 并且适当的条目将填充每个。

过去几天我一直在为此苦苦挣扎,可以将数据按名称或按值进入工作簿或 intDaysInLocation,但将它们结合起来会更好我。

这是使用 VBA 可以实现的吗?

我用来整理工作表的代码(全部在一张工作表中,不考虑 intRouterLocation):

Sub exportMk2 ()

Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rs As DAO.Recordset
Dim strPath As String
Dim strSelectOneType As String
Dim strSelectDaysInLocation As String

' (change strPath back to what you need)
strPath = CurrentProject.Path & Chr(92) & "Pets_dataset_export_" & _
    Format(Date, "yyyy-mm-dd") & ".xlsx"
strSelectDaysInLocation = "SELECT DISTINCT p.intDaysInLocation" & vbCrLf & _
    "FROM Worksheet AS p;"

Set db = CurrentDb
Set rs = db.OpenRecordset(strSelectDaysInLocation, dbOpenSnapshot)
Set rsRouters = db.OpenRecordset(strSelectDaysInLocation, dbOpenSnapshot)

For Each routerLocation In rsRouters
    Do While Not rs.EOF
        strSelectOneType = "SELECT p.ID, p.intDaysInLocation, p.intRouterLocation" & vbCrLf & _
            "FROM Worksheet AS p" & vbCrLf & _
            "WHERE p.intDaysInLocation='" & rs!intDaysInLocation.Value & "';"
        Debug.Print strSelectOneType
        Set qdf = db.QueryDefs("qryExportMe")
        qdf.SQL = strSelectOneType
        qdf.Close
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, _
            "qryExportMe", strPath, True, "woot " & rs!intDaysInLocation.Value
        rs.MoveNext
    Loop
Next
rs.Close
End Sub

【问题讨论】:

  • 嗯。如果你可以为一个做,你可以为两个做。试试andif 语句?
  • 您好,感谢您的回复。但是,有人建议我使用 Do.. While 查找而不是 If 语句来遍历记录集。我将尝试找出另一种迭代方式。谢谢你的帮助,伊恩
  • 您混淆了这两个过程。您确实需要一个 do while 循环来提取数据,但是您需要一个 if 语句来确定将您提取的数据放在哪里......到目前为止您有什么代码?
  • 再次感谢您的回复。我从这里提取的最成功的代码link
  • 呃,我试图过去我的代码,但它太长了

标签: excel ms-access vba


【解决方案1】:

我想我构建了你需要的东西。只需将其指向正确的表、字段和导出位置,就像在测试子中一样。它需要从 Access 运行,并引用您的 Excel 库。

Public Sub Test()

    ExportToExcel "tblData", "intRouterLocation", "intDaysInLocation", CurrentProject.Path & "\Export\"

End Sub

Public Sub ExportToExcel(sTableName As String, sWorkBookNameField As String, sSheetNameField As String, sDestinationFolder As String)

    Dim rsData          As Recordset
    Dim oXL             As Excel.Application
    Dim oWB             As Excel.Workbook
    Dim oSH             As Excel.Worksheet
    Dim sPrevWB         As String
    Dim sPrevSheet      As String
    Dim lRecordcount    As String
    Dim vTempArray()    As Variant
    Dim lFieldID        As Long
    Dim lRecordID       As Long

    With CurrentDb.OpenRecordset("SELECT [" & sWorkBookNameField & "],[" & sSheetNameField & "] FROM [" & sTableName & "] GROUP BY [" & sWorkBookNameField & "],[" & sSheetNameField & "] ORDER BY [" & sWorkBookNameField & "],[" & sSheetNameField & "] DESC;")
        If .EOF And .BOF Then
            .Close
            MsgBox "No data found"
            Exit Sub
        End If

        Set oXL = New Excel.Application

        Do Until .EOF
            If sPrevWB <> .Fields(sWorkBookNameField) Then
                If Not oWB Is Nothing Then
                    oWB.Close True
                    Set oWB = oXL.Workbooks.Add
                Else
                    With oXL
                        Set oWB = .Workbooks.Add
                        .Calculation = xlCalculationManual
                        .ScreenUpdating = False
                    End With
                End If

                oWB.SaveAs sDestinationFolder & .Fields(sWorkBookNameField) & ".xlsx"
                sPrevWB = .Fields(sWorkBookNameField)
                Set oSH = oWB.Sheets(1)
            ElseIf sPrevSheet <> .Fields(sSheetNameField) Then
                If oSH.Index + 1 > oWB.Sheets.Count Then oWB.Sheets.Add
                Set oSH = oWB.Sheets(oSH.Index + 1)
            End If

            oSH.Name = .Fields(sSheetNameField)

            'Push data to sheet (numerous methods, I just picked one)
            Set rsData = CurrentDb.OpenRecordset("SELECT * FROM [" & sTableName & "] WHERE [" & sWorkBookNameField & "]='" & .Fields(sWorkBookNameField) & "' AND [" & sSheetNameField & "]='" & .Fields(sSheetNameField) & "'")

            rsData.MoveLast
            lRecordcount = rsData.RecordCount
            rsData.MoveFirst

            vTempArray = rsData.GetRows(lRecordcount)

            For lFieldID = 0 To UBound(vTempArray, 1)
                oSH.Cells(1, lFieldID + 1) = rsData.Fields(lFieldID).Name
                For lRecordID = 0 To UBound(vTempArray, 2)
                    oSH.Cells(lRecordID + 2, lFieldID + 1) = vTempArray(lFieldID, lRecordID)
                Next lRecordID
            Next lFieldID
            oSH.Cells.EntireColumn.AutoFit
            .MoveNext
        Loop
        .Close
    End With

    oWB.Save
    oXL.Quit

    Set rsData = Nothing
    Set oSH = Nothing
    Set oWB = Nothing
    Set oXL = Nothing

End Sub

【讨论】:

  • PS 如果您在数据库文件旁边创建子文件夹 Export,那么您可以使用 CurrentProject.Path & "\Export\" 作为一个参数
  • 抱歉回复晚了。我摆弄了一下它,它的工作原理!非常感谢您的帮助!
  • 不客气。也许您可以标记我的答案以便将其关闭?提前谢谢你。
猜你喜欢
  • 2015-12-05
  • 1970-01-01
  • 1970-01-01
  • 2011-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-25
  • 1970-01-01
相关资源
最近更新 更多