Need help with a Find Macro

copleyr

Active Member
Joined
Aug 24, 2009
Messages
381
Hello!

I am in desperate need of a macro.

1: On one spreadsheet, I need a macro that can look into a drive (i.e. C:\Program Files\excel files_folder).

2: The list of excel files that are in this folder each have a specific name (i.e. Math001, Math002, Math003)

Is there a way that I can get a macro that can look into this folder and list the first 6 characters of each file in the folder?

I have thousands of these constantly changing files at work and am in desperate need of a macro that can do this.


Thank you in advance!!!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Code:
Sub ListFiles()
Dim MyPathName As String
Dim MyFileName As String
Dim NumChars As Long
Dim X As Long
    NumChars = 6 'Change this to the number of characters you wish to return
    MyPathName = "C:\Program Files\excel files_folder\*.xl*" 'Change this to the folder and filetypes you want to return
    MyFileName = Dir(MyPathName)
    Do While MyFileName <> ""
        X = X + 1
        Sheet1.Cells(X, 1) = Left(MyFileName, NumChars)
        MyFileName = Dir
    Loop
End Sub

Cheers

Dan
 
Upvote 0
Thank you so much!

It worked perfectly when I accessed my hard drive.

However, when I tried to get it to access a drive on a network I am connected to, it wouldn't do anything.

Will I need to give it a new path? I used the same path as my C: drive and it worked well. When I tried to get it to access a K: drive on my network, nothing happened.

Thanks!
 
Upvote 0
Nevermind I got it to work! I wasn't putting a "\" at the end of the path.

Thanks again so much@!
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,277
Members
452,902
Latest member
Knuddeluff

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