+ Reply to Thread
Results 1 to 7 of 7

Loop to import worksheet from different workbooks inside one folder

Hybrid View

  1. #1
    Registered User
    Join Date
    07-05-2018
    Location
    Bonn
    MS-Off Ver
    MS Office 2010
    Posts
    6

    Loop to import worksheet from different workbooks inside one folder

    Hi everyone,

    I hope someone will be able to help me with my issue.

    First, I would like to provide a little background. The goal of my code is to import each first worksheet from all workbooks inside a folder. Each worksheet is imported, processed and exported to a new folder, this is done to each file, but not at once. In order to do that, I am using a loop. I am not that much experienced with VBA, and I builded up the code below by googling, checking different threads, etc.

    My issue: the code is always processing only one file from the folder (infinite times) and never catching the others. Since we will be working with hundreds and maybe even thousands of files, I cannot rename the files to be imported, thereby their names do not follow any pattern, which is why the code should go through all ".xls" files located in a specific folder.

    Sub CombineSheets()
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
        Dim sPath As String
        Dim sFname As String
        Dim wBk As Workbook
        Dim wSht As Variant
        Dim stPath As String
        Dim myPath As String
        Dim myExtension As String
        Dim NewBook As Workbook
        Dim Input_Folder As Range
        Dim Output_Folder As Range
    
    Set Input_Folder = Sheets("Menu").Cells(18, 11)
    Set Output_Folder = Sheets("Menu").Cells(19, 11)
    
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        
           myPath = Input_Folder & "\"
              
        myExtension = "*.xls*"
        myFile = Dir(myPath & myExtension, vbNormal)
        
        ChDir myPath
        
        sFname = Dir(sPath & "*.xls*", vbNormal)
        wSht = InputBox("Enter a worksheet name to copy")
           
        Do While myFile <> ""
            ChDir myPath
            Set wBk = Workbooks.Open(myFile)
            Windows(myFile).Activate
            Sheets(wSht).Copy Before:=ThisWorkbook.Sheets(1)
            wBk.Close False
            Call Import_Pre_Processing
            Call Fill_Data
    
            Sheets("tarif_client_COMPLET").Delete
            
            'Save Steps
        Sheets("Menu").Select
        ActiveCell.FormulaR1C1 = "=tariffs!R[480]C[-9]"
        Range("K17").Select
        Selection.Copy
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Application.CutCopyMode = False
        Dim Output_filename As Range
        Set Output_filename = ThisWorkbook.Sheets("Menu").Cells(17, 11)
        Set NewBook = Workbooks.Add
        ThisWorkbook.Sheets("tariffs").Copy Before:=NewBook.Sheets(1)
     
        If Dir(Output_Folder & "\" & Output_filename & ".xls") <> "" Then
            MsgBox "File " & Output_Folder & "\" & Output_filename & ".xls" & " already exists"
        Else
            NewBook.SaveAs fileName:=Output_Folder & "\" & Output_filename & ".xls"
        End If
        Application.ActiveWorkbook.Close False
     Loop
      
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    
    MsgBox "You can find the files in " & Output_Folder
    End Sub
    Thanks in advance for any help

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: Loop to import worksheet from different workbooks inside one folder


    Hi !

    Quote Originally Posted by lucas.lobo View Post
    Each worksheet is imported, processed and exported to a new folder.
    Imported : where ? That needs a clear explanation of each step …

    What is your question, what are you expecting for ?

    It seems you forgot to follow the Dir VBA help sample, a must read …

  3. #3
    Registered User
    Join Date
    07-05-2018
    Location
    Bonn
    MS-Off Ver
    MS Office 2010
    Posts
    6

    Re: Loop to import worksheet from different workbooks inside one folder

    Hi Marc,

    Thanks for the reply.

    Sorry if it was not clear.

    Before running the code, the user must insert into: Sheets("Menu").Cells(18, 11) , the full path to the folder where the .xls files to be imported are located.

    My expectation is to import all the .xls files from that given folder. However, I am struggling with that because the code is finding repeatedly and for inifinite times the very same file, while it should process the 'next' file, i.e. a different one.

    And thanks for the tip, I will look into the Dir VBA help sample and try to fix this.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Loop to import worksheet from different workbooks inside one folder

    Quote Originally Posted by lucas.lobo View Post
    because the code is finding repeatedly and for inifinite times the very same file
    As you just forgot before the Loop statement to load the next file in the file variable as you can see in the Dir sample …

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Loop to import worksheet from different workbooks inside one folder

    There was a couple of problems.
    1.) As Marc suggested, to get the next file name in the loop you have to use myFile = Dir command at the bottom of the loop. This will retrieve the next file in the folder using the original path from earlier myFile = Dir(myPath & myExtension, vbNormal)

    2.) A caveat with the Dir function is; to get the next file in the folder, you cannot use the Dir function anywhere else in the loop to do something else. If you do, you lose the ability to get the next file in the original folder. In your case, you also used the Dir function within the loop to test if the new filename already existed. In the code below I use another method (FileSystemObject) to test if the file name already exists.

    Try this. Not tested. Very likely there will be an error.

    Sub CombineSheets()
        
        Dim myPath             As String
        Dim myFile             As String
        Dim strOutput_Folder   As String
        Dim strOutput_filename As String
        Dim FSO                As Object
        
        Set FSO = CreateObject("Scripting.FileSystemObject")    '<--to test if file exists
        
        Application.ScreenUpdating = False
        
        strOutput_Folder = Sheets("Menu").Cells(19, 11).Value & "\"
        myPath = Sheets("Menu").Cells(18, 11).Value & "\"
        myFile = Dir(myPath & "*.xls*", vbNormal)
        
        Do While myFile <> ""
            
            With Workbooks.Open(myPath & myFile)
                .Sheets(1).Copy Before:=ThisWorkbook.Sheets(1)
                .Close False
            End With
            
            Call Import_Pre_Processing
            Call Fill_Data
            Sheets("tarif_client_COMPLET").Delete
            
            'Save Steps
            With ThisWorkbook.Sheets("Menu")
                .Range("K17").FormulaR1C1 = "=tariffs!R[480]C[-9]"
                strOutput_filename = .Range("K17").Value & ".xls"
            End With
            
            If FSO.FileExists(strOutput_Folder & strOutput_filename) Then    'Test if file exists
                MsgBox strOutput_Folder & strOutput_filename, vbExclamation, "File Already Exists"
            Else
                ThisWorkbook.Sheets("tariffs").Copy    'Copy sheet as new workbook
                ActiveWorkbook.SaveAs Filename:=strOutput_Folder & strOutput_filename
                ActiveWorkbook.Close False
            End If
            
            myFile = Dir    'next file
            
        Loop
        
        Application.ScreenUpdating = True
        
        MsgBox "You can find the files in " & strOutput_Folder, vbInformation, "Combine Sheets Complete"
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  6. #6
    Registered User
    Join Date
    07-05-2018
    Location
    Bonn
    MS-Off Ver
    MS Office 2010
    Posts
    6

    Re: Loop to import worksheet from different workbooks inside one folder

    Hi Marc and AlphaFrog,

    I just wanted to say thanks! Now the code is working perfectly.

    Thanks also for pointing out to what I did wrong - very helpful to avoid the same mistake another time

    Regards
    Lucas

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Loop to import worksheet from different workbooks inside one folder

    You're welcome. Thanks for the feedback.

+ 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. Loop through folder with txt files, import them and save as xls file
    By maym in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-06-2017, 02:45 AM
  2. Creating a folder in desktop and inside it, save new workbooks...
    By jcmc2112 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-07-2014, 02:04 PM
  3. [SOLVED] Loop Through all workbooks in a Folder
    By ag273n in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-21-2014, 02:17 PM
  4. Loop Through a Folder of Excel Workbooks
    By frankgbr in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-01-2013, 12:18 AM
  5. Replies: 0
    Last Post: 04-25-2013, 03:20 AM
  6. For loop which goes through all workbooks in a folder
    By secondrate in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-07-2010, 02:49 PM
  7. [SOLVED] Loop through folder of workbooks and add rows
    By FIRSTROUNDKO via OfficeKB.com in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 08-10-2006, 02:55 PM

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