Consulting

Results 1 to 12 of 12

Thread: Listview specific rows conditional formatting

  1. #1

    Listview specific rows conditional formatting

    Dear friends,

    Is it possible to format rows in listview and sheet according to status "Etat" (column 47 in my 3 sheets) value: I need a conditional formatting like this:
    1- When Column 47 value is "clôturé" ,I want the whole row in my listview becomes red.
    2- When Column 47 value is "en cours" ,I want the whole row in my listview becomes green.

    Is this approach can be done?

    Waiting for your reply.Thanks in advance,
    Attached Files Attached Files

  2. #2
    Sub SetSpecialFormatCondition()
        Dim WS As Worksheet
        Dim RangeOfCells As Range
        Dim S As String, FormulaStrRed As String, FormulaStrGreen As String
        Dim FC As FormatCondition
    
    
        For Each WS In ThisWorkbook.Worksheets
            S = Trim(WS.Range("AU1").Value)
            If S = "Etat" Then
                FormulaStrRed = "=$AU2=" & """" & "clôturé" & """"    '=$AU2="clôturé"
                FormulaStrGreen = "=$AU2=" & """" & "en cours" & """"    '=$AU2="en cours"
    
    
                Set RangeOfCells = WS.Range("A2:BH" & WS.Range("AU" & WS.Rows.Count).End(xlUp).Row)
                With RangeOfCells
                    For Each FC In .FormatConditions
                        If FC.Type = xlExpression And (FC.Formula1 = FormulaStrRed Or FC.Formula1 = FormulaStrGreen) Then
                            FC.Delete
                        End If
                    Next FC
                    
                    Set FC = .FormatConditions.Add(Type:=xlExpression, Formula1:=FormulaStrGreen)
                    With FC
                        .SetFirstPriority
                        With .Font
                            .ThemeColor = xlThemeColorDark1
                            .ThemeFont = xlThemeFontNone
                        End With
                        With .Interior
                            .PatternColorIndex = xlAutomatic
                            .Color = 5287936
                        End With
                        .StopIfTrue = True
                    End With
                    
                    Set FC = .FormatConditions.Add(Type:=xlExpression, Formula1:=FormulaStrRed)
                    With FC
                        .SetFirstPriority
                        With .Font
                            .ThemeColor = xlThemeColorDark1
                            .ThemeFont = xlThemeFontNone
                        End With
                        With .Interior
                            .PatternColorIndex = xlAutomatic
                            .Color = 255
                        End With
                        .StopIfTrue = True
                    End With
                End With
            End If
        Next WS
    End Sub

  3. #3
    Dear rlv,
    Thank you for your prompt reply.It is working for all sheets ,but I want it to work on my listviw.Is it possible?.

    As for sheets ,I want font colour only to be changed not interior.

    Waiting for your kind reply

  4. #4
    Quote Originally Posted by djemy1975 View Post
    Dear rlv,
    Thank you for your prompt reply.It is working for all sheets ,but I want it to work on my listviw.Is it possible?.
    I cannot say because I don't know what you mean by "my listviw"

    Quote Originally Posted by djemy1975 View Post
    As for sheets ,I want font colour only to be changed not interior.
    In the code I posted, remove or comment-out the lines that set the background (interior) color.

  5. #5
    I have a userform used to filter records in listview1 .I want to apply conditional formatting on that listview (Make the row in red or green according to value in"Etat" column ("clôturé" or "en cours") .Herewith a screenshotlistview.jpg
    Last edited by djemy1975; 11-19-2018 at 03:22 AM.

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Googling "excel vba listview row color" finds a number of posts that you can use

    Including one here on VBAexpress --

    http://www.vbaexpress.com/kb/getarticle.php?kb_id=916
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    Thank you for reply .I tried to use it before and spent much time ,but I could not adapt it as my listview pulls data from three sheets.Could you help me adapt it.
    Best regards,

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Multiple sheet should not make a difference

    As you Add the data to your ListView check and change color as needed

    If you make a very simple test workbook, we can look at that
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    ok I will attach a simple workbook while trying to figure out a solution to the workbook I am working on.I will be back tonight when logged in with my pc.
    thanks

  10. #10
    Quote Originally Posted by Paul_Hossler View Post
    Multiple sheet should not make a difference

    As you Add the data to your ListView check and change color as needed

    If you make a very simple test workbook, we can look at that
    I think my code would be something like that:
    Private Sub CondFormat()'Conditional formatting
    On Error Resume Next
    For i = 0 To ListView1.ListItems.Count - 1
         
     
           
         If ListView1.ListItems(i).ListSubItems(48).Text = "clôturé" Then
            ListView1.ListItems(i).ListSubItems(48).ForeColor = vbRed
         End If
         If ListView1.ListItems(i).ListSubItems(48).Text = "en cours" Then
            ListView1.ListItems(i).ListSubItems(48).ForeColor = vbGreen
         End If
         If ListView1.ListItems(i).ListSubItems(48).Text = "a clôturer" Then
            ListView1.ListItems(i).ListSubItems(48).ForeColor = vbBlue
           
         End If
                       
        Next i
    End Sub
    This function should be called from search button "Private Sub CommandButton1_Click()" in my userform.
    Is it possible to adapt this code
    Attached Files Attached Files

  11. #11
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Capture.JPG

    Private Sub CondFormat()
        Dim i As Long, i1 As Long
        For i = 1 To ListView1.ListItems.Count
            Select Case Trim(ListView1.ListItems(i).ListSubItems(47).Text)
                Case "cl?tur?"
                    For i1 = 1 To ListView1.ListItems(i).ListSubItems.Count
                        ListView1.ListItems(i).ListSubItems(i1).ForeColor = vbRed
                    Next i1
                Case "en cours"
                    For i1 = 1 To ListView1.ListItems(i).ListSubItems.Count
                        ListView1.ListItems(i).ListSubItems(i1).ForeColor = vbGreen
                    Next i1
                Case "a cl?turer"
                    For i1 = 1 To ListView1.ListItems(i).ListSubItems.Count
                        ListView1.ListItems(i).ListSubItems(i1).ForeColor = vbBlue
                    Next i1
            End Select
        Next i
    End Sub
    In case some of the characters did not show

    Capture1.JPG
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  12. #12
    Quote Originally Posted by Paul_Hossler View Post
    Capture.JPG

    Private Sub CondFormat()
        Dim i As Long, i1 As Long
        For i = 1 To ListView1.ListItems.Count
            Select Case Trim(ListView1.ListItems(i).ListSubItems(47).Text)
                Case "cl?tur?"
                    For i1 = 1 To ListView1.ListItems(i).ListSubItems.Count
                        ListView1.ListItems(i).ListSubItems(i1).ForeColor = vbRed
                    Next i1
                Case "en cours"
                    For i1 = 1 To ListView1.ListItems(i).ListSubItems.Count
                        ListView1.ListItems(i).ListSubItems(i1).ForeColor = vbGreen
                    Next i1
                Case "a cl?turer"
                    For i1 = 1 To ListView1.ListItems(i).ListSubItems.Count
                        ListView1.ListItems(i).ListSubItems(i1).ForeColor = vbBlue
                    Next i1
            End Select
        Next i
    End Sub
    In case some of the characters did not show

    Capture1.JPG

    Thank you for your reply.It is fantastic Mr Paul.

    Highly appreciated,

Posting Permissions

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