PDA

View Full Version : Solved: Lect combobox item, display wksheet name in listbox



AlvinChaand
09-19-2009, 01:30 PM
i have 2 comboboxes,
comboYEAR to hold the years from 2005 to current year
comboMonth to hold the 12 months jan thru dec
a listbox to display results named -displaylist
All my worksheets are named in this fashion - dd-mmm-yyyy (eg. 23-jan-2008)

my user is allowed to select from both combos
When he selects the year combo, all the sheets the particular yyyy is displayed in the listbox
when he selects the month combo, the search is refined and only that particular month in the year is displayed
when he clicks the items displayed in the listbox,the particular worksheet opens up inthe background.

PROBLEM:
the year combo works fine but my month combo is not working well...
when I select a month in the combobox, instead of displayong sheetnames with just the selected months, all the sheets are displayed :(

can anyone please advise?
Any help at all will be greatly appreciated



here are my codes:



Code:



Option Explicit

Private Sub ComboMONTH_Change()
Dim ws As Worksheet

****With ComboMONTH
*******If .ListIndex <> -1 Then
************DisplayList.Clear
************For Each ws In Worksheets
****************If IsDate(ws.Name) Then
****************If IsDate(ComboMONTH.Value) = Val(.Value) Then
************************DisplayList.AddItem ws.Name
********************End If
****************End If
************Next ws
********End If
****End With


End Sub

Private Sub ComboYEAR_Change()

Dim ws As Worksheet

****With ComboYEAR
********If .ListIndex <> -1 Then
************DisplayList.Clear
************For Each ws In Worksheets
****************If IsDate(ws.Name) Then
********************If Year(DateValue(ws.Name)) = Val(.Value) Then
************************DisplayList.AddItem ws.Name
********************End If
****************End If
************Next ws
********End If
****End With

End Sub

Private Sub DisplayList_Click()
Dim ws As Worksheet, wb As Workbook

Set wb = ActiveWorkbook

For Each ws In wb.Worksheets
ComboYEAR.AddItem ws.Name
Next
*'When Search button is clicked'
*Dim I As Integer, sht As String
**For I = 0 To DisplayList.ListCount - 1 'to go thru all the worksheets in the workbook starting with the first and adding the name of the worksheet to the listbox'
***If DisplayList.Selected(I) = True Then 'if the particular worksheetname is selected, the activate/open the requested worksheet'
*****sht = DisplayList.List(I)
*******End If
****Next I
***Sheets(sht).Activate 'open the requested worksheet'
***Unload Me
****SEARCH_DATE.Show
****
**End

End Sub


Private Sub UserForm_Activate()

Dim I As Long
Dim lMonth As Long

****For lMonth = 1 To 12
*******ComboMONTH.AddItem Format(DateSerial(2000, lMonth, 1), "MMM")
**Next lMonth
****
****For I = 2005 To Year(Date)
********ComboYEAR.AddItem I
****Next I
End Sub

hyzhaozhong
09-20-2009, 06:49 AM
IsDate(ComboMONTH.Value) = Val(.Value)

It's always TRUE.So all sheets display.

IsDate(ComboMONTH.Value) Is boolean type,value is false because the combomonth is not date format.

Val(.Value)Is 0,because combomonth.value is string format.

hyzhaozhong
09-20-2009, 07:10 AM
If Format(DateSerial(2000, Month(DateValue(ws.Name)), 1), "MMM") = .Value Then

I have run and it's OK.