在access中如何用VBA实现将用以下代码查询的数据生成一份名为存量日期的EXECL文件?SELECT 接触清单.呼叫日期 FROM 接触清单 GROUP BY 接触清单.呼叫日期 ORDER BY 接触清单.呼叫日期 DESC
时间: 2024-03-15 08:44:08 浏览: 163
你可以使用以下的 VBA 代码来实现将查询结果生成一份名为存量日期的 Excel 文件:
```VBA
Sub ExportDataToExcel()
Dim rs As Recordset
Dim excelApp As Object
Dim excelWorkbook As Object
Dim excelWorksheet As Object
Dim exportFileName As String
Dim i As Integer
' Set export file name
exportFileName = "存量日期.xlsx"
' Open recordset with query result
Set rs = CurrentDb.OpenRecordset("SELECT 接触清单.呼叫日期 FROM 接触清单 GROUP BY 接触清单.呼叫日期 ORDER BY 接触清单.呼叫日期 DESC")
' Create new Excel application
Set excelApp = CreateObject("Excel.Application")
excelApp.Visible = True
' Create new workbook and worksheet
Set excelWorkbook = excelApp.Workbooks.Add
Set excelWorksheet = excelWorkbook.Sheets(1)
' Write headers to worksheet
excelWorksheet.Cells(1, 1).Value = "呼叫日期"
' Write data to worksheet
i = 2
While Not rs.EOF
excelWorksheet.Cells(i, 1).Value = rs("呼叫日期")
i = i + 1
rs.MoveNext
Wend
' Save workbook and close Excel application
excelWorkbook.SaveAs exportFileName
excelWorkbook.Close
excelApp.Quit
' Clean up
Set excelWorksheet = Nothing
Set excelWorkbook = Nothing
Set excelApp = Nothing
rs.Close
Set rs = Nothing
End Sub
```
这段代码会执行你提供的查询,将结果写入名为“存量日期.xlsx”的 Excel 文件中。你可以在代码中修改查询和文件名以符合你的需求。
阅读全文