0

I'm trying to apply some conditionals rules using VBA in a Range. But I'm very new with conditional formating VBA so I'm a bit lost.

My Users can add rows above of the target range, that mean the range address could be always different.

let's admit that for the exemple, my range is Worksheets("test").Range("MyBoard") ("MyBoard" is my range name, currently located at A19:O32)

How can I apply a rule to turn yellow each rows of my range if the first column contains the value "Customer" ?

Sub FormatRange()
   Dim MyRange As Range
   Set MyRange = Worksheets("test").Range("MyBoard")

   MyRange.FormatConditions.Delete
   MyRange.FormatConditions.Add Type:=xlCellValue, Formula1:="=COUNTIF(MyRange;"*Customer*") > 0"
   MyRange.FormatConditions(1).Interior.Color = RGB(255, 255, 0)
End Sub

Thanks for the help

3
  • Is "MyBoard" a named ranges? If so, how many column does it have? Then, the formula must count the specific cell value in the range first column... Commented Aug 5, 2022 at 12:32
  • You may need to change your formula, e.g., Type:=xlExpression, Formula1:="=$A1>1", where the $A1 is important to allow the formatting to occur on that specific row. Commented Aug 5, 2022 at 12:32
  • I just edit my post @FaneDuru Commented Aug 5, 2022 at 12:37

3 Answers 3

1

Please, use the next adapted code:

Sub FormatRange()
   Dim MyRange As Range, listSep As String
   Set MyRange = Range("MyBoard")

   listSep = Application.International(xlListSeparator)
   MyRange.FormatConditions.Delete
   MyRange.FormatConditions.Add Type:=xlExpression, formula1:="=ISNUMBER(SEARCH(" & _
                  """Customer""" & listSep & MyRange.cells(1, 1).Address(0, 1) & "))"
   MyRange.FormatConditions(1).Interior.Color = RGB(255, 255, 0)
End Sub
Sign up to request clarification or add additional context in comments.

23 Comments

@Xodarap Didn't you find some time to test the above code? I updated it. In case of Formatcontitions formula, the separator should be the local list separator (according to localization...).
@Xodarap Is it so difficult to test the code and send some feedback?
Hello @FaneDuru I just didn't found the time to test your code last friday. But I just tried it and it's interesting. I have an issue with the formula. I convert it into french and it's working as a "normal rule" in conditional formating option but not by using VBA. The formula keep all "" on it. so nothing happen. I try to find a solution
In french the formula is : MyRange.FormatConditions.Add Type:=xlExpression, formula1:="=ESTNUM(CHERCHE(" & """Customer""" & listSep & MyRange.cells(1, 1).Address(0, 1) & "))" and when I apply this formula I don't get any error but when I check in Conditional formating the formule is : ="ESTNUM(CHERCHE(""Customer"";$A14))"
Ho that's good to know ! Thanks for all the help. I will adapt it for all the other formating I have to do. It should be ok from now
|
1

Conditional formatting has some very particular format to get an entire row to work.

E.g., If i want to apply a color to each row, between certain columns of a specified range:

With .Range(.Cells(1, startColumn), .Cells(lastRow, endColumn))
    .FormatConditions.Add Type:=xlExpression, Formula1:="=$A1>1"
    .FormatConditions(1).Font.Italic = True
End With

Edit1: Indicating use of Find() for the row containing "Customer" being used for the above code.

Sub test()
    With Sheets(1)
        Dim customerCell As Range:  Set customerCell = .Columns(1).Find("Customer")
        If customerCell Is Nothing Then Exit Sub
        Dim lastRow As Long:  lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        .Cells.FormatConditions.Delete
        With .Range(.Cells(customerCell.Row, 1), .Cells(lastRow, 10))
            .FormatConditions.Add Type:=xlExpression, Formula1:="=CountIf($A" & customerCell.Row & ",""*Customer*"")"
            .FormatConditions(1).Interior.Color = RGB(255, 255, 0)
        End With
    End With
End Sub

11 Comments

Ok that's very different of what I expected, In each formula I found they all start at A1 but my range start (currently) at A19 so how can I adapt that to my situation ? I mean it should be dynamic isn't ? today it's A19 but tommorow it could be A40
@Xodarap You can use Range.Find().Row to determine where the first instance of "Customer" occurs, and use that row for the start. Let me give an update to my answer.
@Xodarap just posted update; note that I used columns 1 to 10 in the example code.
Well this solution work ! but only for the row who contains exactly "Customer" in the first column. How can I edit it to apply it also to all row with column "Old Customer" / "New Customer" / "Customer 2021" / "Customer 2022" ??
@Xodarap Formula1:="=CountIf($A" & customerCell.Row & ",""*Customer*"")". I updated the post. You had the CountIf() in yours and it would be the way to do it... note the double quotes, "" is used in this code because you want an actual quotation mark to appear in the string.
|
0

I think, this is what your are looking for:

Sub FormatRange()
   Dim MyRange As Range
   Set MyRange = Worksheets("test").Range("MyBoard")

    Dim startAddress As String
    startAddress = MyRange.Cells(1, 1).Address(False, True) ' will return e.g. $A19 in your case
    
    Dim formula As String
    'formula = startAddress & " = ""customer"""      'exact customer
    formula = "ISNUMBER(FIND(""customer""," & startAddress & "))" ' *customer*
    
    Dim fc As FormatCondition
    With MyRange
        .FormatConditions.Delete
        Set fc = .FormatConditions.Add(xlExpression, Formula1:="=" & formula)
        fc.Interior.Color = RGB(255, 255, 0)
    End With
    
End Sub

You have to reference the first cell within your range - and "fix" the column --> .Address(False, True) will return $A19 in your case.

Then you need to build a valid string for the formula to pass to the format condition

You need double quotes for "customer" when building the string.

6 Comments

Sadly I got an error at the row : Set fc = .FormatConditions.Add(xlExpression, Formula1:="=" & formula) the error is : incorrect call or argument
Made a change to the formula - it is a bit tricky for me to test as I have a German excel - and there you have to put the german version of the formula for format condition
Same story for me with a deprecated french version ...
Will there be users of the sheet with other languages? In French you would need "ESTNUM(TROUVE("customer""," & startAddress & "))" --> en.excel-translator.de/translator helps translating the formulas.
Regarding an international version, look here: stackoverflow.com/q/69220627/16578424
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.