Results 1 to 3 of 3
  1. #1
    Kody_Devl is offline Novice
    Windows 10 Access 2016
    Join Date
    Jun 2017
    Posts
    22

    Access VBA to add Conditional Formatting In Excel (From Access)

    Click image for larger version. 

Name:	ConditionalFormat_Excel.png 
Views:	12 
Size:	17.2 KB 
ID:	42749

    Hi All,

    I want to add conditional formatting using VBA from Access inserted into my excel spreadsheet.
    I am unable to find specific code example that inserts the rules correctly (my desired rules which are displayed the attachment above).



    Here are some failed code attempts:
    Can you help me?

    Code:
    With sh.Name
    
    
       'With Range("A2:A30").FormatConditions.Add(xlCellValue, xlGreater, "400")
       'With Range("I2:I30").FormatConditions.Add(xlCellValue, xlEqual, "R")
       'With Range("I2:I30").FormatConditions.Add(xlCellValue, xlEqual, "R")
       'With Range("I2:I30").FormatConditions.Add("$AF2", xlEqual, "R")
       'With Range("I2:I30").FormatConditions.Add("$AF2", xlEqual, "R")
       'With Range("I2:I30").FormatConditions.Add (Type:=xlExpression, Formula1: "=$AF2="R"")
       'With Range("I2:I30").FormatConditions.Add (Type: =xlexpression, Formula1:="=$AF2="R"")
       'With Range("I2:I30").FormatConditions.Add(Type:=xlExpression, Formula1:="=$G2=$D2")
       'With Range("I2:I30").FormatConditions.Add(Type:=xlExpression, Operator:=xlEqual, Formula1:="=$AF2="R"")
       'With Range("I2:I30").FormatConditions.Delete
       'With Range("I2:I30").FormatConditions.Add(Type:=xlExpression, Formula:="=$AF2=R"")
       With Range("I2:I30").FormatConditions.Add(xlExpression, "=$AF2=R")
       
       With .Interior
       .ColorIndex = 36
       End With
    
       End With
       End With
    .



    Many Thanks

    Kody_Devl
    Last edited by Kody_Devl; 08-20-2020 at 05:03 AM. Reason: Lost the screen shot so I will attempt Attachment

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,114
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Part of the problem with the VBA code is there are 14 WITH statements, but only 3 END WITH statements. For each "With", there MUST be an "End With" (just like Sub...End Sub)

    Maybe this will help you
    I use a button ("cmdExportData") to export the data to an Excel workbook
    Code:
    Private Sub cmdExportData_Click()
       '
       'other code to export data to Excel workbook
       '
       'now edit the newly created workbook
       Call EditVendorWkSht(strSaveFileName, "Statement Monthly Summary", Me.cboMonth & " " & Me.cboYear & " Hours")
    End Sub
    The call runs the code to add two header rows and adds the Vendor Name and the Date
    Code:
    Sub EditVendorWkSht(pWkshtPathName As String, pVendor As String, pMthYr As String)
       Dim xlx As Object       'Excel object
       Dim xlw As Object       'Workbook
       Dim xls As Object       'Worksheet`
       Dim xlc As Object       'Cell reference
       Dim blnEXCEL As Boolean
    
       blnEXCEL = False
    
       ' Establish an EXCEL application object
       On Error Resume Next
       Set xlx = GetObject(, "Excel.Application")
       If Err.Number <> 0 Then
          Set xlx = CreateObject("Excel.Application")
          blnEXCEL = True
       End If
       Err.Clear
       On Error GoTo 0
    
       ' Change True to False if you do not want the workbook to be
       ' visible when the code is running
       xlx.Visible = False
    
       ' Replace C:\Filename.xls with the actual path and filename
       ' of the EXCEL file into which you will write the data
       Set xlw = xlx.Workbooks.Open(pWkshtPathName)
    
       ' Replace WorksheetName with the actual name of the worksheet in the EXCEL file
       ' (note that the worksheet must already be in the EXCEL file)
       Set xls = xlw.Worksheets(1)
    
       ' Replace A1 with the cell reference into which the first data value is to be written
       Set xlc = xls.Range("A1")   ' this is the first cell into which data will go
    
    
    'This is my code
               '**** You would have your Conditional code here replacing mine ****
       xls.Rows("1").EntireRow.Insert
       xls.Rows("1").EntireRow.Insert
       xls.Range("A1").Select
       xls.Range("A1").FormulaR1C1 = pVendor
       xls.Range("A2").Select
       xls.Range("A2").FormulaR1C1 = pMthYr
       '---end of my code
    
    
      
      
       ' Close the EXCEL file while saving the file, and clean up the EXCEL objects
       Set xlc = Nothing
       Set xls = Nothing
       xlw.Close True   ' close the EXCEL file and save the new data
       DoEvents
       Set xlw = Nothing
       If blnEXCEL = True Then
          xlx.Quit
       End If
       Set xlx = Nothing
    End Sub
    And, of course, EVERY module should have these two lines as the top two lines
    Code:
    Option Compare Database
    Option Explicit


    The code for "EditVendorWkSht" has been adapted from Ken Snell's site http://www.accessmvp.com/KDSnell/EXCEL_Export.htm

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Conditional Formatting in a Access Form
    By adillpickle in forum Forms
    Replies: 3
    Last Post: 01-26-2016, 10:55 AM
  2. excel cells conditional formatting with access vba
    By trevor40 in forum Programming
    Replies: 2
    Last Post: 12-17-2014, 07:31 PM
  3. Access Reports - Conditional Formatting
    By harpreett.singhh@gmail.co in forum Access
    Replies: 3
    Last Post: 08-07-2014, 02:38 PM
  4. Conditional Formatting in Access
    By Triland in forum Access
    Replies: 29
    Last Post: 01-18-2013, 01:00 PM
  5. Access Conditional formatting
    By mailboy in forum Access
    Replies: 1
    Last Post: 12-17-2011, 01:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums