Selected Answer
Kalil
In the attached revised file, I've added a new ComboBox2 (with associated textbox "File Name") to your UserForm.
I've declared a new variable (for your file folderpath) which is at the top of the UserForm code (and so "known" to all UserForm procedures):
Option Explicit
Dim va, vc
Dim oldValue As String
Dim FldPth As String
Then, when the form is launched, this modified code runs (changes in bold, with comments):
Private Sub UserForm_Initialize()
Dim x
For Each x In Array("PURCHASE", "SALES", "VV", "RETURNS")
ComboBox1.AddItem x
Next
ListBox1.ColumnCount = 9
ListBox1.ColumnWidths = "100,60,60,100,80,70,50,50,50"
Dim NxtFl As String
' put your folder path here, ending in backslash \
FldPth = "E:\Documents\Spreadsheets\TeachExcel files\Hasson files\"
' get first matching file
NxtFl = Dir(FldPth & "*.xls*")
' loop
While NxtFl <> ""
' add to CB2, if not this file
If NxtFl <> ThisWorkbook.Name Then ComboBox2.AddItem NxtFl
' get next file (if any)
NxtFl = Dir
Wend
End Sub
where you MUST edit the filepath (just in bold below) before trying this with your files, taking note of the comment:
' put your folder path here, ending in backslash \
FldPth = "E:\Documents\Spreadsheets\TeachExcel files\Hasson files\"
Now the new ComboBox will list files matching the "wildcard" search string in the line starting NxtFl =
If you pick a file, it runs your CB1 code (in case you picked the SHEET first):
Private Sub ComboBox2_Change()
' see if a sheetname was already picked
Call ComboBox1_Change
End Sub
When you pick a sheet (after picking a file), this modified code runs:
Private Sub ComboBox1_Change()
Dim Wb1 As Workbook
' do nothing unless a file and sheetname is picked
If ComboBox1.Value = "" Or ComboBox2.Value = "" Then Exit Sub
Application.ScreenUpdating = False
'Open the selected workbook
Set Wb1 = Workbooks.Open(FldPth & ComboBox2.Value)
Dim n As Long, i As Long
With ComboBox1
va = Empty
If Evaluate("isref('" & .Value & "'!A1)") Then
n = Sheets(.Value).Range("D" & Rows.Count).End(xlUp).Row
va = Sheets(.Value).Range("A2:I" & n)
If n > 1000 Then n = 1000
vc = Sheets(.Value).Range("A2:I" & n)
Call toFormat(va)
Call toFormat(vc)
ListBox1.List = vc
End If
End With
TextBox1 = ""
'Close the workbook after populating listbox
Wb1.Close SaveChanges:=False
Application.ScreenUpdating = True
End Sub
You'll need to change your code for date filters in a similar way but you should get the idea.
Hope this fixes your problem- if so, please remember to mark this Answer as Selected.