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