Excel 互操作格式
使用 Access 2007 vba 寻址 Excel 2007 工作簿(“WB”)...
此例程将工作表添加到现有且打开的 Excel 工作簿中,然后将记录集的行添加到工作表中。现在很混乱,我将其设置为创建多个工作表,所有工作表都以不同的方式查看相同的数据。
我已经成功发布了行,但现在我想更好地设置它们的格式,以避免对不了解 Excel 的目标用户造成混淆。我想更改标题单元格的颜色,并且想将列设置为“自动调整”。
有人可以帮忙吗?
Sub SummarySheets(WB As Excel.WorkBook, TempTableName As String)
Const SummarySheetName As String = "Timesheet Summaries"
Const SummaryQueryName As String = "qrySATempSummarybyWO"
Const SummaryTitleRow As Integer = 1
Dim xlSumSht As Excel.Worksheet
If SheetExists(WB, SummarySheetName) Then
WB.Sheets(SummarySheetName).Delete
End If
Set xlSumSht = WB.Sheets.Add(After:=WB.Sheets(WB.Sheets.count))
xlSumSht.NAME = SummarySheetName
xlSumSht.Activate
Dim intRow As Integer
Dim intCol As Integer
Dim intStartRow As Integer
Dim strSQL As String
Dim strQry As String
Dim rstSummary As DAO.Recordset
strQry = CurrentDbC.QueryDefs(SummaryQueryName).SQL
strSQL = Replace(strQry, "tblStaffAugTrans", "[" & TempTableName & "]")
Set rstSummary = CurrentDbC.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges + dbFailOnError)
If rstSummary.EOF = False Then
intRow = SummaryTitleRow
For intCol = 1 To rstSummary.Fields.count
xlSumSht.Cells(intRow, intCol).Value = rstSummary.Fields(intCol - 1).NAME
Next intCol
End If
While rstSummary.EOF = False
intRow = intRow + 1
For intCol = 1 To rstSummary.Fields.count
xlSumSht.Cells(intRow, intCol).Value = rstSummary.Fields(intCol - 1).Value
Next intCol
rstSummary.MoveNext
Wend
For intCol = 1 To rstSummary.Fields.count
'xlSumSht.Columns.EntireColumn(, intCol).AutoFit
Next intCol
rstSummary.Close
结束子
Using Access 2007 vba addressing an Excel 2007 workbook ("WB")....
This routine adds a worksheet to an existing and open excel workbook, then adds the rows of a recordset to the sheet. It's quite messy for now, I'm setting it up to create several sheets, all looking at the same data in different ways.
I have succeeded in posting the rows, but now I want to format them a little better to avoid confusing Excel-ignorant target users. I want to change the color of the header cells, and I want to set the columns to "autofit".
Can anyone help?
Sub SummarySheets(WB As Excel.WorkBook, TempTableName As String)
Const SummarySheetName As String = "Timesheet Summaries"
Const SummaryQueryName As String = "qrySATempSummarybyWO"
Const SummaryTitleRow As Integer = 1
Dim xlSumSht As Excel.Worksheet
If SheetExists(WB, SummarySheetName) Then
WB.Sheets(SummarySheetName).Delete
End If
Set xlSumSht = WB.Sheets.Add(After:=WB.Sheets(WB.Sheets.count))
xlSumSht.NAME = SummarySheetName
xlSumSht.Activate
Dim intRow As Integer
Dim intCol As Integer
Dim intStartRow As Integer
Dim strSQL As String
Dim strQry As String
Dim rstSummary As DAO.Recordset
strQry = CurrentDbC.QueryDefs(SummaryQueryName).SQL
strSQL = Replace(strQry, "tblStaffAugTrans", "[" & TempTableName & "]")
Set rstSummary = CurrentDbC.OpenRecordset(strSQL, dbOpenDynaset, dbSeeChanges + dbFailOnError)
If rstSummary.EOF = False Then
intRow = SummaryTitleRow
For intCol = 1 To rstSummary.Fields.count
xlSumSht.Cells(intRow, intCol).Value = rstSummary.Fields(intCol - 1).NAME
Next intCol
End If
While rstSummary.EOF = False
intRow = intRow + 1
For intCol = 1 To rstSummary.Fields.count
xlSumSht.Cells(intRow, intCol).Value = rstSummary.Fields(intCol - 1).Value
Next intCol
rstSummary.MoveNext
Wend
For intCol = 1 To rstSummary.Fields.count
'xlSumSht.Columns.EntireColumn(, intCol).AutoFit
Next intCol
rstSummary.Close
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的东西应该可以完成工作。这将简单地将标题设置为黄色内部颜色,并自动调整列。
Something like this should do the job. This will simply set the headings to have a Yellow interior colour and will auto-fit the columns.