Populate listbox based on file name

Mumba

New Member
Joined
May 3, 2018
Messages
14
Hi Guys

I am looking to populate 2 different listboxes but the files i wish to fill into the listbox are in the same folder.
Currently i can get the code to fill listbox1 with all files but 1, which is put in the second listbox...

I have been looking at a lot of threads, but i can't seem to find identical issues.

Code:
Private Sub UserForm_Initialize()

'x is a variable that is use to change the folder path in the userform
    If x = 0 Then
        TextBox1.Value = Year(Now)
        TextBox2.Value = Month(Now)
    Else
        TextBox1.Value = TextBox1.Value
        TextBox2.Value = TextBox2.Value
    End If
    
fpath = ThisWorkbook.Path & "\Extracts\" & TextBox1.Value & "\" & TextBox2.Value & "\"
orderfile = Dir(fpath & "Order*.xls*", vbNormal)
stockfile = Dir(fpath & "Lager*.xls*", vbNormal)

ListBox1.Clear
ListBox2.Clear

Call CheckFolder
    'Do While stockfile <> ""
     Do While Len(orderfile) > 0
        ListBox1.AddItem orderfile
        orderfile = Dir(orderfile)
    Loop
    
    Do While Len(stockfile) > 0
        ListBox2.AddItem stockfile
        stockfile = Dir()
    Loop
    
x = 0
Stop
End Sub

Private Sub CheckFolder()

If Dir(fpath & "\*.*") = "" Then
    MsgBox "There are no files available" & vbNewLine & "Please save stockfile in the following folder:" & vbNewLine & fpath
End If
End Sub
[\Code]
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
You can have two Dir() running at the same time. Try it this way:

Code:
Private Sub UserForm_Initialize()

'x is a variable that is use to change the folder path in the userform
If x = 0 Then
    TextBox1.Value = Year(Now)
    TextBox2.Value = Month(Now)
Else
    TextBox1.Value = TextBox1.Value
    TextBox2.Value = TextBox2.Value
End If

fpath = ThisWorkbook.Path & "\Extracts\" & TextBox1.Value & "\" & TextBox2.Value & "\"

ListBox1.Clear
ListBox2.Clear
Call CheckFolder

orderfile = Dir(fpath & "Order*.xls*", vbNormal)
Do While Len(orderfile) > 0
    ListBox1.AddItem orderfile
    orderfile = Dir
Loop

stockfile = Dir(fpath & "Lager*.xls*", vbNormal)
Do While Len(stockfile) > 0
    ListBox2.AddItem stockfile
    stockfile = Dir
Loop

x = 0
Stop ' Stop? Really?

End Sub
Private Sub CheckFolder()

If Dir(fpath & "\*.*") = "" Then
    MsgBox "There are no files available" & vbNewLine & "Please save stockfile in the following folder:" & vbNewLine & fpath
End If

End Sub

WBD
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top