I have a database that exports a table to Excel and I need the cell formats in Column Z to be either Currency or Percentage based upon the value in Column Y. I know I can do this in Excel with conditional formatting, but I am stumped on how to do it in Access.
I tried this
Sub GetExcel_INV_Hist(File_Path_Name)
Dim MyXL As Object
Set MyXL = CreateObject("Excel.Application")
MyXL.Workbooks.Open (File_Path_Name)
MyXL.Visible = True
' IHFormat Macro
MyXL.Application.Columns("Z:AC").Select
MyXL.Application.Selection.NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
MyXL.Application.Range("Z:Z").Select
MyXL.Application.Selection.NumberFormat = "0.00%"
MyXL.Application.Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=$Y3 = ""GM"""
MyXL.Application.Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
MyXL.Application.ExecuteExcel4Macro "File_Path_Name!(2,1,""0.00%"")"
MyXL.Application.Selection.FormatConditions(1).StopIfTrue = False
MyXL.Application.Selection.Copy
MyXL.Application.Range("Z4").Select
MyXL.Application.Range(Selection, Selection.End(xlDown)).Select
MyXL.Application.Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Which first changed the format of Z to currency, then I selected the first cell, Z3, checked to see if Y3 = GM and if it did, changed the format to percent. Then select the rest of Z and did a Paste Special/Format. It throws an error at the ExecuteExcel4Macro line. I'm assuming because the conditional formatting wasn't done on the workbook the process creates.
Thanks in advance for any help.
Edit: I recorded the macro in excel, so the line in question was created as part of the excel macro. I've expanded my code and this is the error:
The rest of the formatting works perfectly.
Thanks again
Application.ExecuteExcel4Macro
, but I agree, it's not clear what this has to do with the conditional formatting. - BigBenxlExpression
means - you'll need to update that to2
(the numerical equivalent of xlExpression). - Darren Bartrup-Cook