Solved

Amend VBA Code to run through all files within a folder

Posted on 2011-02-16
4
267 Views
Last Modified: 2012-05-11
Good Afternoon

Is it possible to amend the following code to loop through each file in a folder, regardless of the filename?

Many thanks
Sub AppendData()

    Dim wbkMaster As Workbook
    Dim shtMaster As Worksheet
    Dim rngMaster As Range
    Dim wbkData As Workbook
    Dim shtData As Worksheet
    Dim rngData As Range
    
' change path and file name to suit
    Set wbkMaster = Workbooks.Open("D:\temp\testmaster.xls")
    Set shtMaster = wbkMaster.Worksheets(1)
    
' change path and file name to suit
    Set wbkData = Workbooks.Open("D:\temp\test1.xls")
    Set shtData = wbkData.Worksheets(1)
    
' get end of master
    Set rngMaster = shtMaster.Range("A65536").End(xlUp)
' Change as per your need
    Set rngData = shtData.Range("A11:A" & shtData.Range("A11").End(xlDown).Row)
' copy data across
    rngData.Copy rngMaster
    
    MsgBox "Appended " & rngData.Rows.Count & " rows of data to Master data", vbInformation
    
' simply close data
    wbkData.Close False
' save and close master
    wbkMaster.Close True

'  release objects
    Set rngData = Nothing
    Set shtData = Nothing
    Set wbkData = Nothing
    Set rngMaster = Nothing
    Set shtMaster = Nothing
    Set wbkMaster = Nothing
End Sub

Open in new window

0
Comment
Question by:Washcare
  • 2
4 Comments
 
LVL 33

Expert Comment

by:jppinto
ID: 34907142
Please check this code to see if you can find what you're looking for:

http://excel-user.blogspot.com/2009/10/replace-value-on-several-excel-files.html

jppinto
0
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
ID: 34907276
This uses DIR

Sub AppendData()
    Dim wbkMaster As Workbook, wbkData As Workbook
    Dim shtMaster As Worksheet, shtData As Worksheet
    Dim rngMaster As Range, rngData As Range
    Dim FolderToSearch As String, strPath As String
    
    Set wbkMaster = Workbooks.Open("D:\temp\testmaster.xls")
    Set shtMaster = wbkMaster.Worksheets(1)
    
    '~~> Folder where you want to seach files
    FolderToSearch = "C:\Temp\"
    strPath = FolderToSearch & "*.xls"
    
    strPath = Dir(strPath, vbNormal)
    
    Do While strPath <> ""
        Set wbkData = Workbooks.Open(FolderToSearch & strPath)
        Set shtData = wbkData.Worksheets(1)
        
        Set rngMaster = shtMaster.Range("A65536").End(xlUp)
        Set rngData = shtData.Range("A11:A" & shtData.Range("A11").End(xlDown).Row)
        rngData.Copy rngMaster
        
        MsgBox "Appended " & rngData.Rows.Count & " rows of data to Master data", vbInformation
        
        wbkData.Close False
        strPath = Dir
    Loop
    
    wbkMaster.Close True

    Set rngData = Nothing
    Set shtData = Nothing
    Set wbkData = Nothing
    Set rngMaster = Nothing
    Set shtMaster = Nothing
    Set wbkMaster = Nothing
End Sub

Open in new window


Sid
0
 

Author Comment

by:Washcare
ID: 34907335
Sid

Thank you for the amendment. At some point in the code I think I need testmaster.xls, to be saved and closed before moving onto the next file, otherwise the changes will be lost?

Many thanks
0
 

Author Comment

by:Washcare
ID: 34907394
Sorry

I have moved testmaster.xls out of the folder which I am looping through issue resolved.

Thank you
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now