abdelfattah
Well-known Member
- Joined
- May 3, 2019
- Messages
- 1,494
- Office Version
- 2019
- 2010
- Platform
- Windows
Hi again
I want combining two procedures for theses
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
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 .
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
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
thanks in advance .