【问题标题】:Formatting outputted Excel files from Access using VBA?使用 VBA 格式化从 Access 输出的 Excel 文件?
【发布时间】:2013-01-02 06:36:20
【问题描述】:

这里我有一些 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

【问题讨论】:

  • 不,您必须使用自动化打开 Excel 文件以对其进行格式化。

标签: excel vba ms-access


【解决方案1】:

这是 Phil.Wheeler 的 Code 和我之前的输入的快速而肮脏的组合,对我来说这是有效的。不要忘记在 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

【讨论】:

  • 您的答案就目前而言是正确的,但没有解决如何从 Access 为指定的和已关闭的工作簿执行此操作的重要方面。
  • 你是对的,当然。我建议创建一个循环遍历输出文件夹中所有文件的 excel 宏,然后按照上面的说明进行操作。如果您将该宏放在 Workbook_Open() 事件中,您应该能够通过仅打开这一个工作簿来执行您想要的访问权限。
  • Hm.. 另一方面,您可能没有这个超级酷的 excelfile 并通过访问完成所有操作 :o) 但我认为 Remou 是写的,您必须为此打开文件.
【解决方案2】:

是的,这是可能的!这是从我的一个代码中破解的,可能需要进行一些编辑才能工作......

'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...

【讨论】:

  • 如何包含 xl?我不断收到“未定义的变量”
【解决方案3】:

我也遇到过几次这个问题。正如@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

【讨论】:

    【解决方案4】:

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

    如果我编写了我负责的 VBA,直到我为此而死,这是我使用 excel 表时遇到的最大问题之一。这样(理论上)他们应该能够更改列,而无需更改数据的输出方式,无需您即可呈现。

    +1 打开 excel 文件本身并使用该自动化对其进行格式化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-19
      • 1970-01-01
      • 2015-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多