繁体   English   中英

使用格式将访问查询导出到Excel

[英]Export Access Query to Excel with Formatting

因此,我有一个要发送回excel的访问权限查询。 虽然使用导出向导很好,但我想为导出过程增加更多的自动化。 到目前为止,我正在研究代码,因此在导出过程中,最终的excel工作表将具有某些格式。 就基本格式而言,我还可以找到很多资源来帮助我。

我的问题是我想设置条件格式,以便如果特定列(G)具有值,则整行都突出显示。 我对如何通过Access中的vba代码设置Excel的条件格式有些迷惑

这是我所拥有的

Dim appExcel As Variant
 Dim MyStr As String
 Dim rng As Excel.Range

' Creates Excel object and Adds a Workbook to it
    Set appExcel = CreateObject("Excel.application")
    appExcel.Visible = False
    appExcel.Workbooks.Add


    Set wksNew = appExcel.Worksheets("Sheet1")

    appExcel.Visible = True

' The first thing I do to the worksheet is to set the font.
' Not all are required, but I included them as examples.
 With appExcel
    .Cells.Font.Name = "Calbri"
    .Cells.Font.Size = 11
    .Cells.NumberFormat = "@"                                   'all set to Text Fields
    ' My first row will contain column names, so I want to freeze it
    .Rows("2:2").Select
    .ActiveWindow.FreezePanes = True

    ' ... and I want the header row to be bold
    .Rows("1:1").Font.Bold = True
    .Rows("1:1").Font.ColorIndex = 1
    .Rows("1:1").Interior.ColorIndex = 15

    ' Adds conditional formatting based on Values in the G column

    rng = .Range("A2:J20").Select
    rng.FormatConditions.Add Type:=xlExpression, Formula1:="=NOT($G2 = 0)"
    rng.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With appExcel.Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
    End With

End With

当前,代码执行到我的条件格式块为止,然后告诉我未设置对象变量或With块。

我检查了以下代码直到最后运行:

Dim appExcel As Variant
 Dim MyStr As String
 Dim rng As Excel.Range
 Dim wksNew

' Creates Excel object and Adds a Workbook to it
    Set appExcel = CreateObject("Excel.application")
    appExcel.Visible = False
    appExcel.Workbooks.Add


'   Set wksNew = appExcel.Worksheets("Sheet1")
    Set wksNew = appExcel.Worksheets(1)

    appExcel.Visible = True

' The first thing I do to the worksheet is to set the font.
' Not all are required, but I included them as examples.
 With appExcel
    .Cells.Font.Name = "Calbri"
    .Cells.Font.Size = 11
    .Cells.NumberFormat = "@"                                   'all set to Text Fields
    ' My first row will contain column names, so I want to freeze it
    .Rows("2:2").Select
    .ActiveWindow.FreezePanes = True

    ' ... and I want the header row to be bold
    .Rows("1:1").Font.Bold = True
    .Rows("1:1").Font.ColorIndex = 1
    .Rows("1:1").Interior.ColorIndex = 15

    ' Adds conditional formatting based on Values in the G column

    Set rng = .Range("A2:J20")
    rng.Select
    rng.FormatConditions.Add Type:=xlExpression, Formula1:="=NOT($G2 = 0)"
    rng.FormatConditions(rng.FormatConditions.Count).SetFirstPriority
    With rng.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
    End With

End With

祝好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM