Solved

XLSX Conditional Formatting through Access

Posted on 2013-12-06
5
656 Views
Last Modified: 2013-12-08
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_cmdDPCAudit_Click:
    Exit Sub

Err_cmdDPCAudit_Click:
    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
    xlSheet.Select
   
    'format entire spreadsheet
    xlSheet.Columns("A:Y").AutoFit
   
    '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
    Next
   
    xlApp.Visible = True
   
    Set xlApp = Nothing
    Set xlSheet = Nothing

Exit_ModifyDPCAudit:
    Exit Sub

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

End Sub

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
0
Comment
Question by:acramer_dominium
  • 3
  • 2
5 Comments
 
LVL 19

Expert Comment

by:regmigrant
ID: 39701904
Rather than change everything why not continue with the Xls save and then perform the save as at the end of the macro ?
Reg
0
 

Author Comment

by:acramer_dominium
ID: 39701911
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.
0
 
LVL 19

Accepted Solution

by:
regmigrant earned 500 total points
ID: 39701923
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

Reg

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, _
        fileFilter:=NewFileType)
 
    If NewFile <> "" And NewFile <> "False" Then
        ActiveWorkbook.SaveAs Filename:= NewFile, _
            FileFormat:=xlNormal, _
            Password:="", _
            WriteResPassword:="", _
            ReadOnlyRecommended:=False, _
            CreateBackup:=False
 
        Set ActBook = ActiveWorkbook
        Workbooks.Open CurrentFile
        ActBook.Close
    End If
 
    Application.ScreenUpdating = True
End Sub
0
 

Author Closing Comment

by:acramer_dominium
ID: 39701981
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
0
 
LVL 19

Expert Comment

by:regmigrant
ID: 39705013
Glad I could help - thanks for the feedback

Reg
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you how to use shortcut menus in the Access run-time environment.
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now