使用VBA从Access中格式化输出的Excel文件?

在这里,我有一些VBA代码输出到Excel文件吨文件。 我的问题是,从这里,有无论如何,格式的Excel文件有点? 我想要做的是使列粗体,并使列符合大小的标题。

Sub OutPutXL() Dim qdf As QueryDef Dim rs As DAO.Recordset Set qdf = CurrentDb.QueryDefs("OutputStudents") Set rs = CurrentDb.OpenRecordset("Teachers") Do While Not rs.EOF qdf.SQL = "SELECT * FROM Students WHERE contact='" & rs!contact & "'" ''Output to Excel DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, _ qdf.Name, "C:\Users\chrisjones\Documents\ProjectionsFY14\Teachers\" _ & rs!contact & ".xls", True rs.MoveNext Loop End Sub 

对的,这是可能的! 这是从我的一个代码黑客一起,可能需要一点编辑之前,它的工作…

 'This deals with Excel already being open or not On Error Resume Next Set xl = GetObject(, "Excel.Application") On Error GoTo 0 If xl Is Nothing Then Set xl = CreateObject("Excel.Application") End If Set XlBook = GetObject(filename) 'filename is the string with the link to the file ("C:/....blahblah.xls") 'Make sure excel is visible on the screen xl.Visible = True XlBook.Windows(1).Visible = True 'xl.ActiveWindow.Zoom = 75 'Define the sheet in the Workbook as XlSheet Set xlsheet1 = XlBook.Worksheets(1) 'Then have some fun! with xlsheet1 .range("A1") = "some data here" .columns("A:A").HorizontalAlignment = xlRight .rows("1:1").font.bold = True end with 'And so on... 

这是一个Phil.Wheeler的代码和我以前的input一个快速和肮脏的组合,对我来说这是工作。 不要忘记在Access-Macro中添加Excel的对象库。

 Sub doWhatIWantTheDirtyWay() pathToFolder = "C:\Users\Dirk\Desktop\myOutputFolder\" scaleFactor = 0.9 Set objExcel = CreateObject("Excel.Application") objExcel.Visible = False objExcel.DisplayAlerts = False Set objFso = CreateObject("Scripting.FileSystemObject") Set objFolder = objFso.GetFolder(pathToFolder) For Each objFile In objFolder.Files If objFso.GetExtensionName(objFile.path) = "xls" Then Set objWorkbook = objExcel.Workbooks.Open(objFile.path) For Each sh In objWorkbook.Worksheets If sh.UsedRange.Address <> "$A$1" Or sh.Range("A1") <> "" Then With sh columncount = .Cells(1, 256).End(xlToLeft).Column For j = 1 To columncount With .Cells(1, j) i = Len(.Value) .ColumnWidth = i * scaleFactor .Font.Bold = True End With Next End With End If Next objWorkbook.Close True End If Next objExcel.Quit End Sub 

我也偶然遇到过这个问题。 正如@Remou所说,你将需要打开excel来格式化xls文件,这个代码的修改默默地打开了Excel,这应该让你在正确的方向。 请记住在VBA项目中添加对Microsoft Excel对象库的引用。

 Sub OutPutXL() Dim qdf As QueryDef Dim rs As DAO.Recordset Dim xl as Excel.Application Dim wb as Object Dim strFile as string Set qdf = CurrentDb.QueryDefs("OutputStudents") Set rs = CurrentDb.OpenRecordset("Teachers") Set xl = New Excel.Application xl.DisplayAlerts = False Do While Not rs.EOF qdf.SQL = "SELECT * FROM Students WHERE contact='" & rs!contact & "'" 'Output to Excel strFile = "C:\Users\chrisjones\Documents\ProjectionsFY14\Teachers\" & rs!contact & ".xls" DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, qdf.Name, strFile, True 'Start formatting' Set wb = xl.Workbooks.Open(strFile) With wb.Sheets(qdf.name) 'Starting with a blank excel file, turn on the record macro function' 'Format away to hearts delight and save macro' 'Past code here and resolve references' End With wb.save wb.close set wb = Nothing rs.MoveNext Loop xl.quit set xl = Nothing End Sub 

你可以(取决于文件的数量)为你输出的每个文件制作一个模板。 从长远来看,如果有人需要改变格式,他们可以改变模板,这将是更容易的你,现在你不必筛选一堆Excel格式化垃圾。 你甚至可以让一个合格的最终用户这样做。

如果我写了我负责的VBA,直到我死了,这是我用Excel表格遇到的最大的问题之一。 这样(理论上)他们应该能够改变一列,而不改变数据的输出方式,只是在没有你的情况下呈现。

+1要打开Excel文件本身,并使用该自动化格式。