VBA Excel Userform Listbox Initial Selection Based on Cell Value With Color

DMumme

New Member
Joined
Jun 14, 2014
Messages
23
I have a Userform with several items including a Listbox entitled LBNewCase. The ListBox is populated (RowSource) through a range of cells. There is a cell that indicates the previous choice from this list.This cell (LastCaseMatch) uses the match function to find the position of the previous selection within the list used as the RowSource. When the ListBox is initialized I would like the list to appear with the last choice pre-selected/highlighted and the BackColor to be a light red so that the user will not have to change the selection if it to be the same for the current input but with the BackColor is light red to indicate that no change has been made (another item selected from the list) and that they should at least be aware of looking at this item.

If a change is made I would like to change the BackColor to a light yellow which would indicate to the user that they did make a change.

I have included an counter to track the number of changes being made before changing the BackColor to light yellow because the Private Sub UserForm_Initialize() calls the Private Sub LBNewCase_Change() multiple times and I do not wish the BackColor to be changed to light yellow thru the Private Sub LBNewCase_Change() during the initialization.

In the Userform:
Code:
Private Sub UserForm_Initialize()
    
      Sheets("Library").Range("NewCaseCounter") = 0 

'Set RowSource Locations for the Userform Listboxes
      CasesList = Sheets("Library").Range("CasesList").Value
      LBNewCase.RowSource = CasesList
    
'Set Userform Boxes That Bring in the Last Asset's Data to Light Red
      LBNewCase.BackColor = RGB(255, 115, 115) 'light red
      
'Set Userform Initial Values
          NewCaseMatch = Sheets("Library").Range("LastCaseMatch") - 1
          Sheets("Library").Range("LastCaseMatchB") = NewCaseMatch
      LBNewCase.Selected(NewCaseMatch) = True

 End Sub

Private Sub LBNewCase_Change()
    NewCaseCounter = Sheets("Library").Range("NewCaseCounter")
    NewCaseCounter = NewCaseCounter + 1
    Sheets("Library").Range("NewCaseCounter") = NewCaseCounter
    NewCaseMatch = Sheets("Library").Range("LastCaseMatchB")
    If NewCaseCounter > 2 Then
        LBNewCase.BackColor = RGB(255, 255, 205) 'light yellow
        NewCaseMatchB = Sheets("Library").Range("LastCaseMatchB")
    End If
    LBNewCase.Selected(NewCaseMatch) = True
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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