Excel Interop格式

时间:2012-02-15 17:42:15

标签: excel-vba interop excel-2007 vba excel

使用Access 2007 vba寻址Excel 2007工作簿(“WB”)....

此例程将工作表添加到现有和打开的Excel工作簿,然后将记录集的行添加到工作表。现在它非常混乱,我正在设置它以创建几张纸,所有纸张都以不同的方式查看相同的数据。

我已成功发布行,但现在我想更好地格式化它们以避免混淆Excel无知的目标用户。我想更改标题单元格的颜色,我想将列设置为“autofit”。

有人可以帮忙吗?

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

1 个答案:

答案 0 :(得分:1)

这样的事情应该可以胜任。这样可以简单地将标题设置为黄色内部颜​​色,并自动调整列。

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