I have an Access 2007 database where I generate and export a report to Excel 2007. I then have a button on my report to open and format the exported file. The file opens just fine. The initial formating is completed, but then it hacks on defining conditional formats. My conditional formating is pretty simple - comparing all the date fields in column G with 4 other date fields (A1, B1, C1, and D1). The error is one I found to be common when searching the net 'Run-time error '5': Invalid procedure call or argument' I have not been able to find another thread with a solution that fitls my issue. Below is my code - it stops on the first call to Add a Formula for conditional format. I followed other advise of recording a macro in Excel and then copy/paste to Access, but I've made some minor modifications not pertaining to the conditional formulas. Any help is much appriciated.
Private Sub btnExport_SB_Click()
Dim objXLApp As Object
Dim objXLBook As Object
Dim strFormula1 As String
Dim strFormula2 As String
Dim strFormula3 As String
Dim strFormula4 As String
Set objXLApp = CreateObject("Excel.Application")
Set objXLBook = objXLApp.Workbooks.Open("D:\data\WorkData\Projects\ProjectPlanning\qryProjectMSTR3_SB.xls")
objXLApp.Application.Visible = True
'strFormula1 = "=IF(G1<$D$1,1,0)"
'strFormula2 = "=IF(G1<$C$1,1,0)"
'strFormula3 = "=IF(G1<$B$1,1,0)"
'strFormula4 = "=IF(G1<$A$1,1,0)"
With objXLApp
'Code from Excel Macro
.Rows.AutoFit
.Rows("2:2").Font.Bold = True
.Range("A1
1").NumberFormat = "m/d/yyyy"
.COLUMNS("G:G").NumberFormat = "m/d/yyyy"
.Range("A2").FormulaR1C1 = "Project Manager"
.Range("B2").FormulaR1C1 = "Oracle Number"
.Range("C2").FormulaR1C1 = "Customer"
.Range("D2").FormulaR1C1 = "Project Number"
.Range("E2").FormulaR1C1 = "Project"
.Range("K2").FormulaR1C1 = "Cost Center"
.COLUMNS("G:G").Select
With .COLUMNS("G:G")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=IF(G1<$D$1)=1"
.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
End With
With .COLUMNS("G:G").FormatConditions(1).Font
.Color = -1003520
.TintAndShade = 0
End With
.COLUMNS("G:G").FormatConditions(1).StopIfTrue = True
.COLUMNS("G:G").FormatConditions.Add Type:=xlExpression, Formula1:="=IF(G1<$C$1)=1"
.COLUMNS("G:G").FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With .COLUMNS("G:G").FormatConditions(1).Font
.ThemeColor = -11489280
.TintAndShade = 0
End With
.COLUMNS("G:G").FormatConditions(1).StopIfTrue = True
.COLUMNS("G:G").FormatConditions.Add Type:=xlExpression, Formula1:="=IF(G1<$B$1)=1"
.COLUMNS("G:G").FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With .COLUMNS("G:G").FormatConditions(1).Font
.Color = -15773952
.TintAndShade = 0
End With
.COLUMNS("G:G").FormatConditions(1).StopIfTrue = True
.COLUMNS("G:G").FormatConditions.Add Type:=xlExpression, Formula1:="=IF(G1<$A$1)=1"
.COLUMNS("G:G").FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With .COLUMNS("G:G").FormatConditions(1).Font
.Color = -16776961
.TintAndShade = 0
End With
.COLUMNS("G:G").FormatConditions(1).StopIfTrue = True
ActiveSheet.Outline.ShowLevels RowLevels:=1
End With
'Done Excel Formating
'wbk.Close SaveChanges:=True
'xlObj.Quit
End Sub
Private Sub btnExport_SB_Click()
Dim objXLApp As Object
Dim objXLBook As Object
Dim strFormula1 As String
Dim strFormula2 As String
Dim strFormula3 As String
Dim strFormula4 As String
Set objXLApp = CreateObject("Excel.Application")
Set objXLBook = objXLApp.Workbooks.Open("D:\data\WorkData\Projects\ProjectPlanning\qryProjectMSTR3_SB.xls")
objXLApp.Application.Visible = True
'strFormula1 = "=IF(G1<$D$1,1,0)"
'strFormula2 = "=IF(G1<$C$1,1,0)"
'strFormula3 = "=IF(G1<$B$1,1,0)"
'strFormula4 = "=IF(G1<$A$1,1,0)"
With objXLApp
'Code from Excel Macro
.Rows.AutoFit
.Rows("2:2").Font.Bold = True
.Range("A1

.COLUMNS("G:G").NumberFormat = "m/d/yyyy"
.Range("A2").FormulaR1C1 = "Project Manager"
.Range("B2").FormulaR1C1 = "Oracle Number"
.Range("C2").FormulaR1C1 = "Customer"
.Range("D2").FormulaR1C1 = "Project Number"
.Range("E2").FormulaR1C1 = "Project"
.Range("K2").FormulaR1C1 = "Cost Center"
.COLUMNS("G:G").Select
With .COLUMNS("G:G")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=IF(G1<$D$1)=1"
.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
End With
With .COLUMNS("G:G").FormatConditions(1).Font
.Color = -1003520
.TintAndShade = 0
End With
.COLUMNS("G:G").FormatConditions(1).StopIfTrue = True
.COLUMNS("G:G").FormatConditions.Add Type:=xlExpression, Formula1:="=IF(G1<$C$1)=1"
.COLUMNS("G:G").FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With .COLUMNS("G:G").FormatConditions(1).Font
.ThemeColor = -11489280
.TintAndShade = 0
End With
.COLUMNS("G:G").FormatConditions(1).StopIfTrue = True
.COLUMNS("G:G").FormatConditions.Add Type:=xlExpression, Formula1:="=IF(G1<$B$1)=1"
.COLUMNS("G:G").FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With .COLUMNS("G:G").FormatConditions(1).Font
.Color = -15773952
.TintAndShade = 0
End With
.COLUMNS("G:G").FormatConditions(1).StopIfTrue = True
.COLUMNS("G:G").FormatConditions.Add Type:=xlExpression, Formula1:="=IF(G1<$A$1)=1"
.COLUMNS("G:G").FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With .COLUMNS("G:G").FormatConditions(1).Font
.Color = -16776961
.TintAndShade = 0
End With
.COLUMNS("G:G").FormatConditions(1).StopIfTrue = True
ActiveSheet.Outline.ShowLevels RowLevels:=1
End With
'Done Excel Formating
'wbk.Close SaveChanges:=True
'xlObj.Quit
End Sub