Hi again,
My apologies for not getting back to you sooner but the Olympics on TV are
playing havoc with my time.
Something you need to realize is that with a combo box displaying 9 rows, if
the existing value is within the last 4 values in the list then the combo
must display 9 rows. It will not display the existing value in the middle and
leave blank rows at the bottom. It will display the last 9 rows of the list
and the previously selected value will be where ever it falls in the
sequence. Conversely with the first rows in the list. it can't display blank
rows at the top. The code has to handle the top rows or an error occurs
trying to set the TopIndex but it does not appear to generate any errors for
the bottom rows so I did not bother trying to do anything special with them.
My previous code was not quite correct and I have had another look at it.
From your last post, I am assuming that you are trying to pick dates from 3
combo boxes for Year, Month and Day. Is my assumption correct? If so then the
following code might help.
Private Sub ComboYear_Enter()
'ListBox RowSource set to Sheet1.Range("A1:A151)
'which is 1900 to 2050
Me.ComboYear.DropDown
If Me.ComboYear.ListIndex - 4 >= 0 Then
Me.ComboYear.TopIndex = _
Me.ComboYear.ListIndex - 4
Else
Me.ComboYear.TopIndex = 0
End If
End Sub
Private Sub ComboMonth_Enter()
'ListBox RowSource set to Sheet1.Range("B1:B12)
'which is January to December
'Because the ComboBox has 9 ListRows,
'if the value is within the last 4 rows
'then it must display the last 9 rows of the list.
Me.ComboMonth.DropDown
If Me.ComboMonth.ListIndex - 4 >= 0 Then
Me.ComboMonth.TopIndex = _
Me.ComboMonth.ListIndex - 4
Else
Me.ComboMonth.TopIndex = 0
End If
End Sub
Private Sub ComboDay_Enter()
'Must select year first to determine if leap year.
If Me.ComboYear.Value >= 1900 And Me.ComboYear <= 2050 Then
Select Case Me.ComboMonth.Value
Case "January", "March", "May", "July", _
"August", "October", "December"
Me.ComboDay.RowSource = _
"Sheet1!C1:C31"
Case "April", "June", "September", _
"November"
Me.ComboDay.RowSource = "Sheet1!C1:C30"
Case "February"
If Me.ComboYear.Value Mod 4 = 0 Then
'Leap year
Me.ComboDay.RowSource = _
"Sheet1!C1:C29"
Else
'Not a leap year
Me.ComboDay.RowSource = _
"Sheet1!C1:C28"
End If
Case Else
'Must select month to determine number of
'days in the dropdown.
MsgBox "Require Month selection first"
Me.ComboMonth.SetFocus
End Select
Me.ComboDay.DropDown
Else
MsgBox "Require Year selection first"
End If
If Me.ComboDay.ListIndex - 4 >= 0 Then
Me.ComboDay.TopIndex = Me.ComboDay.ListIndex - 4
Else
Me.ComboDay.TopIndex = 0
End If
End Sub