new procedure to select item in listbox based on last highlighted cell

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,507
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hello

in this part of my project will call this code from UserForm_Initialize() , also I have combobox1,2 also call this code from them

what I need it when populate data based on selected item from comboboxes . then should select item in list box automatically based on last highlighted cell by red color (there are many highlighted cells by red color, just I want select row in listbox automatically based on last cell is highlighted in column E)

every time will change last highlighted cell in location .

note: I want showing this selected item by moving above if the selected item is not visible(meaning I have to use scroll bar up to down to see it, I don't want it. I want automatically without interfere from me. this note is option if it's impossible just ignore it(I will post new thread to solve this note)


VBA Code:
Private Sub LBoxPop()
    Dim r          As Long, c As Long
    Dim Data()     As Variant
    Dim rng        As Range
 
    Set rng = ws.Cells(1, 1).CurrentRegion
    ReDim Data(1 To rng.Rows.Count, 1 To rng.Columns.Count)
 
    For r = 1 To UBound(Data, xlRows)
        For c = 1 To UBound(Data, xlColumns)
            Data(r, c) = rng.Cells(r, c).Text
        Next c
    Next r
 
    With UserForm1.ListBox1
        .ColumnCount = 5
        .columnWidths = "80;240;120;120;120"
        .List = Data
    End With
 
End Sub
if anybody need the whole codes I will post , just inform me .
thanks
 
Last edited:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Like this?
VBA Code:
Private Sub LBoxPop()
    Dim r          As Long, c As Long
    Dim Data()     As Variant
    Dim rng        As Range
    
    Set rng = ws.Cells(1, 1).CurrentRegion
    ReDim Data(1 To rng.Rows.Count, 1 To rng.Columns.Count + 1)
 
    For r = 1 To UBound(Data, xlRows)
        For c = 1 To UBound(Data, xlColumns)
            Data(r, c) = rng.Cells(r, c).Text
        Next c
        Data(r, 6) = rng.Cells(r, 5).Interior.Color
    Next r
 
    With UserForm1.ListBox1
        .ColumnCount = 5
        .ColumnWidths = "80;240;120;120;120"
        .List = Data
    End With
    
    For i = UBound(Data, xlRows) To 1 Step -1
      If Data(i, 6) = 255 Then
        UserForm1.ListBox1.Selected(i - 1) = True
         UserForm1.ListBox1.TopIndex = i - 1
        Exit For
      End If
    Next
 
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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