+ Reply to Thread
Results 1 to 9 of 9

Paste the contents of several folders into a spreadsheet

Hybrid View

  1. #1
    Registered User
    Join Date
    08-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Smile Paste the contents of several folders into a spreadsheet

    Using a macro, I'm wanting to take a folder that has several sub-folders and have the sub-folder names go in the top row of each column in a spreadsheet and the files within each folder entered below each corresponding folder, preferably while being able to specify file types. I found a macro for pasting the contents of one folder at a time, but it doesn't enter the folder name, it doesn't loop and it places the file name starting on the active cell. I just want A1 to have the name of subfolder 1, B1 to have subfolder 2, and so on. It would also be nice if it added a new sheet each time the macro was ran. Can anyone help me with this?

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Paste the contents of several folders into a spreadsheet

    Paste the CONTENTS of those folders into individual columns, or paste a LISTING of the filenames in said folders?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    08-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Paste the contents of several folders into a spreadsheet

    It would be a listing of the filenames. For example, I would want it to list all the tif filenames in every subfolder in each column under the subfolder's name.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Paste the contents of several folders into a spreadsheet

    These three macros work together to accomplish this task. Drop them all into a new Module, edit the initialfilename in the StartingMacro so that it opens to a folder closer to where you expect to start. Then run that macro, it will use the other two macros to loop through every folder in the selected folder and run the HyperlinkFiles macro on each folder separately.

    Option Explicit
    Dim wsList As Worksheet, Col As Long, Ext As String, AddLinks As Boolean
    
    Sub StartingMacro()
    Dim calcmode As Long, fPATH As String
    
    Application.ScreenUpdating = False      ' Set various application properties.
    
    'Select folder
        With Application.FileDialog(msoFileDialogFolderPicker)
            .AllowMultiSelect = False
            .InitialFileName = "C:\2013\"
            .Show
            If .SelectedItems.Count > 0 Then
                fPATH = .SelectedItems(1)
            Else
                Exit Sub
            End If
        End With
    
    'Select the file type to list
        Ext = Application.InputBox("What kind of files? Type the file extension to collect" _
                & vbLf & vbLf & "(Example:  jpg, tif, gif, bmp, *)", "File Type", "tif", Type:=2)
    
        If Ext = "False" Then Exit Sub
    
    'Option to create hyperlinks
        AddLinks = MsgBox("Add hyperlinks to the file listing?", vbYesNo) = vbYes
    
    ' Add a new worksheet
        Set wsList = Sheets.Add(After:=Sheets(Sheets.Count))
        Col = 1
        wsList.Cells(1, Col) = fPATH
    
        Call LoopController(fPATH)      'starts the loop feeding in the main folder
    
    wsList.Columns.AutoFit
    Application.ScreenUpdating = True   ' Restore the application properties.
    
    End Sub
    
    
    Private Sub LoopController(sSourceFolder As String)
    'This will loop into itself, first processing the files in the folder
    'then looping into each subfolder deeper and deeper until all folders processed
    Dim Fldr As Object, FL As Object, SubFldr As Object
    
        Call HyperlinkFiles(sSourceFolder & Application.PathSeparator)
    
        Set Fldr = CreateObject("scripting.filesystemobject").Getfolder(sSourceFolder)
        For Each SubFldr In Fldr.SubFolders
            LoopController SubFldr.Path
        Next
    
    End Sub
    
    Sub HyperlinkFiles(fPATH As String)
    'Author:    Jerry Beaucaire, ExcelForum.com
    'Date:      10/8/2010
    'Summary:   User selects a folder and file type, macro returns
    '           a complete listing of all files matching that type
    '           with a hyperlink to the file for ease of opening
    Dim fname As String, NR As Long
    
    Col = Col + 1
    NR = 2
    
        With wsList
            .Cells(1, Col) = fPATH
    
            fname = Dir(fPATH & "*." & Ext)
    
            Do While Len(fname) > 0
                .Cells(NR, Col) = fname
                If AddLinks Then .Hyperlinks.Add Anchor:=.Cells(NR, Col), _
                    Address:=fPATH & fname, TextToDisplay:=fname
              'set for next entry
                NR = NR + 1
                fname = Dir
            Loop
    
        End With
    
    End Sub

  5. #5
    Registered User
    Join Date
    08-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Paste the contents of several folders into a spreadsheet

    Thanks for the code. I gave it a try and I keep getting a "variable not defined" compile error for the LoopController sub routine for some reason. I am not experienced enough to figure it out unfortunately. From what I can tell the macro appears to be just what I was looking for with lots of flexibility. Any ideas on what could be causing the error?

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Paste the contents of several folders into a spreadsheet

    Only way I can review what you've done in your workbook is to see the workbook.

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic


    Make sure all that code above goes into a single Module (Insert > Module)

  7. #7
    Registered User
    Join Date
    08-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Paste the contents of several folders into a spreadsheet

    I just opened a blank spreadsheet and VB, then pasted the code into a newly inserted Module and ran it. Is there supposed to be something in the spreadsheet prior to running the macro?

  8. #8
    Registered User
    Join Date
    08-12-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Paste the contents of several folders into a spreadsheet

    I don't know what is different, but I tried running it and it worked without getting any compiling errors. Thanks for the code!

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Paste the contents of several folders into a spreadsheet

    You're very welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Paste into new workbook and save as depending on folders contents
    By ScabbyDog in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-09-2013, 04:48 PM
  2. [SOLVED] Copy/Paste & Clear Contents Macro on multiple spreadsheet
    By greggatz in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-01-2012, 10:16 AM
  3. Macro to list folders and contents within it Including subfolders.
    By pekunda in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-12-2012, 08:17 AM
  4. extracting file contents from different folders!
    By via135 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-11-2008, 02:21 PM
  5. paste contents from other spreadsheet
    By JohnT in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-08-2005, 12:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1