?
Solved

Amend VBA Code to run through all files within a folder

Posted on 2011-02-16
4
Medium Priority
?
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
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…

777 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