Use Excel 2007 to create a File Location and List

Christopher_Green

New Member
Joined
Apr 28, 2013
Messages
12
Hi,

I have the below macro that will look in a specific File location / directory and produce me a list of files within that area.

If possible i would like to change the macro so that rather then me going into the VBA to change the location an input message box asks me which file location i want to generate the list from.

I would also then like the Macro to concatenate this information togeather for me to produce the full file path e.g. (C:\Documents and Settings\greenc4\My Documents\Document 1.doc)

The last things is i would like it to produce this information on the same sheet and not add a new one when the macro runs.

I hope this makes sense.

Here is the macro:

Code:
Sub List_All_Files()
     
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim ws As Worksheet
     
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set ws = Worksheets.Add
     
     'Get the folder object associated with the directory
    Set objFolder = objFSO.GetFolder("C:\")
    ws.Cells(1, 1).Value = "The files found in " & objFolder.Name & "are:"
     
     'Loop through the Files collection
    For Each objFile In objFolder.Files
        ws.Cells(ws.UsedRange.Rows.Count + 1, 1).Value = objFile.Name
    Next
     
     'Clean up!
    Set objFolder = Nothing
    Set objFile = Nothing
    Set objFSO = Nothing
     
End Sub

I am thankful for any help you can give.

Thanks

Christopher Green
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Code:
Sub List_Files()
Dim par As String
Dim sfil As String
Dim shellApp As Object
Set shellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, "C:\")
Dim cel As Range
Set cel = ActiveCell
par = shellApp.self.Path
sfil = Dir(par & "\" & "*.*")
    Do Until sfil = ""
        cel.Hyperlinks.Add cel, par & "\" & sfil, , , par & "\" & sfil
        Set cel = cel.Offset(1)
        sfil = Dir$
    Loop
ActiveCell.EntireColumn.AutoFit
End Sub
 
Upvote 0
Hi Irobbo314,

The Macro works great thanks, i changed the location of the macro to "" so that it can pick up from anywhere and not just my C drive.

Here is the amended codes:

Code:
Sub List_Files()
Dim par As String
Dim sfil As String
Dim shellApp As Object
Set shellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, "")
Dim cel As Range
Set cel = ActiveCell
par = shellApp.self.Path
sfil = Dir(par & "\" & "*.*")
    Do Until sfil = ""
        cel.Hyperlinks.Add cel, par & "\" & sfil, , , par & "\" & sfil
        Set cel = cel.Offset(1)
        sfil = Dir$
    Loop
ActiveCell.EntireColumn.AutoFit
End Sub

Thanks for your help with this

Chris :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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