Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

search for data within month

0

good day guys !

I have  userform  search  for  data for  specific  sheet based on combobox 1  and  textbox1  will  search based on column D within selectd sheet from combobox1.  

now  I add combobox2 should link  with combobox1 by select  the  month  

first  the  userform  will show data   in listbox when run  the userform  is  ok

second  will  select  sheet  from combobox1 and  populate  data in listbox is ok

third   populate  data  in listbox when fill textbox based on column D  to  search  within selected  sheet from combobox1

what  I  want to  connect  combobox2 with listbox1  and combobox1 

it  when select just combobox2 then will populate in listbox based on month individually , and  when select combobox1,2  then will populate data in listbox  based on month  and within selected sheet from combobox 1

I hope  to  anybody has  time  to  look for  my  file .

Answer
Discuss

Answers

0
Selected Answer

Hi Mussa

REVISION 30 Jan 2023

You will have more work to do but here's a start...

In the attached revised file, I modified these procedures (changes in bold).

1. to allow selection of sheets by ComboBox1:

Private Sub UserForm_Initialize()
    Dim lindex&
    Dim rngDB As Range, rng As Range
    Dim i, myFormat(1) As String
    Dim sWidth As String
    Dim vR() As Variant
    Dim n As Integer
    Dim myMax As Single
    
    Dim SH As Worksheet
    ' populate ComboBox1 list eith sheet names
    For Each SH In ThisWorkbook.Worksheets
        ComboBox1.AddItem SH.Name
    Next SH

<< existing code>>

2. To match ComboBox2 selections on UserForm activation:

Private Sub UserForm_Activate()
Dim i As Long, dic As Object
  Dim SH As Worksheet
  Set SH = sheet1
  Set dic = CreateObject("Scripting.Dictionary")

  For i = 2 To SH.Range("A" & Rows.Count).End(xlUp).Row
    dic(Format(CDate(SH.Range("A" & i).Value), "mmm-yyyy")) = Empty
  Next
  ComboBox2.List = dic.Keys
  ' set comboBox1 to the matching value
  ComboBox1.Value = SH.Name
End Sub

3. To populate ComboBox2 based on ComboBox1 selection:

Private Sub ComboBox1_Change()
  With ComboBox1
    ' combined tests for Exit
    If .Value = "" Or .ListIndex = -1 Then Exit Sub
    
    ListBox1.RowSource = ""
    ListBox1.Clear
    ListBox1.RowSource = "'" & Sheets(.Value).Name & "'!" & Sheets(.Value).Range("A1", Sheets(.Value).Range("H" & Rows.Count).End(3)).Address
    TextBox1 = "" 'added- clear search box and CB2
    ComboBox2.Value = ""
  End With
  ' rewrite array
  Arr = ListBox1.List
  
  ' get ready to populate CB2
  Dim SH As Worksheet, dic As Object, i As Long
  Set SH = Worksheets(ComboBox1.Value)
  Set dic = CreateObject("Scripting.Dictionary")
  ' populate CB2
  With SH
    For i = 2 To .Range("A" & .Rows.Count).End(xlUp).Row
      dic(Format(CDate(.Range("A" & i).Value), "mmm-yyyy")) = Empty
    Next i
  End With
  ComboBox2.List = dic.Keys
End Sub

4. To filter by date if ComboBox2 is changed:

Private Sub ComboBox2_Change()

    If ComboBox2.Value = "" Then Exit Sub

    If ComboBox2.ListIndex = -1 Then Exit Sub
    
    ' clear text search
    TextBox1.Value = ""
    
    With ListBox1
        .RowSource = ""
        .Clear
        .RowSource = "'" & Sheets(ComboBox1.Value).Name & "'!" & Sheets(ComboBox1.Value).Range("A1", Sheets(ComboBox1.Value).Range("H" & Rows.Count).End(3)).Address
        Arr = .List
    End With
    FilterByDate
End Sub

which calls a new sub (based on your code elsewhere in the file):

Sub FilterByDate()
    Dim i As Long, ii As Long, n As Long
    
    If Me.ComboBox2 = "" Then Exit Sub
    
    With Me.ListBox1
    
        If .ListCount > 0 Then
            .RowSource = ""
            .Clear
        End If
        

        ' filter data
        For i = 1 To UBound(Arr, 1)
        ' filter array by chosen date
            If Format(CDate(Arr(i, 0)), "mmm-yyyy") = Me.ComboBox2.Value Then
                .AddItem
                For ii = 0 To UBound(Arr, 2)
                    .List(n, ii) = Arr(i, ii)
                    ' overwrite the date if appropriate
                    If ii = 0 Then .List(n, 0) = Format(CDate(Arr(i, 0)), "dd/mm/yyyy")
                Next ii
                n = n + 1
            End If
        Next
    End With
End Sub

When you filter by TextBox1, you number the rows (and omit the dates) in the first column of the ListBox (but need dates for date fileter). I think you probably need to create an array with both index numbers and dates to make filtering easier .  

Hope this helps- over to you

Discuss

Discussion

thanks  john 
but  when  I  select  the  second  sheet from combobox1 and  MM-YYYY from combobox2then  will clear  listbox  (not  filter  based on sheet name and MM-YYYY)
Mussa (rep: 48) Jan 26, '23 at 5:09 am
Mussa. Not sure what you're saying now. Your question was called "search for data within month" (sic) and you said "when select just combobox2 then will populate in listbox based on month individually"  (and my part code did that with the selected sheet) but without clearing TextBox1. What is it you want? (Note I have very limited time today)
John_Ru (rep: 6142) Jan 26, '23 at 7:58 am
Hi John ,
all of   what  you  said  that's  correct, but  when  you have  time  just  try  selecting   first  sheet  and  select  month  it  works  but  when  you  select  second  sheet and  select   JAN month  will  clear data from listbox  shouldn't 
last  notice :  why  if  I  select  second  sheet  and  I  have  already  selected  month FEB  for  first  sheet  the  data in listbox  will show month JAN ? should  be  clear .  , if I  select JAN-2023  for  first  sheet   , then  will shows error"could not  set the list property" in this line 
 .List(n, ii) = Arr(i, ii)

Mussa (rep: 48) Jan 26, '23 at 8:33 am
Mussa. Please see revised answer/file. The problem was that Arr was not being rewritten when the sheet changed.
John_Ru (rep: 6142) Jan 30, '23 at 10:19 am
 now  works  very  well 
thanks  for  your  fixing  the  errors and  updating .
Mussa (rep: 48) Jan 30, '23 at 10:42 am
Good. Thanks for selecting my Answer, Mussa
John_Ru (rep: 6142) Jan 30, '23 at 10:45 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login