将Access查询导出到Excel

我有一个Access 2007数据库,我已经创build了大约15个SQL查询来处理特定数据,我创build了一个使用Access中的菜单的主框架导航菜单,现在我需要使用VBA代码将所有查询提取到Excel,已经设法通过创build一个button并为其指定此代码来执行下面的代码。

Private Sub query1_Click() DoCmd.TransferSpreadsheet acExport, _ acSpreadsheetTypeExcel9, "Total Users and Sessions", _ "C:\UsersandSessions.xls", , "Total Users & Sessions" End Sub 

现在我的问题是,查询被导出到Excel的罚款,但没有任何格式的应用程序完成,我想添加一些格式,至less在标题和可能在电子表格中的标题和一个我不太喜欢的是所有的logging都是从第一个单元格开始的。 另外我更喜欢如果我再次在Access中按下该button,Excel电子表格已经存在与该查询输出,然后再次单击时,它会再次写入到下一个可用表。

任何build议或想法非常受欢迎。

简短的故事,你不能。 您可能可以在Excel端执行一些脚本来格式化结果文件。 如果你想要漂亮的东西,你可能想创build一个报告。

您可以,而是将Excel表安装为表,然后在Excel文件中分隔的工作表上,引用第一个工作表,并格式化第二个工作表进行查看。

如果使用DoCmd.TransferSpreadsheet并创build一个原始文件,然后对其进行编辑以使格式正确,那么可以再次运行DoCmd.TransferSpreadsheet,它将使用值更新文件,但保留格式。

但是,如果人类通过添加新的选项卡或添加计算等来更改文件,那么DoCmd.TransferSpreadsheet将不再工作,并将失败,并显示一个丑陋的错误消息。 因此,我们在环境中执行的操作是将DoCmd.TransferSpreadsheet复制到带有格式的原始文件,然后在VBA中将该文件复制到用户桌面,然后打开该复制,以便用户不会混淆原始来源excel文件。

这种方法是一个最小的代码,干净,易于维护的解决scheme。 但它确实需要一个额外的“源”或原始文件挂起。 在Access 2007中工作。

你也希望结果结束在一个新的标签。 不幸的是,我认为这需要一些excel自动化。 Acccess里面的VBA可以在Excel中调用VBA中的函数。 该VBA可以根据需要复制标签。

我的想法是从Access的Excel自动化混合,并在Excel中创build一个模板,并有一个数据表链接到您的查询。 开始在Excel中创build数据表。 如果你想或者在任何地方,你可以开始向下三行,向右两列。 转到你的数据选项卡,点击访问,find你的数据库,select你想要链接到您的查询,select表作为单选button,但单击属性下一步,而不是确定,取消选中启用后台刷新,这部分是关键…下连接string中的定义选项卡,你会看到一个部分,说:模式=共享拒绝写更改为模式=读取,这将确保查询刷新毫无错误的MS Access VBA,而打开数据库,并将保持您的如果你的查询是一个可写的查询,用户可以回写数据库。 设置完成后,您可以调整表格格式,但是您可以从表格devise选项卡中进行select,并保留该格式。

为了达到这个目的,我们假设你在单元格B4中启动了表格,并且为了以下VBA示例的目的,命名工作表CurrentDay,确保将该引用replace为实际的位置。

接下来回到Access并编写你的VBA,首先确保在你的VBA窗口中,你可以参考Microsoft Excel 12.0 Object Library,selectTools> References并从字母列表中select它。 创build你的子如下:

 Sub query1_click() Dim xl as Excel.Application Dim wbk as Excel.Workbook Dim wks as Excel.Worksheet Dim RC as Integer Dim CC as Integer Set xl = New Excel.Application Set wbk = xl.wbk.Open "X:\Filelocation\FileName.xlsx" 'name and path you saved the file you previously created xl.Visible = True 'The above is not necessary but you may want to see your process work the first few times and it will be easier than going to task manager to end Excel if something fails. RC = xl.Application.CountA(xl.wbk.Worksheets("CurrentDay").Range("B:B")) + 3 'This will count the rows of data in your table including your header so you can copy the data to another tab dynamically as the size of your table expands and shrinks we add 3 to it because we started at row 4 and we need the location of the last row of the record set. CC = xl.Application.CountA(xl.wbk.Worksheets("CurrentDay").Range("4:4")) + 1 'This counts the header row and adds one space because we will use this as a location holder for our copy / paste function Set wks = xl.wbk.Worksheets.Add wks.Name = format(date(),"MM_dd_yy") 'this will name the tab with today's date... you can eliminate this step if you just want the sheets to be the generic Sheet1, Sheet2, etc. With xl.wbk .Worksheets("CurrentDay").Range(Cells(4,2),Cells(RC,CC)).Copy .wks.PasteSpecial xlPasteValues 'This pastes the values so that the table links do not paste otherwise every tab would just refresh everyday. .wks.PasteSpecial xlPasteFormats 'This gets your formatting. .RefreshAll 'This will refresh your table Wend With xl .Save .Close False .Quit Wend Set xl = Nothing Set wbk = Nothing Set wks = Nothing End Sub 

这应该让你的数据不能从你的工作表的A1开始,每次保存你的旧数据,并自动执行访问步骤。