Excel VBA - Listbox populated with file names - Search and import

einargud

New Member
Joined
Jun 15, 2014
Messages
10
Hey folks!

I've made a listbox populated with xlsx files from a specific directory. Inside these excel files are informations about users (name, address, phone numbers, etc.) 9 cells total (A1 to A9).

Let's say the folder which the xlsx files are stored contains about 500 files. Every file looks the same.

My questions are:

How can I search these files (filename) with instant update in listbox? (f.e. if I type the letter 'J' into the searchbox, John will be the only one on the list.)
and
How can I populate cells in Sheet4 (A1 to A9) from a file on the list? (Basically pulling information about John from his xlsx file for example)

Here's what I'm working on.
tRm5w5T.jpg


Here's the code for the listbox.
Code:
Private Sub button_load_Click()

[COLOR=#008000]'Code not found 404 :)[/COLOR]

End Sub




Private Sub UserForm_Initialize()
    Dim fileList() As String
    Dim fName As String
    Dim fPath As String
    Dim I As Integer
   [COLOR=#008000]  'define the directory to be searched for files[/COLOR]
    fPath = "C:\Users\Einar Þór\Desktop\Jötunn_excel\_kaupendur\"
     
   [COLOR=#008000]  'build a list of the files[/COLOR]
    fName = Dir(fPath)
    While fName <> ""
        [COLOR=#008000] 'add fName to the list[/COLOR]
        I = I + 1
        ReDim Preserve fileList(1 To I)
        fileList(I) = fName
        [COLOR=#008000] 'get next filename[/COLOR]
        fName = Dir()
    Wend
    [COLOR=#008000] 'see if any files were found[/COLOR]
    If I = 0 Then
        MsgBox "No files found"
        Exit Sub
    End If
    [COLOR=#008000] 'cycle through the list and add to listbox[/COLOR]
    For I = 1 To UBound(fileList)
        Me.ListBox1.AddItem fileList(I)
    Next
End Sub

Thank you for your time.
 
The OP did kind of say they wanted an 'instant' update as they typed in the textbox.:)
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
The OP did kind of say they wanted an 'instant' update as they typed in the textbox.:)

if he doesn't have a huge array to filter (considering it is a list of files, it shouldn't be), he should use Change Event if he likes the "instant update" experience :)
 
Upvote 0
the different file extension shouldn't be the cause. There has to be an issue with the original code, maybe you didn't declare the fileList array at the module level?

This works perfectly! And it's set up exactly like mine. But mine does not work at all!

Is it the file type? Binary instead of macro-enabled? or am I overlooking some dumb typo?

As Norie pointed out, putting _Change instead of _BeforeUpdate with your code worked really well and is exactly like how I want it to be.
 
Upvote 0
VBA_Geek

What's the problem if there's a large array?

That shouldn't affect the filter.
 
Upvote 0
the Filter Function is called at every letter you type into or delete from the textbox, i do love the instant updating experience but from an efficiency point of view i prefer the other event as it calls the Filter just one time
 
Upvote 0
This is weird. I think I've checked for everything, yet, this error comes up:
F5jQRa2.jpg


This is highlighted:
lHRzlht.jpg



Any ideas?

ps. Any thoughts on the second question? importing data from those listBox files? :)
 
Upvote 0
you get this compile error because there is no reference added to the Microsoft Scripting RunTime, go to Tools > References and add it. this error should then disappear
 
Upvote 0
VBA Geek

I know that every time you type/delete in the textbox the filter code will be triggered, but that code executes almost instantaneously so I'm not sure about there being a problem with efficiency.

Also, to trigger the BeforeUpdate code you either need to press enter or move to another control, and when you do that the focus moves to either the next control in the tab order or the control you moved to.
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

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