Excel 互操作格式

发布于 2025-01-06 02:52:24 字数 1680 浏览 0 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝颜夕 2025-01-13 02:52:24

像这样的东西应该可以完成工作。这将简单地将标题设置为黄色内部颜​​色,并自动调整列。

With xlSumSht.Range(Cells(SummaryTitleRow, 1), Cells(SummaryTitleRow, rstSummary.Fields.Count))
    .Interior.Color = vbYellow
    .EntireColumn.AutoFit
End With

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.

With xlSumSht.Range(Cells(SummaryTitleRow, 1), Cells(SummaryTitleRow, rstSummary.Fields.Count))
    .Interior.Color = vbYellow
    .EntireColumn.AutoFit
End With
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文