使用格式导出Access查询到Excel

所以我有访问查询,我想发回到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 

目前代码执行,直到我的条件格式化块,然后它告诉我,对象variables或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 

祝你好运。