combining two procedures by selected last item in listbox or based on last highlighted cell

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hi again
I want combining two procedures for theses
VBA Code:
 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
         UserForm1.ListBox1.Left = i - 1
        
        Exit For
      End If
    Next

VBA Code:
 For i = ListBox1.ListCount - 1 To 0 Step -1

    Debug.Print i, ListBox1.List(i, 0)
      If ListBox1.List(i, 0) <> "" Then
        ListBox1.ListIndex = i
        Exit For
      End If
    Next i
first code will selected item from listbox based on last highlighted cell by red color for column E

second code will select the last row in listbox

what I need it if there is no red color in last cell then should select the lastrow in listbox but if there is red color for last cell then should select the row based on last cell is highlighted by red .
this is the whole code
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 = "100;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
         UserForm1.ListBox1.Left = i - 1
        
        Exit For
      End If
    Next
  
 
        For i = ListBox1.ListCount - 1 To 0 Step -1

    Debug.Print i, ListBox1.List(i, 0)
      If ListBox1.List(i, 0) <> "" Then
        ListBox1.ListIndex = i
        Exit For
      End If
    Next i

 
End Sub
currently the code just select last row in listbox without take consideration the condition of last highlighted cell by red color in column E
thanks in advance .
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,215,068
Messages
6,122,950
Members
449,095
Latest member
nmaske

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