Results 1 to 9 of 9

Thread: Conditional Formatting

  1. #1
    Lively Member
    Join Date
    Jan 12
    Posts
    71

    Conditional Formatting

    Hi

    I want to change the Font Colour on based of some condition e.g. If number is 100% and above then change numbers colour to Green
    If it's above 95% and Less than 100% then Amber
    If it's less than 95% then Red

    And this need to be changed for a range e.g. D120

    I'm not sure how to apply that condition.

    Appreciate your help with sample code.

  2. #2
    Fanatic Member
    Join Date
    Oct 08
    Location
    Midwest Region, United States
    Posts
    1,022

    Re: Conditional Formatting

    You don't need code for this; you can use "conditional formatting."

    What version of Excel do you have?

  3. #3
    Lively Member
    Join Date
    Jan 12
    Posts
    71

    Re: Conditional Formatting

    This needs to be done using MSAccess VBA

  4. #4
    Fanatic Member
    Join Date
    Oct 08
    Location
    Midwest Region, United States
    Posts
    1,022

    Re: Conditional Formatting

    You want to change the fonts in an Excel sheet via Access VBA, is that right?

  5. #5
    Lively Member
    Join Date
    Jan 12
    Posts
    71

    Re: Conditional Formatting

    Yes. I'm creating Excel using Access VBA. After creating the excel I have done some formatting and need to change the colour of selected few ranges e.g. from "C3:C10" if number is 100% and above then change number colour to Green
    If it's above 95% and Less than 100% then Amber
    If it's less than 95% then Red

  6. #6
    Fanatic Member
    Join Date
    Oct 08
    Location
    Midwest Region, United States
    Posts
    1,022

    Re: Conditional Formatting

    Here is some code from Access (2002) that works on my Excel 2010. I only put the "less than 95%" condition in there. When you have more than 1 condition, you need to set a priority order for them.

    Code:
    Private Sub O_Sheet()
        Dim xlsApp As Excel.Application
        Dim xlsBook As Excel.Workbook
        Dim i As Integer
    
        Set xlsBook = Workbooks.Open("C:\Users\myPath\VB\AnyFile.xlsx")
        
        For i = 1 To 2
            Range("a" & i).FormatConditions.Add xlCellValue, Operator:=xlLess, Formula1:="0.95"
            Range("a" & i).FormatConditions(1).Font.Color = -16383844   'red for LT 95%
        Next i
        
        xlsBook.Close
        
        Set xlsBook = Nothing
        
    End Sub

  7. #7
    Lively Member
    Join Date
    Jan 12
    Posts
    71

    Re: Conditional Formatting

    Thanks for the reply.

    I have used below the code but this is not working

    Code:
    Sub setRAGStatus(strRange As String)
        objXLSheet.range(strRange).Select
        objXLSheet.range(strRange).FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=1"
        objXLSheet.range(strRange).FormatConditions(1).Font.Color = -11489280
    
        objXLSheet.range(strRange).FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, Formula1:="=0.95", Formula2:="=1"
        objXLSheet.range(strRange).FormatConditions(1).Font.Color = RGB(247, 153, 75)
    
        objXLSheet.range(strRange).FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="0.95"
        objXLSheet.range(strRange).FormatConditions(1).Font.Color = -16776961
    End Sub
    Value of strRange is S4:T18

    It's not changing the Font Color

  8. #8
    Lively Member
    Join Date
    Jan 12
    Posts
    71

    Re: Conditional Formatting

    I changed the code and that worked only once:

    Code:
    Sub setRAGStatus(strRange As String)
        objXLSheet.range(strRange).Select
        objXLSheet.range(strRange).FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=1"
        objXLSheet.range(strRange).FormatConditions(Selection.FormatConditions.count).SetFirstPriority
        objXLSheet.range(strRange).FormatConditions(1).Font.Color = RGB(0, 255, 0)
        
        objXLSheet.range(strRange).Select
        objXLSheet.range(strRange).FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, Formula1:="=0.95", Formula2:="=1"
        objXLSheet.range(strRange).FormatConditions(Selection.FormatConditions.count).SetFirstPriority
        objXLSheet.range(strRange).FormatConditions(1).Font.Color = RGB(255, 153, 0)
        
        objXLSheet.range(strRange).Select
        objXLSheet.range(strRange).FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=0.95"
        objXLSheet.range(strRange).FormatConditions(Selection.FormatConditions.count).SetFirstPriority
        objXLSheet.range(strRange).FormatConditions(1).Font.Color = RGB(255, 0, 0)
    
    End Sub
    This code worked only once but now it's not working quite wiered

    I'm getting error below error
    "Run time error 91"
    "Object variable or with block variable not set"

    Above error I'm getting at below line
    Code:
    objXLSheet.range(strRange).FormatConditions(Selection.FormatConditions.count).SetFirstPriority
    Highly appreciate your help.

  9. #9
    Fanatic Member
    Join Date
    Oct 08
    Location
    Midwest Region, United States
    Posts
    1,022

    Re: Conditional Formatting

    Not sure why it's not working for you. Here is my version:

    Code:
    Private Sub O_Sheet()
        Dim xlsBook As Excel.Workbook
        Dim i As Integer
    
        Set xlsBook = Workbooks.Open("C:\YourPath\YourFile.xlsx")
        
        For i = 1 To 2
            Range("a" & i).FormatConditions.Add xlCellValue, Operator:=xlEqual, Formula1:="1"
            Range("a" & i).FormatConditions(1).Font.Color = -16383844   'green for = 1
            Range("a" & i).FormatConditions(1).Font.Bold = False
            Range("a" & i).FormatConditions(1).Font.Italic = True
        Next i
        
        For i = 1 To 2
            Range("a" & i).FormatConditions.Add xlCellValue, Operator:=xlBetween, Formula1:="=0.95", Formula2:="=1"
            Range("a" & i).FormatConditions(1).Font.Color = -16383844   'red for LT 95%
            Range("a" & i).FormatConditions(1).Font.Bold = True
        Next i
        
        xlsBook.Save
        xlsBook.Close
        
        Set xlsBook = Nothing
        
        MsgBox "done"
    
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •