我目前在MS Access 07上有一个查询,如下例所示。数据是动态的,因此区域数量每周都会发生变化。
ZONE ERROR
ZONE 1 | ERROR 1
ZONE 1 | ERROR 3
ZONE 2 | ERROR 4
ZONE 2 | ERROR 5
ZONE 3 | ERROR 1
ZONE 3 | ERROR 3
每周,我都需要运行此过程,为每周出现的每个区域创建一个单独的Excel文件。目前,我认为我可能需要将数据导入Excel,然后使用高级过滤器将每个区域ID标记为新列,Unique Zones,然后运行VBA代码,该代码将基于以下内容创建新文件Unique Zones列中的数据......不确定我是否在正确的路径上。
答案 0 :(得分:0)
由于您已经在谈论在Excel中使用VBA代码,因此您可能不会反对Access中的VBA解决方案。
假设你的表(或查询)被称为[ZoneErrors]。您可以在Access中创建类似以下VBA函数的东西,然后从Access宏调用它:
Public Function ExportZoneErrors()
Dim cdb As DAO.Database, rst As DAO.Recordset, qdf As DAO.QueryDef, _
excelFileName As String
Set cdb = CurrentDb
Set rst = cdb.OpenRecordset("SELECT DISTINCT [ZONE] FROM [ZoneErrors]", dbOpenSnapshot)
Do While Not rst.EOF
On Error Resume Next
cdb.QueryDefs.Delete "tmpErrorsForJustOneZone"
On Error GoTo 0
Set qdf = cdb.CreateQueryDef("tmpErrorsForJustOneZone", _
"SELECT * FROM [ZoneErrors] WHERE [ZONE]='" & rst!ZONE & "'")
qdf.Close
cdb.QueryDefs.Refresh
' build Excel file name (probably needs to be fancier in production)
excelFileName = "Errors for " & rst!ZONE & ".xlsx" ' e.g., "Errors for ZONE 1.xlsx"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "tmpErrorsForJustOneZone", excelFileName
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
Set cdb = Nothing
End Function