Results 1 to 5 of 5

Macro to choose folder and cycle through all files

Threaded View

  1. #1
    Registered User
    Join Date
    06-20-2011
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    6

    Macro to choose folder and cycle through all files

    Thanks in advance.

    I have a workbook that contains many control charts and currently it requires someone to open each file in a folder to enter all the data into the workbook using a hot-key'd macro. I'd like to be able to cut down the work involved and just have the user choose the folder so that a similar macro could copy all the data into the workbook where the macros already in place would be able to compile it.

    While I've seen may similar questions on this forum. This code http://www.excelforum.com/forum-rule...rum-rules.htmlappears to be what I believe I need,so I've altered it to my needs.


    I'm not sure why the code below will not work:

    Sub Runfolder()
    
    Dim screenUpdateState       As Variant
    Dim statusBarState          As Variant
    Dim eventsState             As Variant
    Dim fso                     As Object
    Dim fPath                   As String
    Dim myFolder, myFile
    Dim wb                      As Workbook
    Dim SavePath                As String
    Dim I, x                    As Integer
    Dim ws                      As Worksheet
    
    ' Turn off some Excel functionality so your code runs faster
        screenUpdateState = Application.ScreenUpdating
        statusBarState = Application.DisplayStatusBar
        eventsState = Application.EnableEvents
    
        Application.ScreenUpdating = False
        Application.DisplayStatusBar = False
        Application.EnableEvents = False
    
    ' Use File System Object to choose folder with files
        Set fso = CreateObject("Scripting.FileSystemObject")
        
        With Application.FileDialog(msoFileDialogFolderPicker)
            .AllowMultiSelect = False
            .Show
            If .SelectedItems.Count > 0 Then fPath = .SelectedItems(1) & "\"
        End With
    
    ' Open each file consequently
            Set myFolder = fso.GetFolder(fPath).Files
                For Each myFile In myFolder
                    If LCase(myFile) Like "*.xls*" Then
                        For I = 1 To x
    
    ' Perform tasks with each file
                            Set wb = Workbooks.Open(myFile)
                            With wb.Worksheets("summary")
          'my changes
          Sheets("Summary").Select
         Cells.Select
        Selection.Copy
        Windows("Stats2011.xlsm").Activate
        Sheets("Paste").Select
        Cells.Select
        ActiveSheet.Paste
                               
                                
                                
                            End With
                            
                     
                           
                            ' Close file
                            wb.Close True
    
    ' Loop through all files in folder
                        Next I
                    End If
                Next myFile
    
    'clean up
        myFile = vbNullString
        I = 1
    
    ' Turn Excel functionality back on
        Application.ScreenUpdating = screenUpdateState
        Application.DisplayStatusBar = statusBarState
        Application.EnableEvents = eventsState
    
    End Sub
    Last edited by Mr.Pinches; 07-08-2011 at 04:35 PM. Reason: [Solved]

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