list box fill with each item color

Waqas ali

Board Regular
Joined
Nov 6, 2010
Messages
163
I have items in list box. I want to display with each item with its own color
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Code:
Option Explicit

Private Sub UserForm_Initialize()
    'Copied from http://www.vbaexpress.com/forum/showthread.php?9152-Change-the-color-of-text-in-a-listbox-based-upon-a-certain-criteria
    ' because links sometimes get broken
    
    'Using 'Microsoft Listview Control 6.0'
    '  more at https://msdn.microsoft.com/en-us/library/ms172636(v=vs.90).aspx
    '  but that info seems to be for a later version
    
    'Fill A2:Dxx with numbers, if col B number is <1 then the column A number in
    '  the listview box will be red
    
    Dim startrow As Integer     'beginning of data
    Dim endrow As Integer       'end of data
    Dim pos As Integer          'actual row
    Dim lv_item As Integer      'no of the listview item
    Dim counting As Integer     'loop for processing all items
    Dim sSheetName As String    'Sheet that contains the ListView control
    
    sSheetName = "Sheet3"
    
    startrow = 2
    endrow = Worksheets(sSheetName).Range("B" & Rows.Count).End(xlUp).Row

    pos = 2
    lv_item = 1
    With ListView1
         'gives me headers at the top
        .View = lvwReport
         'defining the columnheaders
        With .ColumnHeaders
            .Clear
            .Add , , "Column 1", 60
            .Add , , "Column 2", 60
            .Add , , "Column 3", 60
            .Add , , "Column 4", 60
        End With
        .HideColumnHeaders = False
        .Appearance = cc3D
        .FullRowSelect = True
        For counting = startrow To endrow
            If Worksheets(sSheetName).Range("B" & pos).Value > 1 Then
                .ListItems.Add , , Worksheets(sSheetName).Range("A" & pos)
                .ListItems(lv_item).ForeColor = RGB(255, 0, 0)
                .ListItems(lv_item).ListSubItems.Add , , Worksheets(sSheetName).Range("B" & pos)
                .ListItems(lv_item).ListSubItems.Add , , Worksheets(sSheetName).Range("C" & pos)
                .ListItems(lv_item).ListSubItems.Add , , Worksheets(sSheetName).Range("D" & pos)
                .ListItems(lv_item).Bold = True
            Else
                .ListItems.Add , , Worksheets(sSheetName).Range("A" & pos)
                .ListItems(lv_item).ForeColor = RGB(0, 0, 0)
                .ListItems(lv_item).ListSubItems.Add , , Worksheets(sSheetName).Range("B" & pos)
                .ListItems(lv_item).ListSubItems.Add , , Worksheets(sSheetName).Range("C" & pos)
                .ListItems(lv_item).ListSubItems.Add , , Worksheets(sSheetName).Range("D" & pos)
             End If
           lv_item = lv_item + 1
            pos = pos + 1
        Next counting
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,375
Members
448,955
Latest member
BatCoder

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