+ Reply to Thread
Results 1 to 2 of 2

VBA Code won't loop through all files in the folder

Hybrid View

  1. #1
    Registered User
    Join Date
    03-22-2019
    Location
    Wales
    MS-Off Ver
    2013
    Posts
    1

    VBA Code won't loop through all files in the folder

    I have a bunch of CSV files in one folder. The aim is to copy all the data (excluding the header row) from each file and collate them into one. Below is the code I'm currently using, the problem is the code only copies data from one file then ends completely ignoring all other files in the folder

    Sub get_all_files()
    'DECLARE AND SET VARIABLES
    Dim wbk As Workbook, Filename As String, Path As String
    
    Dim current_workbook As Workbook, wbk_num_rows As Long, counter As Long, i As Long
    
    
    Set current_workbook = ActiveWorkbook
    
    Path = current_workbook.Path & "\" 
    Filename = Dir(Path & "*.csv")
    
    '--------------------------------------------
    'OPEN EXCEL FILES
    
    Application.ScreenUpdating = False
    
    counter = 2
    
     Do While Len(Filename) > 0  'IF NEXT FILE EXISTS THEN
        
        Set wbk = Workbooks.Open(Path & Filename)
    
        wbk_num_rows = wbk.Worksheets(1).Range("A" & Rows.Count).End(xlUp).Row ' number of rows in new opened workbook
        
        For i = 2 To wbk_num_rows
            
            Workbooks(current_workbook.Name).Worksheets(1).Range("A" & counter, "AA" & counter) = wbk.Worksheets(1).Range("A" & counter, "AA" & counter).Value
                          
            counter = counter + 1
            
        Next i
        
        wbk.Close True
            
        
        Workbooks(current_workbook.Name).Worksheets(1).Range("A2:O" & counter).WrapText = False
        
        
        Filename = Dir
    Loop
    
    Application.ScreenUpdating = True
    
    End Sub
    I believe that the problematic line is:

            Workbooks(current_workbook.Name).Worksheets(1).Range("A" & counter, "AA" & counter) = wbk.Worksheets(1).Range("A" & counter, "AA" & counter).Value
    I've come to this conclusion because belovw is the code that us to be in use and my code would loop through very folder. Though since making this change it no longer does.

            Workbooks(current_workbook.Name).Worksheets(1).Range("A" & counter) = wbk.Worksheets(1).Range("AA" & i)
            Workbooks(current_workbook.Name).Worksheets(1).Range("B" & counter) = wbk.Worksheets(1).Range("C" & i) 
            Workbooks(current_workbook.Name).Worksheets(1).Range("C" & counter) = wbk.Worksheets(1).Range("D" & i) 
            Workbooks(current_workbook.Name).Worksheets(1).Range("E" & counter) = wbk.Worksheets(1).Range("I" & i) 
            Workbooks(current_workbook.Name).Worksheets(1).Range("G" & counter) = wbk.Worksheets(1).Range("R" & i) 
            Workbooks(current_workbook.Name).Worksheets(1).Range("H" & counter) = wbk.Worksheets(1).Range("W" & i)
            Workbooks(current_workbook.Name).Worksheets(1).Range("J" & counter) = wbk.Worksheets(1).Range("X" & i) 
            Workbooks(current_workbook.Name).Worksheets(1).Range("K" & counter) = wbk.Worksheets(1).Range("Y" & i) 
            Workbooks(current_workbook.Name).Worksheets(1).Range("L" & counter) = wbk.Worksheets(1).Range("A" & i) 
            Workbooks(current_workbook.Name).Worksheets(1).Range("N" & counter) = "Y"
    The reason I changed the working code is because I no longer required only certain columns, but instead I need all columns to be copied into the file.

    I don't understand why the range change has stopped my program from working as expected . A solution and explanation (if possible) would be much appreciated.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA Code won't loop through all files in the folder

    Hi,

    You need to reset the counter to zero for every new file and change
    Workbooks(current_workbook.Name).Worksheets(1).Range("A" & counter, "AA" & counter) = wbk.Worksheets(1).Range("A" & counter, "AA" & counter).Value
    to
    Workbooks(current_workbook.Name).Worksheets(1).Range("A" & Rows.Count).End(xlUp).Cells(2, 1) = wbk.Worksheets(1).Range("A" & counter, "AA" & counter).Value
    However, assuming the rows in the range A1:AA1 in the file being copied are contiguous and to avoid the need for the loop you could simplify this to

    Sub get_all_files()
    'DECLARE AND SET VARIABLES
        Dim wbk As Workbook, Filename As String, Path As String
    
        Dim current_workbook As Workbook
    
        Set current_workbook = ActiveWorkbook
    
        Path = current_workbook.Path & "\"
        Filename = Dir(Path & "*.csv")
    
    
        '--------------------------------------------
        'OPEN EXCEL FILES
    
        Application.ScreenUpdating = False
    
        Do While Len(Filename) < 0  'IF NEXT FILE EXISTS THEN
    
            Set wbk = Workbooks.Open(Path & Filename)
            Application.DisplayAlerts = False
            wbk.Worksheets(1).Range("A1:AA1").CurrentRegion.Copy
            Workbooks(current_workbook.Name).Worksheets(1).Range("A" & Rows.Count).End(xlUp).Cells(2, 1).PasteSpecial (xlPasteValues)
            wbk.Close True
            Filename = Dir
        Loop
        
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
        Workbooks(current_workbook.Name).Worksheets(1).Range("A2:O2").CurrentRegion.WrapText = False
    End Sub
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

+ 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 Code to extract information from all files in a folder to a mastersheet
    By flaire14 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-22-2017, 08:34 PM
  2. Replies: 0
    Last Post: 05-24-2016, 12:32 PM
  3. [SOLVED] Choose a folder and loop through all files in the chosen folder
    By smartbuyer in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-10-2016, 11:19 PM
  4. [SOLVED] Loop Through Folder, Create Emails with Sub Folder Names in Subject, Attach files in sub
    By Rschwar23 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-30-2015, 10:06 AM
  5. loop through all files in a folder and run code to unmerge and shift
    By ammartino44 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-05-2015, 05:02 PM
  6. code to loop through all .docx files in folder
    By rs1aj in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 04-12-2015, 04:41 PM
  7. Replies: 12
    Last Post: 03-09-2015, 05:52 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