ListBox Add Item based on criteria

G

Guest

I have a list box that is populated by the code below. I am having trouble
just showing todays entries. Column G is a time stamp of the entries made
and I would like the listbox to just show today's and not all the entries in
the excel database.


If Workbooks(xlDatabaseName).Sheets(xlTable).Range("A2") <> "" Then
With Workbooks(xlDatabaseName).Sheets(xlTable)
Set Rng = .Range("A1", .Range("A1").End(xlDown))
Set co = cell.Offset
End With

For Each cell In Rng.Cells
With UserForm1.ListBox1
.AddItem cell.Value
.List(.ListCount - 1, 1) = cell.Offset(0, 1).Value
.List(.ListCount - 1, 2) = cell.Offset(0, 2).Value
.List(.ListCount - 1, 3) = cell.Offset(0, 3).Value
.List(.ListCount - 1, 4) = cell.Offset(0, 4).Value
.List(.ListCount - 1, 5) = cell.Offset(0, 5).Value
.List(.ListCount - 1, 6) = cell.Offset(0, 6).Value

If .ListCount > 5 Then
.TopIndex = .ListCount - 5
End If
If CheckBox1.Value = True Then
.ListIndex = .ListCount - 1
End If
End With
Next cell
End If

Thanks in advance
 
T

Tom Ogilvy

If Workbooks(xlDatabaseName).Sheets(xlTable).Range("A2") <> "" Then
With Workbooks(xlDatabaseName).Sheets(xlTable)
Set Rng = .Range("A1", .Range("A1").End(xlDown))
Set co = cell.Offset
End With

For Each cell In Rng.Cells
With UserForm1.ListBox1
if clng(cell.offset(0,6).Value) = clng(date) then
.AddItem cell.Value
.List(.ListCount - 1, 1) = cell.Offset(0, 1).Value
.List(.ListCount - 1, 2) = cell.Offset(0, 2).Value
.List(.ListCount - 1, 3) = cell.Offset(0, 3).Value
.List(.ListCount - 1, 4) = cell.Offset(0, 4).Value
.List(.ListCount - 1, 5) = cell.Offset(0, 5).Value
.List(.ListCount - 1, 6) = cell.Offset(0, 6).Value

If .ListCount > 5 Then
.TopIndex = .ListCount - 5
End If
If CheckBox1.Value = True Then
.ListIndex = .ListCount - 1
End If
End if
End With
Next cell
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top