我將查詢結果輸出到Excel,並希望通過Access VBA語法進行格式化。每次嘗試添加格式時,都會出現錯誤。我被困在一個從Access訪問VBA的格式Excel工作表
無法設置Range類
這是我的語法的HorizontalAlignment屬性 - 應該怎樣改變,以便需要語法可以修改的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
編輯
我想下面的語法,而我得到的
所需的對象
就行了錯誤Selection.Font.Bold = True
這個充滿修飾語法
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
看看這個答案。嘗試使用數值。 http://stackoverflow.com/questions/6952084/excel-cell-alignments-numerical-values-for-e-g-xlleft-xlright-or-xlcenter – geeFlo
甚至使用數值仍然是相同的問題。 – BellHopByDayAmetuerCoderByNigh