doesn't populate all of dates in listbox based on condition in textbox

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hello
I try to populate all of dates whether equal today or bigger than today in listbox based on textbox 1
so far just show date today
forMat date should be MM/DD/YYYY
this is the part of my code .
VBA Code:
a = Len(Me.TextBox1.Text)
 
 If Left(Sheet1.Cells(ss, "d").Value, a) = Left(Me.TextBox1.Text, a) Then
 If a < Date Or a = Date Then
Me.ListBox1.AddItem Sheet1.Cells(ss, 1)
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = Sheet1.Cells(ss, "e")
Me.ListBox1.List(ListBox1.ListCount - 1, 2) = Sheet1.Cells(ss, "d")
Me.ListBox1.List(ListBox1.ListCount - 1, 3) = Sheet1.Cells(ss, "c")
any idea to fix it?
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
VBA Code:
If a < Date Or a = Date Then
    Me.ListBox1.AddItem Sheet1.Cells(ss, 1)
    for i = 5 to 7
             Me.ListBox1.AddItem Sheet1.Cells(ss, i)
    next i
end if
 
Upvote 0
thanks but it doesn't work . it will show all of data without any exception.
 
Upvote 0
Hi,
you are not sharing all the code so this is a bit of a guess but see if helps

VBA Code:
Private Sub abdelfattah()
    Dim a           As Date
    Dim arr         As Variant
    Dim c           As Long, r As Long
    
    With Me.TextBox1
        If Not IsDate(.Text) Then Exit Sub Else a = DateValue(.Value)
    End With
    
    'intialize array
    '(assumes contiguous data starting on row 1 & sheet not protected)
    arr = Sheet1.Range("A1").CurrentRegion.Value

    With Me.ListBox1
        .Clear
        'loop array
        For r = 2 To UBound(arr, xlRows)
            If arr(r, 5) <= a Then
                .AddItem arr(r, 1)
                For c = 1 To 3
                    .List(.ListCount - 1, c) = arr(r, Choose(c, 5, 4, 3))
                Next c
            End If
        Next r
    End With
    
End Sub

Dave
 
Upvote 0
Hi Dave
doesn't work for me (it shows all of data without exception )

code
VBA Code:
Private Sub CommandButton2_Click()
        With Me.ListBox1
            .ColumnCount = 5
            .ColumnWidths = "100;100;100;100;100"
        End With
Dim ls As Integer
Dim ss As Integer
 ls = Sheet1.Range("d10000").End(xlUp).Row
 Me.ListBox1.Clear
 For ss = 2 To ls
 a = Len(Me.TextBox1.Text)
 If a <= Date Then
 If Left(Sheet1.Cells(ss, "d").Value, a) = Left(Me.TextBox1.Text, a) Then
Me.ListBox1.AddItem Sheet1.Cells(ss, 1)
Me.ListBox1.List(ListBox1.ListCount - 1, 1) = Sheet1.Cells(ss, "e")
Me.ListBox1.List(ListBox1.ListCount - 1, 2) = Sheet1.Cells(ss, "d")
Me.ListBox1.List(ListBox1.ListCount - 1, 3) = Sheet1.Cells(ss, "c")
Me.ListBox1.List(ListBox1.ListCount - 1, 4) = Sheet1.Cells(ss, "b")
Me.ListBox1.List(ListBox1.ListCount - 1, 5) = Sheet1.Cells(ss, "a")
 End If
 End If
 Next ss
End Sub
data in sheet

date.xlsm
ABCDE
1customerinvoice noinvoice numberdatephone no
2al112513252/18/201612334
3al212612152/19/201612335
4al31272362/20/201612336
5al412855662/11/201612337
6al512952362/20/201612338
7al613022542/19/201612339
8al7131253210/12/202212340
9al813212525/18/202212341
10al913312568/9/202212342
11al1013452310/31/202212343
12al1113526451/22/202312344
13al12136125610/12/202212345
14al1313756327/7/202312346
15al141381549/28/202312347
16al1513955210/12/202212348
17al1614056983/12/202412349
Sheet1
 
Upvote 0
try this correction to my code & see if resolves

VBA Code:
Private Sub CommandButton2_Click()
    Dim a           As Date
    Dim arr         As Variant
    Dim c           As Long, r As Long
    
    With Me.TextBox1
        If Not IsDate(.Text) Then Exit Sub Else a = DateValue(.Value)
    End With
    
    'intialize array
    '(assumes contiguous data starting on row 1 & sheet not protected)
    arr = Sheet1.Range("A1").CurrentRegion.Value

    With Me.ListBox1
        .Clear
        'loop array
        For r = 2 To UBound(arr, xlRows)
            If arr(r, 4) <= a Then
                .AddItem arr(r, 1)
                For c = 1 To 4
                    .List(.ListCount - 1, c) = arr(r, Choose(c, 5, 4, 3, 2))
                Next c
            End If
        Next r
    End With
    
End Sub

Private Sub UserForm_Initialize()
        With Me.ListBox1
            .ColumnCount = 5
            .ColumnWidths = "100;100;100;100;100"
        End With
End Sub

Dave
 
Upvote 0
Solution

Forum statistics

Threads
1,214,875
Messages
6,122,037
Members
449,062
Latest member
mike575

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