Solved

Amend VBA Code to run through all files within a folder

Posted on 2011-02-16
4
266 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry

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

Thank you
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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

12 Experts available now in Live!

Get 1:1 Help Now