[英]Format Excel Worksheet From Access VBA
I am outputting query results to Excel and want to format through my Access VBA syntax. 我正在将查询结果输出到Excel,并希望通过Access VBA语法进行格式化。 Everytime I try to add in the formatting, I get errors.
每次尝试添加格式时,都会出现错误。 The one I am stuck on is
我坚持的是
Unable to set the HorizontalAlignment property of the Range class
无法设置Range类的HorizontalAlignment属性
This is my syntax - what should be altered so that the syntax can modify Excel as needed? 这是我的语法-应该进行哪些更改,以便语法可以根据需要修改Excel?
Private Sub ToExcel_Click()
Dim lngColumn As Long
Dim xlx As Object, xlw As Object, xls As Object, xlc As Object
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strPathFileName As String, strWorksheetName As String
Dim strRecordsetDataSource As String
Dim blnEXCEL As Boolean, blnHeaderRow As Boolean
blnEXCEL = False
strPathFileName = "Z:\House\Data.xlsx"
strRecordsetDataSource = "Fa"
blnHeaderRow = True
On Error Resume Next
Set xlx = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set xlx = CreateObject("Excel.Application")
blnEXCEL = True
End If
Err.Clear
On Error GoTo 0
xlx.Visible = True
Set xlw = xlx.Workbooks.Add
Set xls = xlw.Worksheets(1)
xls.Name = "Cu"
Set xlc = xls.Range("A1")
Set dbs = CurrentDb()
Set rst = dbs.OpenRecordset(strRecordsetDataSource, dbOpenDynaset, dbReadOnly)
If rst.EOF = False And rst.BOF = False Then
If blnHeaderRow = True Then
For lngColumn = 0 To rst.Fields.Count - 1
xlc.Offset(0, lngColumn).Value = rst.Fields(lngColumn).Name
Next lngColumn
Set xlc = xlc.Offset(1, 0)
End If
xlc.CopyFromRecordset rst
End If
rst.Close
Set rst = Nothing
dbs.Close
Set dbs = Nothing
With xls
.Range("A1:N1").Select
.Columns("A:N").HorizontalAlignment = xlCenter
.Columns("A:N").VerticalAlignment = xlBottom
.Columns("A:N").WrapText = True
.Columns("A:N").Orientation = 0
.Columns("A:N").AddIndent = False
.Columns("A:N").IndentLevel = 0
.Columns("A:N").ShrinkToFit = False
.Columns("A:N").ReadingOrder = xlContext
.Columns("A:N").MergeCells = False
.Columns("A:N").Selection.Font.Bold = True
.Columns("N:N").ColumnWidth = 8.86
.Columns("I:I").ColumnWidth = 8.86
.Columns("C:G").Select
.Selection.NumberFormat = "$#,##0"
.Columns("J:J").Select
.Selection.NumberFormat = "$#,##0"
.Columns("K:M").Select
.Selection.NumberFormat = "0%"
.Range("P18").Select
.Columns("A:A").EntireColumn.AutoFit
.Columns("B:B").EntireColumn.AutoFit
.Range("A1").Select
End With
Set xlc = Nothing
Set xls = Nothing
xlw.SaveAs strPathFileName
xlw.Close False
Set xlw = Nothing
If blnEXCEL = True Then xlx.Quit
Set xlx = Nothing
End Sub
EDIT 编辑
I tried the below syntax, and I get an error of 我尝试了以下语法,但出现错误
object required
所需对象
on the line Selection.Font.Bold = True
在
Selection.Font.Bold = True
this is full modified syntax 这是完整的修改语法
With xls
'.Range("A1:N1").Select
.Columns("A:N").Select
Selection.Font.Bold = True
.Columns("A:N").HorizontalAlignment = xlCenter
.Columns("A:N").VerticalAlignment = xlBottom
.Columns("A:N").WrapText = True
.Columns("A:N").Orientation = 0
.Columns("A:N").AddIndent = False
.Columns("A:N").IndentLevel = 0
.Columns("A:N").ShrinkToFit = False
.Columns("A:N").ReadingOrder = xlContext
.Columns("A:N").MergeCells = False
'.Columns("A:N").Selection.Font.Bold = True
.Columns("N:N").ColumnWidth = 8.86
.Columns("I:I").ColumnWidth = 8.86
.Columns("C:G").Select
Selection.NumberFormat = "$#,##0"
.Columns("J:J").Select
Selection.NumberFormat = "$#,##0"
.Columns("K:M").Select
Selection.NumberFormat = "0%"
.Range("P18").Select
.Columns("A:A").EntireColumn.AutoFit
.Columns("B:B").EntireColumn.AutoFit
.Range("A1").Select
End With
I am running Excel 2013 and this syntax works for me. 我正在运行Excel 2013,并且此语法对我有用。 First, in the VBE for Access ensure that you have added a reference to
首先,在Access VBE中,确保已添加对
Microsoft Excel 15.0 Object Library
Microsoft Excel 15.0对象库
Then to shorten your syntax you can just use the below: 然后,为了缩短语法,您可以使用以下代码:
With xls
.rows("1:1").Font.Bold = True
.Range("A:N").HorizontalAlignment = xlCenter
.Range("A:N").VerticalAlignment = xlBottom
.Range("C:G").NumberFormat = "$#,##0"
.Range("J:J").NumberFormat = "$#,##0"
.Range("K:M").NumberFormat = "0%"
.Range("A:N").WrapText = True
.Range("N:N").ColumnWidth = 8.86
.Range("I:I").ColumnWidth = 8.86
.Range("A:A").ColumnWidth = 9
End With
That should do all the formatting you are after and remove the "fluff" syntax from what appears to be the Macro Recorder. 这样就可以完成所有格式化工作,并从看起来像是宏记录器的内容中删除“ fluff”语法。 I believe (although I did not test) that the code provided by @June7 would also work if you add the reference to Excel from the VBE.
我相信(尽管我没有测试),如果您从VBE添加对Excel的引用,则@ June7提供的代码也将起作用。
Tested your code by removing all the recordset stuff just to test the Excel parts. 通过删除所有记录集内容(仅用于测试Excel部件)来测试代码。 Don't quite get the same error.
不太会得到相同的错误。 For me, the 4 lines with Selection reference cause issue.
对我来说,带有“选择”参考的4行会引起问题。
Added line to select columns A:N and modified the line setting font: 添加了行以选择列A:N,并修改了行设置字体:
.Columns("A:N").Select .Columns(“ A:N”)。选择
Selection.Font.Bold = True Selection.Font.Bold = True
I Removed the dot (.) from front of Selection on the other 3 lines. 我从其他三行的Selection前面删除了点(。)。
Code ran without error. 代码运行无误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.