Populate data in ListBox1 for valid year

haseft

Active Member
Joined
Jun 10, 2014
Messages
321
Hi,
In Sheet1 there are some columns. 2 of the columns (Agreement_begin in column Z and Agreement_end in column AA) are formatted with date “yyyy-mm-dd”.
In UserForm1 there is a ComboBox2 with populated with year, for example 2016, 2017, 2018, a.s,o and a CommandButton1.
I want to populate data in ListBox1 (which has 5 columns), when I choose a year from ComboBox2 and press the CommandButton1.
The below codes doesn’t work.
Help with this please.

Agreement_begin
(column Z)
Agreement_end
(column AA)
Period (this table is only for visualizing)
2015-01-012016-12-31->20152016
2015-01-012018-12-31->2015201620172018
2017-01-012019-12-31->201720182019
2017-01-012020-12-31->2017201820192020
2018-01-012020-12-31->201820192020
2018-07-012019-06-30->20182019
2018-07-012022-06-30->20182019202020212022
2019-01-012019-12-31->2019
2019-01-012022-12-31->2019202020212022
2020-01-012021-12-31->20202021

The result av valid agreement in ListBox1 (for year choosen from ComboBox2) should be as below.
For example if I choose year 2018 then ListBox1 must show only 6 row of data (se bold rows above).

20152 row of data
20162 row of data
20173 row of data
20186 row of data
20197 row of data
20205 row of data
20213 row of data
20222 row of data

VBA Code:
Private Sub CommandButton1_Click()
   Dim i As Long
   Set sht = ThisWorkbook.Worksheets("Sheet1")
   LastRow = sht.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
     
   Me.ListBox1.Clear
   TextBox4.Value = ""
   ListBox1.Value = ""
 
   With ListBox1
     .ColumnCount = 5
     .ColumnWidths = "190;70;70;50;50"
        For i = 2 To LastRow
           If sht.Range("Z" & i) >= CDate(ComboBox2.Value) And sht.Range("AA" & i) <= CDate(ComboBox2.Value) Then
             .AddItem Range("B" & i).Value
             .List(.ListCount - 1, 1) = sht.Range("Z" & i).Value
             .List(.ListCount - 1, 2) = sht.Range("AA" & i).Value
             .List(.ListCount - 1, 3) = sht.Range("U" & i).Value
             .List(.ListCount - 1, 4) = "$B$" & i
           End If
        Next i
   End With
   
End Sub
 
Last edited:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
In the If line shouldn't you be comparing the combobox to the cells instead of the cells to the combobox ?
 
Upvote 0
If you only have the year in combobox2, then you must compare the year of the date.
Also the signs >= <= were inverted.
Try this:

VBA Code:
Private Sub CommandButton1_Click()
   Dim i As Long, sht As Worksheet, LastRow As Long
   
   Set sht = ThisWorkbook.Worksheets("Sheet1")
   LastRow = sht.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
     
   Me.ListBox1.Clear
   TextBox4.Value = ""
   ListBox1.Value = ""
 
   With ListBox1
     .ColumnCount = 5
     .ColumnWidths = "190;70;70;50;50"
        For i = 2 To LastRow
           If Year(sht.Range("Z" & i)) <= Val(ComboBox2.Value) And _
              Year(sht.Range("AA" & i)) >= Val(ComboBox2.Value) Then
             .AddItem Range("B" & i).Value
             .List(.ListCount - 1, 1) = sht.Range("Z" & i).Value
             .List(.ListCount - 1, 2) = sht.Range("AA" & i).Value
             .List(.ListCount - 1, 3) = sht.Range("U" & i).Value
             .List(.ListCount - 1, 4) = "$B$" & i
           End If
        Next i
   End With
End Sub
 
Upvote 0
I tried that too, no result.
Line if is wrong.

Please comment:
Did you put the complete code?
Did you modify something in the code that I sent?
Does it send you an error message?
In columns Z and A do you have dates or are they texts?
 
Upvote 0
thanks DanteAmor,
its working, I am still checkning the result.
one moe issue:
the value in column 4 in ListBox1 is nummer . I want to get 1000-separator, for example nummer 530000 shuould show 530 000, how?
VBA Code:
.List(.ListCount - 1, 3) = sht.Range("U" & i).Value
 
Upvote 0
1000-separator, for example nummer 530000 shuould show 530 000, how
Try this:

VBA Code:
.List(.ListCount - 1, 3) = Format(sht.Range("U" & i).Value, "000 000")
 
Upvote 0
Solution

Forum statistics

Threads
1,214,952
Messages
6,122,458
Members
449,085
Latest member
ExcelError

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