XLSX Conditional Formatting through Access

Experts. I have a function that dumps a query to excel (xls) then opens that workbook and applies conditional formatting to certain fields.

Our environment is now Office 2010. When the user now saves this spreadsheet and reopens it we lose the conditional formatting. If we do a save as and change the format to xlsx it works fine.

To avoid the user having to do a save as I went to change my output to XLSX in the database. When I did that, now none of my conditional formatting pulls into excel. The code does not error out it just doesn't fill in any of the cells.

Any ideas on how to either not have the user do a save as or do I need to modify all my code to get it to work with xlsx?


Private Sub cmdDPCAudit_Click()
On Error GoTo Err_cmdDPCAudit_Click

    Dim stDocName As String
    stDocName = "qryDPCAuditReport"
    DoCmd.OutputTo acOutputQuery, stDocName, acFormatXLSX, "C:\DDB\DPCAudit.xlsx", False
    Call ModifyDPCAudit("C:\DDB\DPCAudit.xlsx")

    Exit Sub

    MsgBox Err.Description
    Resume Exit_cmdDPCAudit_Click
End Sub

Public Sub ModifyDPCAudit(sFile As String)
On Error GoTo Err_ModifyDPCAudit

    Const xlExpression = 2
    Dim xlApp As Object
    Dim xlSheet As Object
    Dim xlRng As Object

    Set xlApp = CreateObject("Excel.Application")
    Set xlSheet = xlApp.Workbooks.Open(sFile).Sheets("qryDPCAuditReport")

  '  xlApp.Visible = True
    'format entire spreadsheet
    'format the header row
    Set xlRng = xlSheet.Range("A1:Y1")
    xlRng.Interior.Color = xlNone
    xlRng.Font.Bold = False
    xlRng.Font.Underline = True
    xlRng.Rows(2).Insert xlDown
    xlRng.Rows(1).Insert xlDown
    xlSheet.Range("G1").Value = "Tab 1"
    xlSheet.Range("I1").Value = "Tab 1"
    xlSheet.Range("K1").Value = "Tab 1"
    xlSheet.Range("M1").Value = "Tab 2"
    xlSheet.Range("O1").Value = "Tab 3"
    xlSheet.Range("Q1").Value = "Tab 4"
    xlSheet.Range("S1").Value = "Tab 5"
    xlSheet.Range("U1").Value = "Tab 6"
    xlSheet.Range("W1").Value = "Tab 7"
    'conditional formatting on overall
    Set xlRng = xlSheet.Range("E4:E500")
    xlRng.FormatConditions.Add Type:=xlExpression, Formula1:="=($F4=""yes"")"
    xlRng.FormatConditions(1).Interior.Color = vbRed
    xlSheet.Columns("F").Hidden = True

    'conditional formatting on resyndication
    Set xlRng = xlSheet.Range("G4:G500")
    xlRng.FormatConditions.Add Type:=xlExpression, Formula1:="=($H4=""yes"")"
    xlRng.FormatConditions(1).Interior.Color = vbRed
    xlSheet.Columns("H").Hidden = True
    'conditional formatting on feasibility pre
    Set xlRng = xlSheet.Range("I4:I500")
    xlRng.FormatConditions.Add Type:=xlExpression, Formula1:="=($J4=""yes"")"
    xlRng.FormatConditions(1).Interior.Color = vbRed
    xlSheet.Columns("J").Hidden = True
    'conditional formatting on feasibility post
    Set xlRng = xlSheet.Range("K4:K500")
    xlRng.FormatConditions.Add Type:=xlExpression, Formula1:="=($L4=""yes"")"
    xlRng.FormatConditions(1).Interior.Color = vbRed
    xlSheet.Columns("L").Hidden = True
    'conditional formatting on design
    Set xlRng = xlSheet.Range("M4:M500")
    xlRng.FormatConditions.Add Type:=xlExpression, Formula1:="=($N4=""yes"")"
    xlRng.FormatConditions(1).Interior.Color = vbRed
    xlSheet.Columns("N").Hidden = True
    'conditional formatting on bidding
    Set xlRng = xlSheet.Range("O4:O500")
    xlRng.FormatConditions.Add Type:=xlExpression, Formula1:="=($P4=""yes"")"
    xlRng.FormatConditions(1).Interior.Color = vbRed
    xlSheet.Columns("P").Hidden = True
    'conditional formatting on takeover
    Set xlRng = xlSheet.Range("Q4:Q500")
    xlRng.FormatConditions.Add Type:=xlExpression, Formula1:="=($R4=""yes"")"
    xlRng.FormatConditions(1).Interior.Color = vbRed
    xlSheet.Columns("R").Hidden = True
    'conditional formatting on Interim
    Set xlRng = xlSheet.Range("S4:S500")
    xlRng.FormatConditions.Add Type:=xlExpression, Formula1:="=($T4=""yes"")"
    xlRng.FormatConditions(1).Interior.Color = vbRed
    xlSheet.Columns("T").Hidden = True
    'conditional formatting on Implimentation
    Set xlRng = xlSheet.Range("U4:U500")
    xlRng.FormatConditions.Add Type:=xlExpression, Formula1:="=($V4=""yes"")"
    xlRng.FormatConditions(1).Interior.Color = vbRed
    xlSheet.Columns("V").Hidden = True
    'conditional formatting on completion
    Set xlRng = xlSheet.Range("W4:W500")
    xlRng.FormatConditions.Add Type:=xlExpression, Formula1:="=($X4=""yes"")"
    xlRng.FormatConditions(1).Interior.Color = vbRed
    xlSheet.Columns("X").Hidden = True
    'center columns
    Set xlRng = xlSheet.Range("D:W")
    xlRng.HorizontalAlignment = xlCenter
    'format the percentage columns
    Set xlRng = xlSheet.Range("E:W")
    xlRng.NumberFormat = "0%"
    'underline the tab header row
    Set xlRng = xlSheet.Range("A1:Y1")
    xlRng.Font.Underline = True
    'remove shading on the blank row
    Set xlRng = xlSheet.Range("A3:Y3")
    xlRng.Interior.Color = xlNone
    'remove borders in sheet
    xlSheet.Cells.Borders.LineStyle = xlLineStyleNone

    'change font in sheet
    xlSheet.Cells.Font.Name = "Calibri"
    xlSheet.Cells.Font.Size = 11
    'remove duplicates
    For IdxRow = xlSheet.Range("A" & xlSheet.Rows.Count).End(xlUp).Row To 2 Step -1
    If xlSheet.Range("A" & IdxRow - 1) = xlSheet.Range("A" & IdxRow) Then
       xlSheet.Range("A" & IdxRow) = ""
    End If
    xlApp.Visible = True
    Set xlApp = Nothing
    Set xlSheet = Nothing

    Exit Sub

    vStatusBar = SysCmd(acSysCmdClearStatus)
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_ModifyDPCAudit

