由于您已经在谈论在 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