+ Reply to Thread
Results 1 to 7 of 7

Macro to go thru directory of Excel files

Hybrid View

  1. #1
    Registered User
    Join Date
    03-02-2005
    Location
    Greenville, SC, USA
    Posts
    41

    Macro to go thru directory of Excel files

    Looking for someway to go thru a directory (with or without sub folders) and for every Excel file it finds, open the file and Copy - Paste Special Values each worksheet in the workbook. I wouldn't know how many sheets any given workbook may have until it opened. Can this be done? Copy and paste part is easy enough but hoping not to have to have someone open each one and then run a copy paste macro for each sheet and workbook.
    Last edited by stvgarner; 09-06-2011 at 08:24 AM.

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Macro to go thru directory of Excel files

    Hi,

    Try this:

    Sub LoopFiles()
    
    Dim strDir As String, strFileName As String
    Dim wbOpenBook As Workbook, wsEachSheet As Worksheet
    
    strDir = "C:\Test\"
    strFileName = Dir(strDir & "*.xls")
    
    Do While strFileName <> ""
        Set wbOpenBook = Workbooks.Open(strDir & strFileName)
        For Each wsEachSheet In wbOpenBook.Worksheets
            With wsEachSheet.UsedRange
                .Copy
                .Range("A1").PasteSpecial xlPasteValues
            End With
        Next wsEachSheet
        wbOpenBook.Close True
        strFileName = Dir
    Loop
    
    End Sub

    Change the directory to search as required.

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: Macro to go thru directory of Excel files

    Hi,

    You can use this, this doesn't take into account subfolder, so you may have to loop threw directories, but adjust the code arcordingly.

    
    Sub Steffen()
         
        Dim objFSO As Object
        Dim objFile As Object
        Const strDIR As String = "Yoy directory to look in"
         
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        MsgBox "There are " & objFSO.GetFolder(strDIR).Files.Count & " files in the folder."
        For Each objFile In objFSO.GetFolder(strDIR).Files
            If InStr(1, objFile, ".xls") > 0 Then
                Dim xlApp As Excel.Application
                Application.DisplayAlerts = False
                Workbooks.Open (objFile)
                ws = ThisWorkbook.Sheets.Count
                    For i = 1 To ws
                    ' insert procedure here
                    ' Format Sheets(1).Range("your range to copy").copy
                    ' Workbook.Sheet.Range.Insert (Where to insert the copied range)
                    Next i
                Workbooks(objFile.Name).Close
            End If
        Next objFile
         
        Set objFile = Nothing
        Set objFSO = Nothing
        Application.DisplayAlerts = True
         
    End Sub
    Be aware if you have many files it will be a heavy procedure.

    Steffen Thomsen
    Last edited by Steffen Thomsen; 09-01-2011 at 10:00 AM.

  4. #4
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Macro to go thru directory of Excel files

    snb posted examples of how to loop through directories here which includes code to take account of subfolders:

    http://www.excelforum.com/tips-and-t...ernatives.html

    Dom

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to go thru directory of Excel files

    Hi stvgarner

    I was able to adapt Leith Ross' code from this link to do something similar to what you describe
    http://www.excelforum.com/excel-prog...ml#post2474970
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  6. #6
    Registered User
    Join Date
    03-02-2005
    Location
    Greenville, SC, USA
    Posts
    41

    Re: Macro to go thru directory of Excel files

    I think this will do it! Thanks!!!

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Macro to go thru directory of Excel files

    Hi stvgarner
    Glad to have been of help (if I was).

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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