2013-10-23 67 views
1

所以我有一个访问查询,我想发回Excel。虽然使用导出向导很好,我想为导出过程添加更多自动化功能。到目前为止,我正在处理代码,因此在导出期间,最终的Excel表格将具有一些格式。至于基本格式我很好,我发现很多资源来帮助我。使用格式化将Excel访问查询导出到Excel

我的问题是,我想设置条件格式,以便如果一个特定的列(G)有一个值,那么整个行被突出显示。我是如何通过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块未设置。

+0

当为Object变量赋值(在这种情况下为Range)时,需要使用Set。 'Set rng = .Range(“A2:J20”)'(drop the .Select) –

+0

只需简单一点,这可能会有所帮助:我发现在Excel中使用Record Macro功能很有用。打开它,手动做你想让Excel为你做的事 - 然后关闭它并查看VBA源代码。您可以将VBA复制到Access并在稍作修改后使用它(创建Excel.Application对象,并在对象前面添加常量和顶级语句)。如果你在Excel VBA中的表现不是很强,那么通过VBA可以很好地了解格式化Excel的所有细微差别。 – DHW

+0

@TimWilliams这解决了我的问题,非常感谢 –

回答

1

我检查了下面的代码运行,直到结束:

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 

好运。

相关问题