End Sub

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

regmigrantConnect With a Mentor Commented:
I mean put use the previous code to create and xls but add a 'file save as' to the end of the macro to change it to xlsx - then excel should take care of the corrections for conditional format.

So run the macro as written (creating the xls) then add something like the following (coped from excelly.com) to force the format to xlsx with minimal user intervention


Private Sub SaveWorkbookAsNewFile(NewFileName As String)
    Dim ActSheet As Worksheet
    Dim ActBook As Workbook
    Dim CurrentFile As String
    Dim NewFileType As String
    Dim NewFile As String
    Application.ScreenUpdating = False    ' Prevents screen refreshing.

    CurrentFile = ThisWorkbook.FullName
    NewFileType = "Excel Files 1997-2003 (*.xls), *.xls," & _
               "Excel Files 2007 (*.xlsx), *.xlsx," & _
               "All files (*.*), *.*"
    NewFile = Application.GetSaveAsFilename( _
        InitialFileName:=NewFileName, _
    If NewFile <> "" And NewFile <> "False" Then
        ActiveWorkbook.SaveAs Filename:= NewFile, _
            FileFormat:=xlNormal, _
            Password:="", _
            WriteResPassword:="", _
            ReadOnlyRecommended:=False, _
        Set ActBook = ActiveWorkbook
        Workbooks.Open CurrentFile
    End If
    Application.ScreenUpdating = True
End Sub
Rather than change everything why not continue with the Xls save and then perform the save as at the end of the macro ?
acramer_dominiumAuthor Commented:
I was trying to avoid the user having to do a save as and changing the document type to xlsx. As new users come in they won't know to do that and will lose the formatting.
acramer_dominiumAuthor Commented:
BINGO! Never thought of that. Thank you for the suggestion. I added the below code to the end of my function:

'Save As
    xlApp.DisplayAlerts = False
    ActiveWorkbook.SaveAs FileName:="C:\DDB\DPCAudit.xlsx"
    xlApp.DisplayAlerts = True
Glad I could help - thanks for the feedback

All Courses

From novice to tech pro — start learning today.