Change color of item in listview based on certain text

Yweiss0

New Member
Joined
Aug 3, 2013
Messages
46
Hi everyone,
I have a listview in user form contains data from a spreadsheet.
I want to loop through all items (all of them including the subitems) and change the color of the item if the item contains a text.
For example if the item (or the sub item) contains + the item should become green, if it contains * it should be blue

Thanks for the help
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hello Yweiss0,
I hope so this piece of code will help you to understand how you can do it.
VBA Code:
Private Sub UserForm_Initialize()

    With ListView1
        .View = lvwReport
        .ColumnHeaders.Add , , "Header1"
        .ColumnHeaders.Add , , "Header2"
        .ColumnHeaders.Add , , "Header3"
        .ListItems.Add , , "Item1*"
        .ListItems.Add , , "Item2+"
        .ListItems.Add , , "Item3*"
        .ListItems(1).SubItems(1) = "a*"
        .ListItems(1).SubItems(2) = "B++"
        .ListItems(2).SubItems(1) = "A"
        .ListItems(2).SubItems(2) = "C"
        .ListItems(3).SubItems(1) = "D"
        .ListItems(3).SubItems(2) = "***"
        
        For vN1 = 1 To .ListItems.Count
           If InStr(1, .ListItems(vN1), "+") > 0 Then _
                .ListItems(vN1).ForeColor = RGB(0, 127, 0)
           If InStr(1, .ListItems(vN1), "*") > 0 Then _
                .ListItems(vN1).ForeColor = RGB(0, 0, 255)
        Next vN1
        For vN1 = 1 To .ListItems.Count
            For vN2 = 1 To .ColumnHeaders.Count - 1
                If InStr(1, .ListItems(vN1).ListSubItems(vN2), "+") > 0 Then _
                     .ListItems(vN1).ListSubItems(vN2).ForeColor = RGB(0, 127, 0)
                If InStr(1, .ListItems(vN1).SubItems(vN2), "*") > 0 Then _
                     .ListItems(vN1).ListSubItems(vN2).ForeColor = RGB(0, 0, 255)
            Next vN2
        Next vN1
    End With
    
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top